/** * visitToken(): defined by TokenRuleInterface. * * @see TokenRuleInterface::visitToken() * @param File $file * @return void */ public function visitToken(File $file) { $previousToken = null; $currentToken = $file->current(); $file->next(); $secondStatement = false; while ($file->valid()) { $token = $file->current(); $tokenType = $token->getType(); if (in_array($tokenType, array(T_COMMENT, T_DOC_COMMENT))) { $lexeme = $token->getLexeme(); if (strpos($lexeme, '//') === 0 || strpos($lexeme, '#') === 0) { // Single line comments end the line break; } elseif ($token->getNewlineCount() > 0) { // So do block comments with new lines break; } } elseif ($tokenType === T_WHITESPACE) { if ($token->getNewlineCount() > 0) { // Whitespace new lines are fine as well break; } } else { $secondStatement = true; break; } $file->next(); } if ($previousToken !== null) { if ($currentToken->getLine() === $previousToken->getLine()) { $this->addViolation($file, $currentToken->getLine(), $currentToken->getColumn(), 'Found multiple statements on same line'); } } }
/** * visitToken(): defined by TokenRuleInterface. * * @see TokenRuleInterface::visitToken() * @param File $file * @return void */ public function visitToken(File $file) { $token = $file->current(); if (false !== strpos($token->getLexeme(), "\t")) { $this->addViolation($file, $token->getLine(), $token->getColumn(), 'Tabulator found'); } }
/** * visitToken(): defined by TokenRuleInterface. * * @see TokenRuleInterface::visitToken() * @param File $file * @return void */ public function visitToken(File $file) { if (!$file->seekTokenType(T_STRING, false, ';')) { return; } $token = $file->current(); if (!preg_match('(^' . $this->format . '$)', $token->getLexeme())) { $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('Constant name does not match format "%s"', $this->format)); } }
/** * visitToken(): defined by TokenRuleInterface. * * @see TokenRuleInterface::visitToken() * @param File $file * @return void */ public function visitToken(File $file) { if (!$file->seekTokenType(T_STRING, false, '(')) { return; } $token = $file->current(); if (!in_array($token->getLexeme(), self::$magicMethods) && !preg_match('(^' . $this->format . '$)', $token->getLexeme())) { $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('Method name does not match format "%s"', $this->format)); } }
/** * check(): defined by Rule interface. * * @see Rule::check() * @param File $file * @return void */ public function check(File $file) { $indentation = str_repeat($this->indentStyle === 'space' ? ' ' : "\t", $this->indentCount); $file->rewind(); while (true) { $token = $file->current(); $level = $token->getLevel(); $file->next(); if ($file->current()->getType() === '}' || $file->current()->getType() === ')') { $level--; } $file->prev(); $expectedIndentation = str_repeat($indentation, $level); $actualIndentation = $token->getTrailingWhitespace(); if ($expectedIndentation !== $actualIndentation) { $this->addViolation($file, $token, $column, $message); } if (!$file->seekNextLine()) { return; } } }
/** * visitToken(): defined by TokenRuleInterface. * * @see TokenRuleInterface::visitToken() * @param File $file * @return void */ public function visitToken(File $file) { if (!$file->seekTokenType(T_STRING, false, '{')) { return; } $token = $file->current(); $psr0Compliant = true; if ($token->getNamespace() !== null) { $fqcn = $token->getNamespace() . '\\' . $token->getLexeme(); $path = str_replace('\\', '/', $token->getNamespace()) . '/' . str_replace('_', '/', $token->getLexeme()); } else { $fqcn = $token->getLexeme(); $path = str_replace('_', '/', $token->getLexeme()); if ($this->requireVendorNamespace) { $psr0Compliant = false; } } if ($psr0Compliant) { $expectedPathParts = array_diff(explode('/', $path), array('')); $expectedFilename = array_pop($expectedPathParts) . '.php'; $pathParts = explode('/', str_replace('\\', '/', realpath($file->getFilename()))); $filename = array_pop($pathParts); if ($filename !== $expectedFilename) { // Class name should match filename. $psr0Compliant = false; } elseif (count($expectedPathParts) === 0) { // Vendor level namespace required. $psr0Compliant = false; } else { // Path should match namespace structure. $pathParts = array_slice($pathParts, -count($expectedPathParts)); if ($pathParts !== $expectedPathParts) { $psr0Compliant = false; } } } if (!$psr0Compliant) { $this->addViolation($file, $token->getLine(), $token->getColumn(), sprintf('Class name "%s" is not PSR0 compliant', $fqcn)); } }