Commit: 7d4fa09001cfcc64ffc33e6b8488bbc0c723a6b9

Author: David Persson | Date: 2009-12-09 13:44:12 +0100
Adding 2 new rules, updating `MaxiumumLineLengthRule`.
diff --git a/config/phpca_lithium_standard.ini b/config/phpca_lithium_standard.ini index 84806e0..49a2c8a 100644 --- a/config/phpca_lithium_standard.ini +++ b/config/phpca_lithium_standard.ini @@ -3,7 +3,7 @@ line_endings = "\n" indentation = "\t" extensions = php -additional_rules= extensions/phpca/Rule +additional_rules = extensions/phpca/Rule ; Code [NoCarriageReturnsRule] @@ -29,6 +29,8 @@ additional_rules= extensions/phpca/Rule [NoEolAtEofRule] [MaximumLineLengthRule] line_length = 100 +[NoShortTypeNamesRule] +[TightCastRule] ; Documentation ; [ClassesMustHaveDocBlockRule] diff --git a/extensions/phpca/Rule/MaximumLineLengthRule.php b/extensions/phpca/Rule/MaximumLineLengthRule.php index 0dcae67..556c51d 100644 --- a/extensions/phpca/Rule/MaximumLineLengthRule.php +++ b/extensions/phpca/Rule/MaximumLineLengthRule.php @@ -22,8 +22,10 @@ class MaximumLineLengthRule extends Rule ); foreach ($lines as $i => $line) { if (strlen($line) > $this->settings['line_length']) { - $token = new Token(T_ANY, $line, $i + 1, $this->settings['line_length'] + 1); - $this->addViolation('Maximum line length exceeded', $token); + $this->addViolation( + 'Maximum line length exceeded', + null, $i +1, $this->settings['line_length'] + 1 + ); } } } diff --git a/extensions/phpca/Rule/NoShortTypeNamesRule.php b/extensions/phpca/Rule/NoShortTypeNamesRule.php new file mode 100644 index 0000000..e448e69 --- /dev/null +++ b/extensions/phpca/Rule/NoShortTypeNamesRule.php @@ -0,0 +1,52 @@ +<?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 ($token->getText() !== $text) { + $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/TightCastRule.php b/extensions/phpca/Rule/TightCastRule.php new file mode 100644 index 0000000..65028b3 --- /dev/null +++ b/extensions/phpca/Rule/TightCastRule.php @@ -0,0 +1,39 @@ +<?php + +namespace spriebsch\PHPca\Rule; + +/** + * Ensures that there are no whitespaces after or within a cast. + */ +class TightCastRule extends Rule +{ + /** + * Performs the rule check. + * + * @returns null + */ + protected function doCheck() + { + $casts = array( + T_BOOL_CAST, T_INT_CAST, T_ARRAY_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(); + $token = $this->file->current(); + + if ($token->getId() === T_WHITESPACE) { + $this->addViolation('Whitespace after cast', $token); + } + } + } + } +} +?> \ No newline at end of file