Commit: 7a3acb1f9b097faab5e1971cf6dfdcf589210f6e

Author: David Persson | Date: 2010-02-03 17:24:17 +0100
Moving rules and standard into http://github.com/UnionOfRAD/phpca.
diff --git a/config/phpca_lithium_standard.ini b/config/phpca_lithium_standard.ini deleted file mode 100644 index a049d4a..0000000 --- a/config/phpca_lithium_standard.ini +++ /dev/null @@ -1,43 +0,0 @@ -[PHPca] - -line_endings = "\n" -indentation = "\t" -extensions = php -additional_rules = extensions/phpca/Rule -skip = /*.txt.php, /*.html.php, /*.xml.php - -; Code -[NoCarriageReturnsRule] -[NoTrailingWhitespaceRule] -[OpenTagAtBeginningRule] -[CloseTagAtEndRule] -[EmptyLineBeforeCloseTagRule] -[NoEmptyLineBeforeClassCloseRule] -[IncludeAndRequireWithoutBracketsRule] -[KeywordsAreLowercaseRule] -[NoVarKeywordsRule] -[NoEvalStatementsRule] -[NoGlobalStatementsRule] -[NoGotoStatementsRule] -; [NoEchoStatementsRule] -[NoPrintStatementsRule] -[NoVarDumpStatementsRule] -; [NoInlineHtmlRule] -[NoAlternativeSyntaxStatementsRule] -[NoShutupOperatorsRule] -; [MethodsMustHaveVisibilityOperatorRule] -[OneClassPerFileRule] -[NoEolAtEofRule] -[MaximumLineLengthRule] -line_length = 100 -[NoShortTypeNamesRule] -[NoWhitespaceWithinCastsRule] -[OneSpaceAfterCastsRule] -[OperatorsSurroundedBySpacesRule] -[DocTagsOrderRule] -[ConstantsUppercaseRule] - -; Documentation -; [ClassesMustHaveDocBlockRule] -; [FunctionsMustHaveDocBlockRule] -[FunctionParametersMustBeDocumentedRule] diff --git a/extensions/command/Syntax.php b/extensions/command/Syntax.php index ce70da1..d352e81 100644 --- a/extensions/command/Syntax.php +++ b/extensions/command/Syntax.php @@ -65,7 +65,7 @@ class Syntax extends \lithium\console\Command implements \spriebsch\PHPca\Progre $app = new Application(getcwd()); $app->registerProgressPrinter($this); - $file = LITHIUM_APP_PATH . '/config/phpca_lithium_standard.ini'; + $file = LITHIUM_APP_PATH . '/libraries/phpca/src/Standard/lithium.ini'; $config = new Configuration(getcwd()); $config->setStandard(parse_ini_file($file, true)); diff --git a/extensions/phpca/Rule/ConstantsUppercaseRule.php b/extensions/phpca/Rule/ConstantsUppercaseRule.php deleted file mode 100644 index a0b2e21..0000000 --- a/extensions/phpca/Rule/ConstantsUppercaseRule.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -use spriebsch\PHPca\Token; - -/** - * Ensures that constants are upper case. - */ -class ConstantsUppercaseRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - public function doCheck() - { - $source = $this->file->getSourceCode(); - - $namedConstants = array(); - $definePattern = '/' . $this->configuration->getLineEndings() . '.*?define\("(.*?)".*[^'; - $definePattern .= $this->configuration->getLineEndings() . ']/'; - - preg_match_all($definePattern, $source, $namedConstants); - $declarations = array_shift($namedConstants); - $namedConstants = array_shift($namedConstants); - - foreach($namedConstants as $i => $constant) { - if ($constant != strtoupper($constant)) { - $lines = array(); - $line = preg_match_all( - '/.*?' . $this->configuration->getLineEndings() . '.*?/', - substr($source, 0, strpos($source, 'define("' . $constant)), - $lines - ); - - $this->addViolation( - "Named Constant `{$constant}` not upper case", - null, - $line + 1, - strpos($declarations[$i], $constant) - ); - } - } - } -} - -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/DocTagsOrderRule.php b/extensions/phpca/Rule/DocTagsOrderRule.php deleted file mode 100644 index d6a31a0..0000000 --- a/extensions/phpca/Rule/DocTagsOrderRule.php +++ /dev/null @@ -1,68 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -use spriebsch\PHPca\Token; - -/** - * Ensures documentation tags have a specific order. - */ -class DocTagsOrderRule extends Rule -{ - - /** - * List of possible tags, ordered. - * - * All tags will be referenced against this list. If they - * appear out of order, a violation will be raised. Simple - * order check regardless of missing tags. - */ - protected $tagsOrdered = array( - "@link", - "@see", - "@params", - "@return" - ); - - /** - * Performs the rule check. - * - * @return null - */ - protected function doCheck() - { - while ($this->file->seekTokenId(T_DOC_COMMENT)) { - $token = $this->file->current(); - $docText = $token->getText(); - - // Grab ordered array of the tags in this token - $docTags = array(); - preg_match_all('/@.*?\s/', $docText, $docTags); - $docTags = array_shift($docTags); - - $docIndex = 0; - $lastTag = ""; - foreach ($docTags as $tag) { - $tag = trim($tag); - $tagIndex = array_search($tag, $this->tagsOrdered); - - if ($tagIndex !== false) { - if ($tagIndex < $docIndex) { - $this->addViolation( - "Doc tag `{$tag}` not ordered correctly, came after `{$lastTag}`", - $token - ); - continue; - } - - $docIndex = $tagIndex; - $lastTag = $tag; - } - - } - $this->file->next(); - } - } -} - -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/EmptyLineBeforeCloseTagRule.php b/extensions/phpca/Rule/EmptyLineBeforeCloseTagRule.php deleted file mode 100644 index 7ad13e7..0000000 --- a/extensions/phpca/Rule/EmptyLineBeforeCloseTagRule.php +++ /dev/null @@ -1,40 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures that the closing tag is separated by an empty line. - */ -class EmptyLineBeforeCloseTagRule extends Rule -{ - /** - * Performs the rule check. - * - * We need to check 2 tokens previous to the closing tag otherwise a comment followed by - * and empty line followed by the closing tag is not properly recognized. - * - * @returns null - */ - protected function doCheck() - { - $this->file->seekTokenId(T_CLOSE_TAG); - - $this->file->prev(); - $b = $this->file->current(); - - $this->file->prev(); - $a = $this->file->current(); - - if (!$a || !$b) { - return; - } - - $string = $a->getText() . $b->getText(); - $lineEndings = $this->configuration->getLineEndings(); - - if (!preg_match("/{$lineEndings}{$lineEndings}$/", $string)) { - $this->addViolation('File has no empty line before PHP close tag', $b); - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/FunctionParametersMustBeDocumentedRule.php b/extensions/phpca/Rule/FunctionParametersMustBeDocumentedRule.php deleted file mode 100644 index 87a62d1..0000000 --- a/extensions/phpca/Rule/FunctionParametersMustBeDocumentedRule.php +++ /dev/null @@ -1,67 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensure that each parameter of a function/method is documented. - */ -class FunctionParametersMustBeDocumentedRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - while ($this->file->seekTokenId(T_FUNCTION)) { - $function = $this->file->current(); - $signature = array(); - - // Skip anonymous functions - $this->file->next(); - - if ($this->file->current()->getId() != T_WHITESPACE) { - continue; - } - - while ($this->file->current()->getId() != T_OPEN_CURLY) { - $this->file->next(); - - if (!$this->file->valid()) { - break; - } - $token = $this->file->current(); - - - if ($token->getId() == T_VARIABLE) { - $signature[] = $token; - } - } - - if (!$this->file->seekTokenId(T_DOC_COMMENT, true)) { - $this->file->seekToken($function); - $this->file->next(); - continue; - } - $comment = $this->file->current(); - - if (($comment->getEndLine() + 1) != $function->getLine()) { - $this->file->seekToken($function); - $this->file->next(); - continue; - } - $regex = '/@param\s+(?P<types>[\w\|]*)?\s?(?P<names>\$\w*)?/'; - preg_match_all($regex, $comment->getText(), $matches); - - foreach ($signature as $i => $param) { - if (!isset($matches['names'][$i]) || $param->getText() != $matches['names'][$i]) { - $this->addViolation('Parameter is not documented', $param); - } - } - $this->file->seekToken($function); - $this->file->next(); - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/MaximumLineLengthRule.php b/extensions/phpca/Rule/MaximumLineLengthRule.php deleted file mode 100644 index 556c51d..0000000 --- a/extensions/phpca/Rule/MaximumLineLengthRule.php +++ /dev/null @@ -1,33 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -use spriebsch\PHPca\Token; - -/** - * Ensures that no line is longer than a certain amount of charcters. - */ -class MaximumLineLengthRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $lines = explode( - stripcslashes($this->configuration->getLineEndings()), - $this->file->getSourceCode() - ); - foreach ($lines as $i => $line) { - if (strlen($line) > $this->settings['line_length']) { - $this->addViolation( - 'Maximum line length exceeded', - null, $i +1, $this->settings['line_length'] + 1 - ); - } - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/NoEmptyLineBeforeClassCloseRule.php b/extensions/phpca/Rule/NoEmptyLineBeforeClassCloseRule.php deleted file mode 100644 index 594f22c..0000000 --- a/extensions/phpca/Rule/NoEmptyLineBeforeClassCloseRule.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures that there are no empty lines before the closing curly brace of a class. - */ -class NoEmptyLineBeforeClassCloseRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - while ($this->file->seekTokenId(T_CLASS)) { - $this->file->seekTokenId(T_OPEN_CURLY); - $rewind = $this->file->current(); - $this->file->seekMatchingCurlyBrace($rewind); - $this->file->prev(); - - $token = $this->file->current(); - $lineEndings = $this->configuration->getLineEndings(); - - if (addcslashes($token->getText(), "\0..\37") == $lineEndings . $lineEndings) { - $this->addViolation('Empty line before class closing curly brace', $token); - } - - $this->file->seekToken($rewind); - $this->file->next(); - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/NoEolAtEofRule.php b/extensions/phpca/Rule/NoEolAtEofRule.php deleted file mode 100644 index b8ac56c..0000000 --- a/extensions/phpca/Rule/NoEolAtEofRule.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -use spriebsch\PHPca\Token; - -/** - * Ensures there is no EOL marker at the EOF. - */ -class NoEolAtEofRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $content = $this->file->getSourceCode(); - $eol = stripcslashes($this->configuration->getLineEndings()); - $eof = substr($content, - strlen($eol)); - - if ($eof == $eol) { - $line = substr_count($content, $eol); - $token = new Token(T_WHITESPACE, $eof, $line - 1); - $this->addViolation('EOL at EOF', $token); - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/NoShortTypeNamesRule.php b/extensions/phpca/Rule/NoShortTypeNamesRule.php deleted file mode 100644 index fc37261..0000000 --- a/extensions/phpca/Rule/NoShortTypeNamesRule.php +++ /dev/null @@ -1,51 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures that no short type names are used for casting and documenting. - */ -class NoShortTypeNamesRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $enforce = array( - T_BOOL_CAST => 'boolean', - T_INT_CAST => 'integer' - ); - foreach ($enforce as $id => $text) { - while ($this->file->seekTokenId($id)) { - $token = $this->file->current(); - if (strpos($token->getText(), $text) === false) { - $this->addViolation('Short type name', $token); - } - $this->file->next(); - } - } - - $blacklist = array( - 'int', - 'bool' - ); - while ($this->file->seekTokenId(T_DOC_COMMENT)) { - $token = $this->file->current(); - $lines = explode( - stripcslashes($this->configuration->getLineEndings()), - $token->getText() - ); - foreach ($lines as $i => $line) { - if (preg_match('/\b(int|bool)\b/', $line)) { - $this->addViolation('Short type name in docblock', $token, $token->getLine() + $i); - } - } - - $this->file->next(); - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/NoWhitespaceWithinCastsRule.php b/extensions/phpca/Rule/NoWhitespaceWithinCastsRule.php deleted file mode 100644 index e252671..0000000 --- a/extensions/phpca/Rule/NoWhitespaceWithinCastsRule.php +++ /dev/null @@ -1,33 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures that there are no whitespaces within a cast. - */ -class NoWhitespaceWithinCastsRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $casts = array( - T_BOOL_CAST, T_INT_CAST, T_ARRAY_CAST, T_STRING_CAST, - T_DOUBLE_CAST, T_OBJECT_CAST, T_UNSET_CAST - ); - foreach ($casts as $id) { - while ($this->file->seekTokenId($id)) { - $token = $this->file->current(); - - if ($token->hasWhitespace()) { - $this->addViolation('Whitespace whithin cast', $token); - } - $this->file->next(); - } - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/OneClassPerFileRule.php b/extensions/phpca/Rule/OneClassPerFileRule.php deleted file mode 100644 index bd6ca74..0000000 --- a/extensions/phpca/Rule/OneClassPerFileRule.php +++ /dev/null @@ -1,32 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures there is only one class declared per file. - */ -class OneClassPerFileRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $count = 0; - - while ($this->file->seekTokenId(T_CLASS)) { - $count++; - $token = $this->file->current(); - - if ($count > 1) { - $this->addViolation('More than one class declared in file', $token); - } - - $this->file->seekToken($token); - $this->file->next(); - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/OneSpaceAfterCastsRule.php b/extensions/phpca/Rule/OneSpaceAfterCastsRule.php deleted file mode 100644 index 91d4ba8..0000000 --- a/extensions/phpca/Rule/OneSpaceAfterCastsRule.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures that there is one space after a cast. - */ -class OneSpaceAfterCastsRule extends Rule -{ - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $casts = array( - T_BOOL_CAST, T_INT_CAST, T_ARRAY_CAST, T_STRING_CAST, - T_DOUBLE_CAST, T_OBJECT_CAST, T_UNSET_CAST - ); - foreach ($casts as $id) { - while ($this->file->seekTokenId($id)) { - $this->file->next(); - $token = $this->file->current(); - - if ($token->getId() !== T_WHITESPACE) { - $this->addViolation('No space after cast', $token); - } elseif (strlen($token->getText()) > 1) { - $this->addViolation('More than one space after cast', $token); - } - } - } - } -} -?> \ No newline at end of file diff --git a/extensions/phpca/Rule/OperatorsSurroundedBySpacesRule.php b/extensions/phpca/Rule/OperatorsSurroundedBySpacesRule.php deleted file mode 100644 index 905b459..0000000 --- a/extensions/phpca/Rule/OperatorsSurroundedBySpacesRule.php +++ /dev/null @@ -1,166 +0,0 @@ -<?php - -namespace spriebsch\PHPca\Rule; - -/** - * Ensures that there is one space before and after an operator. - * - * However there are a few exceptions to the spacing rule◌: - * 1. Increment and decrement operators must be directly followed by or following the variable. - * 2. The exclamation mark must be directly followed by the variable. - * 3. Colons appearing as part of a case condition must have no spaces surrounding them. - * 4. Labels must have no spaces surrounding them. - * 5. Negative literal integers or floats must have the minus sign directly attached. - * 6. Minus signs involved in negations of i.e. variables can be spaced or directly attached. - */ -class OperatorsSurroundedBySpacesRule extends Rule -{ - protected $composed = array( - T_AND_EQUAL, T_BOOLEAN_AND, T_BOOLEAN_OR, T_CONCAT_EQUAL, T_DIV_EQUAL, T_IS_EQUAL, - T_IS_GREATER_OR_EQUAL, T_IS_IDENTICAL, T_IS_NOT_EQUAL, T_IS_NOT_IDENTICAL, - T_IS_SMALLER_OR_EQUAL, T_LOGICAL_AND, T_LOGICAL_XOR, T_MINUS_EQUAL, T_MOD_EQUAL, - T_MUL_EQUAL, T_OR_EQUAL, T_PLUS_EQUAL, T_SL_EQUAL, T_SR_EQUAL, - T_XOR_EQUAL, T_INC, T_DEC - ); - - protected $single = array( - T_SL, T_SR, - T_DOT, T_EQUAL, T_LT, T_GT, T_PLUS, T_MINUS, T_MULT, T_DIV, T_PERCENT, T_PIPE, - T_CARET, T_TILDE, T_AMPERSAND, T_QUESTIONMARK, T_COLON, T_EXCLAMATIONMARK - ); - - /** - * Performs the rule check. - * - * @returns null - */ - protected function doCheck() - { - $operators = array_merge($this->single, $this->composed); - $before = $current = $after = null; - - while ($this->file->valid()) { - $this->file->next(); - - $before = $current; - $current = $after; - $after = $this->file->current(); - - if (!$before || !$current || !$after) { - continue; - } - - if ($this->isException($before, $current, $after)) { - $this->file->next(); - $this->file->next(); - $before = $current = $after = null; - continue; - } - - if (!in_array($current->getId(), $operators)) { - continue; - } - if ($before && $before->getId() !== T_WHITESPACE) { - $this->addViolation('No space before operator', $before); - } elseif ($after && $after->getId() !== T_WHITESPACE) { - $this->addViolation('No space after operator', $after); - } - } - } - - protected function isException($before, $current, $after) { - // Exclamation mark - $center = array(T_EXCLAMATIONMARK); - - if (in_array($current->getId(), $center)) { - return true; - } - - // Increment and decrement - $center = array(T_MINUS, T_PLUS); - $right = array(T_MINUS, T_PLUS); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - return true; - } - - $center = array(T_DEC, T_INC); - - if (in_array($current->getId(), $center)) { - return true; - } - - // Single & composed - equal - $center = array( - T_SL, T_SR, - T_DOT, T_EQUAL, T_LT, T_GT, T_PLUS, T_MINUS, T_MULT, T_DIV, T_PERCENT, T_PIPE, - T_CARET, T_TILDE, - - ); - $right = array(T_EQUAL); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - return true; - } - - // The ampersand problem - $center = array(T_AMPERSAND); - $right = array(T_VARIABLE, T_EQUAL, T_STRING); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - return true; - } - - $center = array(T_EQUAL); - $right = array(T_AMPERSAND); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - return true; - } - - // Minus and plus literal number, variable but not within operations - $center = array(T_MINUS, T_PLUS); - $right = array(T_DNUMBER, T_LNUMBER, T_VARIABLE); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - $this->file->prev(); - $this->file->prev(); - $beforebefore = $this->file->current(); - $this->file->next(); - $this->file->next(); - - $bad = array(T_DNUMBER, T_LNUMBER, T_VARIABLE, T_ARRAY); - - if (!in_array($before->getId(), $bad) && !in_array($beforebefore->getId(), $bad)) { - return true; - } - return false; - } - - // Questionmark and colon, ternary operator - $center = array(T_QUESTIONMARK); - $right = array(T_COLON); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - return true; - } - - // Case - $left = array(T_WHITESPACE); - $center = array(T_COLON); - - if (in_array($before->getId(), $left) && in_array($current->getId(), $center)) { - return true; - } - - // Label - $center = array(T_COLON); - $right = array(T_WHITESPACE); - - if (in_array($current->getId(), $center) && in_array($after->getId(), $right)) { - return true; - } - return false; - } -} -?> \ No newline at end of file