Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed bug #20044 : PSR1 camelCase check does not ignore leading under…

…scores

Required the PSR1 standard to use its own sniff rather than rely on a generic one. This also stops function names from being checked as the PSR1 standard only mentions "methods" under the class heading.
  • Loading branch information...
commit dfccab1abd59c0d9166201601aa1ff6f02991490 1 parent 1454a0a
@gsherwood gsherwood authored
View
92 CodeSniffer/Standards/PSR1/Sniffs/Methods/CamelCapsMethodNameSniff.php
@@ -0,0 +1,92 @@
+<?php
+/**
+ * PSR1_Sniffs_Methods_CamelCapsMethodNameSniff.
+ *
+ * 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
+ */
+
+if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) {
+ throw new PHP_CodeSniffer_Exception('Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found');
+}
+
+/**
+ * PSR1_Sniffs_Methods_CamelCapsMethodNameSniff.
+ *
+ * Ensures method names are defined using camel case.
+ *
+ * @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 PSR1_Sniffs_Methods_CamelCapsMethodNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff
+{
+
+ /**
+ * Constructs a PSR1_Sniffs_Methods_CamelCapsMethodNameSniff.
+ */
+ public function __construct()
+ {
+ parent::__construct(array(T_CLASS, T_INTERFACE, T_TRAIT), array(T_FUNCTION), true);
+
+ }//end __construct()
+
+
+ /**
+ * Processes the tokens within the scope.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
+ * @param int $stackPtr The position where this token was
+ * found.
+ * @param int $currScope The position of the current scope.
+ *
+ * @return void
+ */
+ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
+ {
+ $methodName = $phpcsFile->getDeclarationName($stackPtr);
+ if ($methodName === null) {
+ // Ignore closures.
+ return;
+ }
+
+ $testName = ltrim($methodName, '_');
+ if (PHP_CodeSniffer::isCamelCaps($testName, false, true, false) === false) {
+ $error = 'Method name "%s" is not in camel caps format';
+ $className = $phpcsFile->getDeclarationName($currScope);
+ $errorData = array($className.'::'.$methodName);
+ $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData);
+ }
+
+ }//end processTokenWithinScope()
+
+
+ /**
+ * Processes the tokens outside the scope.
+ *
+ * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
+ * @param int $stackPtr The position where this token was
+ * found.
+ *
+ * @return void
+ */
+ protected function processTokenOutsideScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
+ {
+
+
+ }//end processTokenOutsideScope()
+
+
+}//end class
+
+?>
View
63 CodeSniffer/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.inc
@@ -0,0 +1,63 @@
+<?php
+
+abstract class My_Class {
+
+ public function __construct() {}
+ public function My_Class() {}
+ public function _My_Class() {}
+
+ public function getSomeValue() {}
+ public function parseMyDSN() {}
+ public function get_some_value() {}
+ public function GetSomeValue() {}
+ public function getSomeValue_Again() {}
+
+ protected function getSomeValue() {}
+ protected function parseMyDSN() {}
+ protected function get_some_value() {}
+
+ private function getSomeValue() {}
+ private function parseMyDSN() {}
+ private function get_some_value() {}
+
+ function getSomeValue() {}
+ function parseMyDSN() {}
+ function get_some_value() {}
+
+}//end class
+
+function getSomeValue() {}
+function parseMyDSN() {}
+function get_some_value() {}
+
+class Closure_Test {
+ function test() {
+ $foo = function() { echo 'foo'; };
+ }
+}
+
+trait Foo
+{
+ function __call() {}
+}
+
+class Magic_Case_Test {
+ function __Construct() {}
+ function __isSet() {}
+ function __tostring() {}
+}
+function __autoLoad() {}
+
+class Foo extends \SoapClient
+{
+ public function __soapCall(
+ $functionName,
+ $arguments,
+ $options = array(),
+ $inputHeaders = null,
+ &$outputHeaders = array()
+ ) {
+ // body
+ }
+}
+?>
View
75 CodeSniffer/Standards/PSR1/Tests/Methods/CamelCapsMethodNameUnitTest.php
@@ -0,0 +1,75 @@
+<?php
+/**
+ * Unit test class for the CamelCapsMethodName 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 CamelCapsMethodName 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 PSR1_Tests_Methods_CamelCapsMethodNameUnitTest 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(
+ 6 => 1,
+ 7 => 1,
+ 11 => 1,
+ 12 => 1,
+ 13 => 1,
+ 17 => 1,
+ 21 => 1,
+ 25 => 1,
+ 45 => 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
7 CodeSniffer/Standards/PSR1/ruleset.xml
@@ -40,9 +40,6 @@
<!-- 4.3. Methods -->
<!-- Method names MUST be declared in camelCase(). -->
- <rule ref="Generic.NamingConventions.CamelCapsFunctionName">
- <properties>
- <property name="strict" value="false"/>
- </properties>
- </rule>
+ <!-- checked in Methods/CamelCaseMethodNameSniff -->
+
</ruleset>
View
12 package.xml
@@ -42,6 +42,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #20026 : Check for multi-line arrays that should be single-line is slightly wrong
-- Adds new error message for single-line arrays that end with a comma
- Fixed bug #20029 : ForbiddenFunction sniff incorrectly recognizes methods in USE clauses
+ - Fixed bug #20044 : PSR1 camelCase check does not ignore leading underscores
</notes>
<contents>
<dir name="/">
@@ -1135,6 +1136,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
</dir>
+ <dir name="Methods">
+ <file baseinstalldir="PHP" name="CamelCapsMethodNameSniff.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
+ </dir>
</dir>
<dir name="Tests">
<dir name="Classes">
@@ -1152,6 +1158,12 @@ http://pear.php.net/dtd/package-2.0.xsd">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
</dir>
+ <dir name="Methods">
+ <file baseinstalldir="PHP" name="CamelCapsMethodNameUnitTest.inc" role="test" />
+ <file baseinstalldir="PHP" name="CamelCapsMethodNameUnitTest.php" role="test">
+ <tasks:replace from="@package_version@" to="version" type="package-info" />
+ </file>
+ </dir>
</dir>
<file baseinstalldir="PHP" name="ruleset.xml" role="php" />
</dir>
Please sign in to comment.
Something went wrong with that request. Please try again.