/** * Validate content * * @param string $content * @param string $file * @return bool */ public function validate($content, $file) { $ext = pathinfo($file, PATHINFO_EXTENSION); if (!in_array($ext, array('js', 'php', 'phtml'))) { return true; } $content = preg_replace('/\\x0A\\x0D|\\x0D\\x0A|\\x0A|\\x0D/', "\n", $content); $originalArr = preg_split('/\\x0A\\x0D|\\x0D\\x0A|\\x0A|\\x0D/', $content); $parsedArr = CodingStandard::splitContent($content); foreach ($parsedArr as $line => $str) { if (!$str) { //skip empty line continue; } $currentString = trim($originalArr[$line - 1]); //find console.log() if ($this->canCheckType($ext, self::JS_CONSOLE) && false !== strpos($str, 'console.log(')) { $this->addError($file, self::JS_CONSOLE, $currentString, $line); } //find is_null() function if ($this->canCheckType($ext, self::CODE_IS_NULL) && false !== strpos($str, 'is_null(')) { $this->addError($file, self::CODE_IS_NULL, $currentString, $line); } //find qqq() if ($this->canCheckType($ext, self::DEBUG_QQQ) && false !== strpos($str, 'qqq')) { $this->addError($file, self::DEBUG_QQQ, $currentString, $line); } //find var_dump() if ($this->canCheckType($ext, self::DEBUG_VAR_DUMP) && false !== strpos($str, 'var_dump(')) { $this->addError($file, self::DEBUG_VAR_DUMP, $currentString, $line); } } return !$this->errorCollector->hasErrors(); }
/** * Test split complex content */ public function testSplitContentComplex() { $this->markTestIncomplete('Seem it should be simplified.'); $content = file_get_contents(__DIR__ . '/../_fixture/TestClassSplit.php'); $options['errorCollector'] = new ErrorCollector(); $validator = new CodingStandard($options); $parsed = $validator->splitContent($content); $expected = ['<?php', '', 'class Some_testClass2', '{', ' ', ' protected function _render(array $data = array(), $template = null, $blockClass = null)', ' {', ' ', ' if ($blockClass) {', ' $blockClass = \'\' . \'\' . $blockClass;', ' $block = new $blockClass($data);', ' } else {', ' $block = new Renderer($data);', ' }', '', ' if (!$template) {', ' ', ' $template = $this->getControllerName() . DIRECTORY_SEPARATOR', ' . $this->getActionName() . \'\';', ' }', ' $block->setTemplate($template);', '', ' ', ' $actionHtml = $block->toHtml();', '', ' if (!$this->isAjax()) {', ' $block = new Renderer(array(\'\' => $actionHtml));', ' $block->setTemplate(\'\');', '', ' ', ' $message = new Message();', ' $message->setTemplate(\'\');', ' $block->setChild(\'\', $message);', '', ' echo $block->toHtml();', ' } else {', ' echo $actionHtml;', ' }', ' return $this;', ' }', '}', '']; $this->assertEquals($expected, array_values($parsed)); }