CheckSintaxCommand.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace CheckSintaxBundle\Command;
  3. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Yaml\Yaml;
  8. class CheckSintaxCommand extends ContainerAwareCommand
  9. {
  10. /**
  11. * @var array|CheckChars
  12. */
  13. private $chars = [];
  14. private $debug;
  15. protected function configure()
  16. {
  17. $this
  18. ->setName('check:sintax')
  19. ->setDescription('Check sintax for those characters that need open and close')
  20. ->addOption('dir', '', InputOption::VALUE_OPTIONAL, 'Directory to read all yaml files in it.', '')
  21. ->addOption('yml', '', InputOption::VALUE_OPTIONAL, 'Path to read yaml file.', '')
  22. ->addOption('string', '', InputOption::VALUE_OPTIONAL, 'String to check.', '')
  23. ->addOption('debug', '', InputOption::VALUE_OPTIONAL, 'Debug mode.', false)
  24. ->setHelp('Check sintax for those characters that need open and close');
  25. }
  26. /**
  27. * @param InputInterface $input
  28. * @param OutputInterface $output
  29. * @return int Return 0 if OK, otherwise 1
  30. */
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. $dir = $input->getOption("dir");
  34. $fileYaml = $input->getOption("yml");
  35. $string = $input->getOption("string");
  36. $this->debug = $input->getOption("debug");
  37. $this->loadParameters();
  38. $error = "Nothing to do.";
  39. if (trim($fileYaml) != "") {
  40. $output->writeln("Checking sintax " . $fileYaml);
  41. $data = Yaml::parse(file_get_contents($fileYaml));
  42. $error = $this->checkFileArray($data);
  43. } elseif (strlen(trim($dir)) > 0) {
  44. $error = $this->checkPathFile($output, $dir);
  45. } elseif (strlen(trim($string)) > 0) {
  46. $error = $this->verifyString($string);
  47. }
  48. if (is_null($error)) {
  49. $this->returnOK($output);
  50. return 0;
  51. } else {
  52. $output->writeln("CHECK SINTAX ERROR!!!");
  53. $output->writeln($error);
  54. }
  55. return 1;
  56. }
  57. /**
  58. * Load parameters form file.
  59. */
  60. private function loadParameters()
  61. {
  62. $fileLocator = $this->getContainer()->get('file_locator');
  63. $templates = $fileLocator->locate('@CheckSintaxBundle/Resources/templates.txt');
  64. $lines = explode(PHP_EOL, file_get_contents($templates));
  65. foreach ($lines as $l) {
  66. $ch = explode(":", trim($l));
  67. if (count($ch) === 2) {
  68. $this->chars[] = new CheckChars($ch[0], $ch[1]);
  69. }
  70. }
  71. }
  72. /**
  73. * Write string ok.
  74. * @param OutputInterface $output
  75. */
  76. private function returnOK(OutputInterface $output)
  77. {
  78. $output->writeln("CHECK SINTAX OK!!!");
  79. }
  80. /**
  81. * Write error found.
  82. */
  83. private function checkCharsArrayErrors()
  84. {
  85. $error = null;
  86. foreach ($this->chars as $cc) {
  87. if ($cc->getCounter() != 0) {
  88. if ($cc->getCounter() > 0) {
  89. $error = "There is more character " . $cc->getOpen() . " open (" . $cc->getCounter() . ") than character " . $cc->getClose() . " close.";
  90. } else {
  91. $error = "There is more character " . $cc->getClose() . " close (" . ($cc->getCounter() * -1) . ") than character " . $cc->getOpen() . " open.";
  92. }
  93. }
  94. }
  95. return $error;
  96. }
  97. /**
  98. * Reset de counters chars.
  99. */
  100. private function resetCounters()
  101. {
  102. foreach ($this->chars as $cc) {
  103. $cc->resetCounter();
  104. }
  105. }
  106. /**
  107. * @param OutputInterface $output
  108. * @param string $dir Contains directory to read all files in it.
  109. * @return string|null Return error.
  110. */
  111. private function checkPathFile(OutputInterface $output, $dir)
  112. {
  113. $files = scandir($dir);
  114. $error = null;
  115. foreach ($files as $file) {
  116. if (is_file($dir . $file)) {
  117. $output->writeln("Checking sintax " . $dir . $file);
  118. $errorTmp = $this->checkFileArray(Yaml::parse(file_get_contents($dir . $file)));
  119. if (!is_null($errorTmp) && strlen(trim($errorTmp)) > 0) {
  120. if (is_null($error)) {
  121. $error = "\n$dir$file";
  122. } else {
  123. $error = "$error\n--------------------------------------------------------------------------------\n$dir$file";
  124. }
  125. $error = "$error\n$errorTmp\n";
  126. }
  127. }
  128. }
  129. return $error;
  130. }
  131. /**
  132. * @param array $data Contains array with data to check.
  133. * @param string $errorKey Contains key with error.
  134. * @return string|null Return error or null if OK!
  135. */
  136. private function checkFileArray($data, $errorKey = "")
  137. {
  138. $error = null;
  139. foreach ($data as $key => $value) {
  140. if (is_array($value)) {
  141. $error .= $this->checkFileArray($value, $errorKey . ($errorKey == "" ? "" : " -> ") . $key);
  142. } else {
  143. $this->checkString($value);
  144. $errorTmp = $this->checkCharsArrayErrors();
  145. if (!is_null($errorTmp)) {
  146. $errorTmp .= "\nError in $errorKey -> $key.";
  147. if ($this->debug) {
  148. $errorTmp .= "\n$value";
  149. }
  150. $errorTmp .= "\n################################################################################";
  151. }
  152. if (!is_null($errorTmp)) {
  153. if (is_null($error)) {
  154. $error = $errorTmp;
  155. } else {
  156. $error = "$error\n$errorTmp\n";
  157. }
  158. $this->resetCounters();
  159. }
  160. }
  161. }
  162. return $error;
  163. }
  164. //--------------
  165. //--- STRING ---
  166. //--------------
  167. /**
  168. * @param string $string Contains script bash.
  169. */
  170. private function checkString($string)
  171. {
  172. for ($i = 0, $size = (strlen($string) - 1); $i < $size;) {
  173. $pos = $this->checkCharsArray($string[$i], $string[$i + 1]);
  174. if ($pos == 0) {
  175. $i++;
  176. } else {
  177. $i = $i + $pos;
  178. }
  179. }
  180. if ($i <= $size) {
  181. $this->checkCharsArray($string[$size], "");
  182. }
  183. }
  184. /**
  185. * @param string $ch Contians char to check.
  186. * @param string $chplus Contains char +1.
  187. * @return int Return position to advance.
  188. */
  189. private function checkCharsArray($ch, $chplus)
  190. {
  191. $pos = 0;
  192. foreach ($this->chars as $cc) {
  193. $pos = $cc->check($ch, $chplus);
  194. if ($pos > 0) {
  195. return $pos;
  196. }
  197. }
  198. return $pos;
  199. }
  200. /**
  201. * @param string $string Contians string to check
  202. * @return null|string Return the $error value. If null it ok!!!
  203. */
  204. private function verifyString($string)
  205. {
  206. $error = null;
  207. $string = trim($string);
  208. if ($this->isJson($string)) {
  209. $string = str_replace("\\r\\n", "", $string);
  210. if ($this->isValidJson($string)) {
  211. return null;
  212. } else {
  213. $error = $this->errorJson();
  214. }
  215. } else {
  216. $this->checkString($string);
  217. $error = $this->checkCharsArrayErrors();
  218. }
  219. return $error;
  220. }
  221. //------------
  222. //--- JSON ---
  223. //------------
  224. /**
  225. * @return string Return json error.
  226. */
  227. private function errorJson()
  228. {
  229. switch (json_last_error()) {
  230. case JSON_ERROR_DEPTH:
  231. $error = 'The maximum stack depth has been exceeded.';
  232. break;
  233. case JSON_ERROR_STATE_MISMATCH:
  234. $error = 'Invalid or malformed JSON.';
  235. break;
  236. case JSON_ERROR_CTRL_CHAR:
  237. $error = 'Control character error, possibly incorrectly encoded.';
  238. break;
  239. case JSON_ERROR_SYNTAX:
  240. $error = 'Syntax error, malformed JSON.';
  241. break;
  242. // PHP >= 5.3.3
  243. case JSON_ERROR_UTF8:
  244. $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
  245. break;
  246. // PHP >= 5.5.0
  247. case JSON_ERROR_RECURSION:
  248. $error = 'One or more recursive references in the value to be encoded.';
  249. break;
  250. // PHP >= 5.5.0
  251. case JSON_ERROR_INF_OR_NAN:
  252. $error = 'One or more NAN or INF values in the value to be encoded.';
  253. break;
  254. case JSON_ERROR_UNSUPPORTED_TYPE:
  255. $error = 'A value of a type that cannot be encoded was given.';
  256. break;
  257. default:
  258. $error = 'Unknown JSON error occured.';
  259. break;
  260. }
  261. return "\n" . $error . "\n" . json_last_error_msg();
  262. }
  263. /**
  264. * @param string $string Json as string.
  265. * @return bool Return TRUE if is a valid json.
  266. */
  267. private function isJson($string)
  268. {
  269. if (substr($string, 0, 1) == "{" &&
  270. substr($string, strlen($string) - 1, strlen($string)) == "}") {
  271. for ($i = 1, $size = (strlen($string) - 1); $i < $size; $i++) {
  272. if (strlen(trim($string[$i])) != 0) {
  273. // es el primer caracter despues de la llave de inicio, entonces tiene que ser una comilla doble
  274. return $string[$i] == '"';
  275. }
  276. }
  277. } else {
  278. return false;
  279. }
  280. return false;
  281. }
  282. /**
  283. * @param string $string Json as string.
  284. * @return bool Return TRUE if is a valid json.
  285. */
  286. private function isValidJson($string)
  287. {
  288. json_decode($string);
  289. return (json_last_error() == JSON_ERROR_NONE);
  290. }
  291. }
  292. class CheckChars
  293. {
  294. /**
  295. * @var string Contains open char.
  296. */
  297. private $open;
  298. /**
  299. * @var string Contains close char.
  300. */
  301. private $close;
  302. /**
  303. * @var string Contains the number of match. If open match then +1 if close match then -1.
  304. */
  305. private $counter;
  306. /**
  307. * @var boolean Tell if open and close are equals.
  308. */
  309. private $equals;
  310. /**
  311. * OneChar constructor.
  312. * @param $open
  313. * @param $close
  314. */
  315. public function __construct($open, $close)
  316. {
  317. $this->open = $open;
  318. $this->close = $close;
  319. $this->equals = $open == $close;
  320. $this->counter = 0;
  321. }
  322. /**
  323. * @param string $ch Contians char to check.
  324. * @param string $chplus Contains char +1.
  325. * @return int Return position to advance.
  326. */
  327. public function check($ch, $chplus)
  328. {
  329. // primero chequeo si estoy en una dupla, si es asi, entonces no realizo el chequeo de simple
  330. $chplus = $ch . $chplus;
  331. $pos = 0;
  332. if ($chplus == $this->getOpen()) {
  333. $this->foundOpen();
  334. $pos = 2;
  335. } elseif ($chplus == $this->getClose()) {
  336. $this->foundClose();
  337. $pos = 2;
  338. } elseif ($ch == $this->getOpen()) {
  339. $this->foundOpen();
  340. $pos = 1;
  341. } elseif ($ch == $this->getClose()) {
  342. $this->foundClose();
  343. $pos = 1;
  344. }
  345. return $pos;
  346. }
  347. /**
  348. * @return mixed
  349. */
  350. public function getOpen()
  351. {
  352. return $this->open;
  353. }
  354. /**
  355. * @param mixed $open
  356. */
  357. public function setOpen($open)
  358. {
  359. $this->open = $open;
  360. }
  361. /**
  362. * @return mixed
  363. */
  364. public function getClose()
  365. {
  366. return $this->close;
  367. }
  368. /**
  369. * @param mixed $close
  370. */
  371. public function setClose($close)
  372. {
  373. $this->close = $close;
  374. }
  375. /**
  376. * Add 1 to counter
  377. */
  378. public function foundOpen()
  379. {
  380. $this->counter++;
  381. }
  382. /**
  383. * Sub 1 to counter
  384. */
  385. public function foundClose()
  386. {
  387. $this->counter--;
  388. }
  389. /**
  390. * Reset counter
  391. */
  392. public function resetCounter()
  393. {
  394. $this->counter = 0;
  395. }
  396. /**
  397. * @return string
  398. */
  399. public function getCounter()
  400. {
  401. if ($this->equals) {
  402. // si los valores de open y close con iguales y el counter es par, entonces es correcta la cantidad de llaves
  403. return $this->counter % 2 == 0 ? 0 : $this->counter;
  404. } else {
  405. return $this->counter;
  406. }
  407. }
  408. }