Permalink
Browse files

Added Generic LowerCaseKeywordSniff to ensure all PHP keywords are de…

…fined in lowercase
  • Loading branch information...
1 parent eb4b299 commit 5f876fa1839b7ac32479ba650f8d262217fd1fd3 @gsherwood gsherwood committed Mar 20, 2013
@@ -0,0 +1,136 @@
+<?php
+/**
+ * Generic_Sniffs_PHP_LowerCaseKeywordSniff.
+ *
+ * PHP version 5
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ */
+
+/**
+ * Generic_Sniffs_PHP_LowerCaseKeywordSniff.
+ *
+ * Checks that all PHP keywords are lowercase.
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ */
+class Generic_Sniffs_PHP_LowerCaseKeywordSniff implements PHP_CodeSniffer_Sniff
+{
+
+
+ /**
+ * Returns an array of tokens this test wants to listen for.
+ *
+ * @return array
+ */
+ public function register()
+ {
+ return array(
+ T_HALT_COMPILER,
+ T_ABSTRACT,
+ T_ARRAY,
+ T_AS,
+ T_BREAK,
+ T_CALLABLE,
+ T_CASE,
+ T_CATCH,
+ T_CLASS,
+ T_CLONE,
+ T_CONST,
+ T_CONTINUE,
+ T_DECLARE,
+ T_DEFAULT,
+ T_DO,
+ T_ECHO,
+ T_ELSE,
+ T_ELSEIF,
+ T_EMPTY,
+ T_ENDDECLARE,
+ T_ENDFOR,
+ T_ENDFOREACH,
+ T_ENDIF,
+ T_ENDSWITCH,
+ T_ENDWHILE,
+ T_EVAL,
+ T_EXIT,
+ T_EXTENDS,
+ T_FINAL,
@robocoder

robocoder Mar 20, 2013

Contributor

Should be T_FINALLY.

@gsherwood

gsherwood Mar 20, 2013

Owner

No, this should be T_FINAL, which is the "final" keyword, added in PHP5.0. "finally" is not listed as a PHP keyword yet. But this may be a documentation mistake because it also doesn't have any documentation about the T_FINALLY token, which I have already confirmed is in PHP 5.5 and added support for it to PHPCS.

So I'll just add T_FINALLY as a new token because it should end up in the documentation eventually.

+ T_FOR,
+ T_FOREACH,
+ T_FUNCTION,
+ T_GLOBAL,
+ T_GOTO,
+ T_IF,
+ T_IMPLEMENTS,
+ T_INCLUDE,
+ T_INCLUDE_ONCE,
+ T_INSTANCEOF,
+ T_INSTEADOF,
+ T_INTERFACE,
+ T_ISSET,
+ T_LIST,
+ T_LOGICAL_AND,
+ T_LOGICAL_OR,
+ T_LOGICAL_XOR,
+ T_NAMESPACE,
+ T_NEW,
+ T_PRINT,
+ T_PRIVATE,
+ T_PROTECTED,
+ T_PUBLIC,
+ T_REQUIRE,
+ T_REQUIRE_ONCE,
+ T_RETURN,
+ T_STATIC,
+ T_SWITCH,
+ T_THROW,
+ T_TRAIT,
+ T_TRY,
+ T_UNSET,
+ T_USE,
+ T_VAR,
+ T_WHILE,
+ );
+
+ }//end register()
+
+
+ /**
+ * Processes this sniff, when one of its tokens is encountered.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
+ * @param int $stackPtr The position of the current token in the
+ * stack passed in $tokens.
+ *
+ * @return void
+ */
+ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+ $tokens = $phpcsFile->getTokens();
+ $keyword = $tokens[$stackPtr]['content'];
+ if (strtolower($keyword) !== $keyword) {
+ $error = 'PHP keywords must be lowercase; expected "%s" but found "%s"';
+ $data = array(
+ strtolower($keyword),
+ $keyword,
+ );
+ $phpcsFile->addError($error, $stackPtr, 'Found', $data);
+ }
+
+ }//end process()
+
+
+}//end class
+
+?>
@@ -0,0 +1,16 @@
+<?php
+// A few keywords. Obviously not valid syntax.
+class Something extends SomethingElse implements Whatever
+abstract public private protected function whatever() {}
+const array()
+for ($var as $var) { exit; }
+if ($a and $b or $c xor $d) { die; } elseif { } else
+goto a;
+
+Class Something EXTENDS SomethingElse implementS Whatever
+Abstract Public Private Protected function whatever() {}
+CONST array()
+For ($var As $var) { Exit; }
+If ($a AND $b OR $c XOR $d) { Die; } ElseIf { } Else
+GOTO a;
+?>
@@ -0,0 +1,72 @@
+<?php
+/**
+ * Unit test class for the LowerCaseKeyword sniff.
+ *
+ * PHP version 5
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ */
+
+/**
+ * Unit test class for the LowerCaseKeyword sniff.
+ *
+ * A sniff unit test checks a .inc file for expected violations of a single
+ * coding standard. Expected errors and warnings are stored in this class.
+ *
+ * @category PHP
+ * @package PHP_CodeSniffer
+ * @author Greg Sherwood <gsherwood@squiz.net>
+ * @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
+ * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/PHP_CodeSniffer
+ */
+class Generic_Tests_PHP_LowerCaseKeywordUnitTest extends AbstractSniffUnitTest
+{
+
+
+ /**
+ * Returns the lines where errors should occur.
+ *
+ * The key of the array should represent the line number and the value
+ * should represent the number of errors that should occur on that line.
+ *
+ * @return array(int => int)
+ */
+ public function getErrorList()
+ {
+ return array(
+ 10 => 3,
+ 11 => 4,
+ 12 => 1,
+ 13 => 3,
+ 14 => 7,
+ 15 => 1,
+ );
+
+ }//end getErrorList()
+
+
+ /**
+ * Returns the lines where warnings should occur.
+ *
+ * The key of the array should represent the line number and the value
+ * should represent the number of warnings that should occur on that line.
+ *
+ * @return array(int => int)
+ */
+ public function getWarningList()
+ {
+ return array();
+
+ }//end getWarningList()
+
+
+}//end class
+
+?>
View
@@ -407,6 +407,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="LowerCaseConstantSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
+ <file baseinstalldir="PHP" name="LowerCaseKeywordSniff.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file baseinstalldir="PHP" name="NoSilencedErrorsSniff.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
@@ -627,6 +630,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP" name="LowerCaseConstantUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
+ <file baseinstalldir="PHP" name="LowerCaseKeywordUnitTest.inc" role="test" />
+ <file baseinstalldir="PHP" name="LowerCaseKeywordUnitTest.php" role="test">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
<file baseinstalldir="PHP" name="NoSilencedErrorsUnitTest.inc" role="test" />
<file baseinstalldir="PHP" name="NoSilencedErrorsUnitTest.php" role="test">
<tasks:replace from="@package_version@" to="version" type="package-info" />

0 comments on commit 5f876fa

Please sign in to comment.