| Conditions | 21 |
| Paths | 63 |
| Total Lines | 102 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 91 | protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
||
| 92 | { |
||
| 93 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
||
| 94 | if ($methodName === null) { |
||
| 95 | // Ignore closures. |
||
| 96 | return; |
||
| 97 | } |
||
| 98 | |||
| 99 | $className = $phpcsFile->getDeclarationName($currScope); |
||
| 100 | $errorData = array($className.'::'.$methodName); |
||
| 101 | |||
| 102 | // Is this a magic method. i.e., is prefixed with "__" ? |
||
| 103 | if (strpos($methodName, '__') === 0) { |
||
| 104 | $magicPart = strtolower(substr($methodName, 2)); |
||
| 105 | |||
| 106 | if (isset($this->magicMethods[$magicPart]) === false) { |
||
| 107 | $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; |
||
| 108 | $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); |
||
| 109 | } |
||
| 110 | |||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | // PHP4 constructors are allowed to break our rules. |
||
| 115 | if ($methodName === $className) { |
||
| 116 | return; |
||
| 117 | } |
||
| 118 | |||
| 119 | // PHP4 destructors are allowed to break our rules. |
||
| 120 | if ($methodName === '_'.$className) { |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | |||
| 124 | $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
||
| 125 | $isPublic = ($methodProps['scope'] === 'private') ? false : true; |
||
| 126 | $scope = $methodProps['scope']; |
||
| 127 | $scopeSpecified = $methodProps['scope_specified']; |
||
| 128 | |||
| 129 | // If it's a private method, it must have an underscore on the front. |
||
| 130 | if ($isPublic === false) { |
||
| 131 | if ($methodName{0} !== '_') { |
||
| 132 | $error = 'Private method name "%s" must be prefixed with an underscore'; |
||
| 133 | $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData); |
||
| 134 | |||
| 135 | if (isset($phpcsFile->fixer) === true) { |
||
| 136 | $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no'); |
||
| 137 | } |
||
| 138 | |||
| 139 | return; |
||
| 140 | } else { |
||
| 141 | if (isset($phpcsFile->fixer) === true) { |
||
| 142 | $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes'); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | // If it's not a private method, it must not have an underscore on the front. |
||
| 148 | if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') { |
||
| 149 | $error = '%s method name "%s" must not be prefixed with an underscore'; |
||
| 150 | $data = array( |
||
| 151 | ucfirst($scope), |
||
| 152 | $errorData[0], |
||
| 153 | ); |
||
| 154 | $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data); |
||
| 155 | return; |
||
| 156 | } |
||
| 157 | |||
| 158 | // If the scope was specified on the method, then the method must be |
||
| 159 | // camel caps and an underscore should be checked for. If it wasn't |
||
| 160 | // specified, treat it like a public method and remove the underscore |
||
| 161 | // prefix if there is one because we cant determine if it is private or |
||
| 162 | // public. |
||
| 163 | $testMethodName = $methodName; |
||
| 164 | if ($scopeSpecified === false && $methodName{0} === '_') { |
||
| 165 | $testMethodName = substr($methodName, 1); |
||
| 166 | } |
||
| 167 | |||
| 168 | $methodParams = $phpcsFile->getMethodParameters($stackPtr); |
||
| 169 | |||
| 170 | if ($this->isExclusion($className, $methodName, $isPublic) === true |
||
| 171 | || $this->isEventHandlerExclusion($className, $methodName, $methodParams) === true |
||
| 172 | || $this->isTagProcessorExclusion($className, $methodName, $methodParams) === true |
||
| 173 | ) { |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | if (Common::isCamelCaps($testMethodName, false, $isPublic, false) === false) { |
||
| 178 | if ($scopeSpecified === true) { |
||
| 179 | $error = '%s method name "%s" is not in camel caps format'; |
||
| 180 | $data = array( |
||
| 181 | ucfirst($scope), |
||
| 182 | $errorData[0], |
||
| 183 | ); |
||
| 184 | $phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data); |
||
| 185 | } else { |
||
| 186 | $error = 'Method name "%s" is not in camel caps format'; |
||
| 187 | $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); |
||
| 188 | } |
||
| 189 | |||
| 190 | return; |
||
| 191 | } |
||
| 192 | }//end processTokenWithinScope() |
||
| 193 | |||
| 253 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.