Conditions | 12 |
Paths | 12 |
Total Lines | 70 |
Code Lines | 37 |
Lines | 3 |
Ratio | 4.29 % |
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 |
||
90 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
91 | { |
||
92 | if ($this->supportsAbove('5.3') === false) { |
||
93 | return; |
||
94 | } |
||
95 | |||
96 | $tokens = $phpcsFile->getTokens(); |
||
97 | |||
98 | // Skip tokens that are the names of functions or classes |
||
99 | // within their definitions. For example: function myFunction... |
||
100 | // "myFunction" is T_STRING but we should skip because it is not a |
||
101 | // function or method *call*. |
||
102 | $findTokens = \PHP_CodeSniffer_Tokens::$emptyTokens; |
||
103 | $findTokens[] = T_BITWISE_AND; |
||
104 | |||
105 | $prevNonEmpty = $phpcsFile->findPrevious( |
||
106 | $findTokens, |
||
107 | ($stackPtr - 1), |
||
108 | null, |
||
109 | true |
||
110 | ); |
||
111 | |||
112 | if ($prevNonEmpty !== false && in_array($tokens[$prevNonEmpty]['type'], array('T_FUNCTION', 'T_CLASS', 'T_INTERFACE', 'T_TRAIT'), true)) { |
||
113 | return; |
||
114 | } |
||
115 | |||
116 | // If the next non-whitespace token after the function or method call |
||
117 | // is not an opening parenthesis then it can't really be a *call*. |
||
118 | $openBracket = $phpcsFile->findNext( |
||
119 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
120 | ($stackPtr + 1), |
||
121 | null, |
||
122 | true |
||
123 | ); |
||
124 | |||
125 | if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS |
||
126 | || isset($tokens[$openBracket]['parenthesis_closer']) === false |
||
127 | ) { |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | // Get the function call parameters. |
||
132 | $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
||
133 | if (count($parameters) === 0) { |
||
134 | return; |
||
135 | } |
||
136 | |||
137 | // Which nesting level is the one we are interested in ? |
||
138 | $nestedParenthesisCount = 1; |
||
139 | View Code Duplication | if (isset($tokens[$openBracket]['nested_parenthesis'])) { |
|
140 | $nestedParenthesisCount = count($tokens[$openBracket]['nested_parenthesis']) + 1; |
||
141 | } |
||
142 | |||
143 | foreach ($parameters as $parameter) { |
||
144 | if ($this->isCallTimePassByReferenceParam($phpcsFile, $parameter, $nestedParenthesisCount) === true) { |
||
145 | // T_BITWISE_AND represents a pass-by-reference. |
||
146 | $error = 'Using a call-time pass-by-reference is deprecated since PHP 5.3'; |
||
147 | $isError = false; |
||
148 | $errorCode = 'Deprecated'; |
||
149 | |||
150 | if ($this->supportsAbove('5.4')) { |
||
151 | $error .= ' and prohibited since PHP 5.4'; |
||
152 | $isError = true; |
||
153 | $errorCode = 'NotAllowed'; |
||
154 | } |
||
155 | |||
156 | $this->addMessage($phpcsFile, $error, $parameter['start'], $isError, $errorCode); |
||
157 | } |
||
158 | } |
||
159 | }//end process() |
||
160 | |||
234 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.