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 |
||
54 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
55 | { |
||
56 | if ($this->supportsAbove('5.3') === false) { |
||
57 | return; |
||
58 | } |
||
59 | |||
60 | $tokens = $phpcsFile->getTokens(); |
||
61 | |||
62 | // Skip tokens that are the names of functions or classes |
||
63 | // within their definitions. For example: function myFunction... |
||
64 | // "myFunction" is T_STRING but we should skip because it is not a |
||
65 | // function or method *call*. |
||
66 | $findTokens = \PHP_CodeSniffer_Tokens::$emptyTokens; |
||
67 | $findTokens[] = T_BITWISE_AND; |
||
68 | |||
69 | $prevNonEmpty = $phpcsFile->findPrevious( |
||
70 | $findTokens, |
||
71 | ($stackPtr - 1), |
||
72 | null, |
||
73 | true |
||
74 | ); |
||
75 | |||
76 | if ($prevNonEmpty !== false && in_array($tokens[$prevNonEmpty]['code'], array(T_FUNCTION, T_CLASS, T_INTERFACE, T_TRAIT), true)) { |
||
77 | return; |
||
78 | } |
||
79 | |||
80 | // If the next non-whitespace token after the function or method call |
||
81 | // is not an opening parenthesis then it can't really be a *call*. |
||
82 | $openBracket = $phpcsFile->findNext( |
||
83 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
84 | ($stackPtr + 1), |
||
85 | null, |
||
86 | true |
||
87 | ); |
||
88 | |||
89 | if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS |
||
90 | || isset($tokens[$openBracket]['parenthesis_closer']) === false |
||
91 | ) { |
||
92 | return; |
||
93 | } |
||
94 | |||
95 | // Get the function call parameters. |
||
96 | $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); |
||
97 | if (count($parameters) === 0) { |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | // Which nesting level is the one we are interested in ? |
||
102 | $nestedParenthesisCount = 1; |
||
103 | View Code Duplication | if (isset($tokens[$openBracket]['nested_parenthesis'])) { |
|
1 ignored issue
–
show
|
|||
104 | $nestedParenthesisCount = count($tokens[$openBracket]['nested_parenthesis']) + 1; |
||
105 | } |
||
106 | |||
107 | foreach ($parameters as $parameter) { |
||
108 | if ($this->isCallTimePassByReferenceParam($phpcsFile, $parameter, $nestedParenthesisCount) === true) { |
||
109 | // T_BITWISE_AND represents a pass-by-reference. |
||
110 | $error = 'Using a call-time pass-by-reference is deprecated since PHP 5.3'; |
||
111 | $isError = false; |
||
112 | $errorCode = 'Deprecated'; |
||
113 | |||
114 | if ($this->supportsAbove('5.4')) { |
||
115 | $error .= ' and prohibited since PHP 5.4'; |
||
116 | $isError = true; |
||
117 | $errorCode = 'NotAllowed'; |
||
118 | } |
||
119 | |||
120 | $this->addMessage($phpcsFile, $error, $parameter['start'], $isError, $errorCode); |
||
121 | } |
||
122 | } |
||
123 | }//end process() |
||
124 | |||
246 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.