/** * @param \Helmich\TypoScriptParser\Tokenizer\TokenInterface[] $tokens * @param \Helmich\TypoScriptLint\Linter\Report\File $file * @param \Helmich\TypoScriptLint\Linter\LinterConfiguration $configuration * @return void */ public function sniff(array $tokens, File $file, LinterConfiguration $configuration) { $count = count($tokens); for ($i = 0; $i < $count; $i++) { if ($tokens[$i]->getType() === TokenInterface::TYPE_OBJECT_IDENTIFIER) { if (isset($tokens[$i + 1])) { if ($tokens[$i + 1]->getType() !== TokenInterface::TYPE_WHITESPACE) { $warning = new Warning($tokens[$i]->getLine(), NULL, 'No whitespace after object accessor.', Warning::SEVERITY_WARNING, __CLASS__); $file->addWarning($warning); } else { if (trim($tokens[$i + 1]->getValue(), "\n") !== ' ') { $warning = new Warning($tokens[$i]->getLine(), NULL, 'Operator should be followed by single space.', Warning::SEVERITY_WARNING, __CLASS__); $file->addWarning($warning); } } } } } }
/** * @param \Helmich\TypoScriptParser\Tokenizer\TokenInterface[] $tokens * @param \Helmich\TypoScriptLint\Linter\Report\File $file * @param \Helmich\TypoScriptLint\Linter\LinterConfiguration $configuration * @return mixed */ public function sniff(array $tokens, File $file, LinterConfiguration $configuration) { $indentCharacter = $this->useSpaces ? ' ' : "\t"; $tokensByLine = new LineGrouper($tokens); $indentationLevel = 0; /** @var \Helmich\TypoScriptParser\Tokenizer\TokenInterface[] $tokensInLine */ foreach ($tokensByLine->getLines() as $line => $tokensInLine) { foreach ($tokensInLine as $key => $token) { if ($token->getType() === TokenInterface::TYPE_BRACE_CLOSE) { $indentationLevel--; } if ($token->getType() === TokenInterface::TYPE_RIGHTVALUE_MULTILINE) { unset($tokensInLine[$key]); $tokensInLine = array_values($tokensInLine); } } $firstToken = count($tokensInLine) > 0 ? $tokensInLine[0] : NULL; // Skip empty lines. if (count($tokensInLine) == 1 && $firstToken->getType() === TokenInterface::TYPE_WHITESPACE && $firstToken->getValue() === "\n") { continue; } if ($indentationLevel === 0) { if ($tokensInLine[0]->getType() === TokenInterface::TYPE_WHITESPACE && strlen($tokensInLine[0]->getValue())) { $file->addWarning($this->createWarning($line, $indentationLevel, $tokensInLine[0]->getValue())); } } else { if ($tokensInLine[0]->getType() !== TokenInterface::TYPE_WHITESPACE) { $file->addWarning($this->createWarning($line, $indentationLevel, '')); } else { $expectedIndentationCharacterCount = $this->indentPerLevel * $indentationLevel; $expectedIndentation = str_repeat($indentCharacter, $expectedIndentationCharacterCount); if ($tokensInLine[0]->getValue() !== $expectedIndentation) { $file->addWarning($this->createWarning($line, $indentationLevel, $tokensInLine[0]->getValue())); } } } foreach ($tokensInLine as $token) { if ($token->getType() === TokenInterface::TYPE_BRACE_OPEN) { $indentationLevel++; } } } }
/** * @param \Helmich\TypoScriptParser\Parser\AST\Statement[] $statements * @param \Helmich\TypoScriptLint\Linter\Report\File $file * @param \Helmich\TypoScriptLint\Linter\LinterConfiguration $configuration * @return void */ public function sniff(array $statements, File $file, LinterConfiguration $configuration) { $visitor = new NestingConsistencyVisitor(); $traverser = new Traverser($statements); $traverser->addVisitor($visitor); $traverser->walk(); foreach ($visitor->getWarnings() as $warning) { $file->addWarning($warning); } }
/** * @medium */ public function testPlaintextReportIsCorrectlyGenerated() { $file1 = new File('foobar.tys'); $file1->addWarning(new Warning(123, 12, 'Message #1', Warning::SEVERITY_INFO, 'foobar')); $file1->addWarning(new Warning(124, NULL, 'Message #2', Warning::SEVERITY_WARNING, 'foobar')); $file2 = new File('bar.txt'); $file2->addWarning(new Warning(412, 141, 'Message #3', Warning::SEVERITY_ERROR, 'barbaz')); $report = new Report(); $report->addFile($file1); $report->addFile($file2); $this->printer->writeReport($report); $this->assertEquals(self::EXPECTED_XML_DOCUMENT, $this->output->fetch()); }
/** * @medium */ public function testXmlReportIsCorrectlyGenerated() { $file1 = new File('foobar.tys'); $file1->addWarning(new Warning(123, 12, 'Message #1', Warning::SEVERITY_INFO, 'foobar')); $file1->addWarning(new Warning(124, NULL, 'Message #2', Warning::SEVERITY_WARNING, 'foobar')); $file2 = new File('bar.txt'); $file2->addWarning(new Warning(412, 141, 'Message #3', Warning::SEVERITY_ERROR, 'barbaz')); $report = new Report(); $report->addFile($file1); $report->addFile($file2); $this->output->expects($this->once())->method('write')->with(self::EXPECTED_XML_DOCUMENT); $this->printer->writeReport($report); }
/** * @param \Helmich\TypoScriptParser\Tokenizer\TokenInterface[] $tokens * @param \Helmich\TypoScriptLint\Linter\Report\File $file * @param \Helmich\TypoScriptLint\Linter\LinterConfiguration $configuration * @return mixed */ public function sniff(array $tokens, File $file, LinterConfiguration $configuration) { foreach ($tokens as $token) { if (!($token->getType() === TokenInterface::TYPE_COMMENT_ONELINE || $token->getType() === TokenInterface::TYPE_COMMENT_MULTILINE)) { continue; } $commentContent = preg_replace(',^\\s*(#|/\\*|/)\\s*,', '', $token->getValue()); if (preg_match(Tokenizer::TOKEN_OPERATOR_LINE, $commentContent, $matches)) { $warning = new Warning($token->getLine(), 0, 'Found commented code (' . $matches[0] . ').', Warning::SEVERITY_INFO, __CLASS__); $file->addWarning($warning); } } }
/** * @param \Helmich\TypoScriptParser\Tokenizer\TokenInterface[] $tokens * @param \Helmich\TypoScriptLint\Linter\Report\File $file * @param \Helmich\TypoScriptLint\Linter\LinterConfiguration $configuration * @return void */ public function sniff(array $tokens, File $file, LinterConfiguration $configuration) { foreach ($tokens as $token) { if ($token->getType() !== TokenInterface::TYPE_RIGHTVALUE || strlen($token->getValue()) < 8) { continue; } if (preg_match(self::CONSTANT_EXPRESSION, $token->getValue())) { continue; } if (!array_key_exists($token->getValue(), $this->knownRightValues)) { $this->knownRightValues[$token->getValue()] = 0; } $this->knownRightValues[$token->getValue()]++; if ($this->knownRightValues[$token->getValue()] > 1) { $warning = new Warning($token->getLine(), NULL, 'Duplicated value "' . $token->getValue() . '". Consider extracting it into a constant.', Warning::SEVERITY_WARNING, __CLASS__); $file->addWarning($warning); } } }