Conditions | 11 |
Paths | 12 |
Total Lines | 64 |
Code Lines | 34 |
Lines | 15 |
Ratio | 23.44 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
53 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
54 | { |
||
55 | if ($this->supportsAbove('7.0')) { |
||
56 | $tokens = $phpcsFile->getTokens(); |
||
57 | |||
58 | // Skip tokens that are the names of functions or classes |
||
59 | // within their definitions. For example: function myFunction... |
||
60 | // "myFunction" is T_STRING but we should skip because it is not a |
||
61 | // function or method *call*. |
||
62 | $functionName = $stackPtr; |
||
63 | $findTokens = array_merge( |
||
64 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
65 | array(T_BITWISE_AND) |
||
66 | ); |
||
67 | |||
68 | $functionKeyword = $phpcsFile->findPrevious( |
||
69 | $findTokens, |
||
70 | ($stackPtr - 1), |
||
71 | null, |
||
72 | true |
||
73 | ); |
||
74 | |||
75 | View Code Duplication | if ($tokens[$functionKeyword]['code'] === T_FUNCTION |
|
1 ignored issue
–
show
|
|||
76 | || $tokens[$functionKeyword]['code'] === T_CLASS |
||
77 | ) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | // If the next non-whitespace token after the function or method call |
||
82 | // is not an opening parenthesis then it cant really be a *call*. |
||
83 | $openBracket = $phpcsFile->findNext( |
||
84 | PHP_CodeSniffer_Tokens::$emptyTokens, |
||
85 | ($functionName + 1), |
||
86 | null, |
||
87 | true |
||
88 | ); |
||
89 | |||
90 | if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
||
91 | return; |
||
92 | } |
||
93 | |||
94 | $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
||
95 | |||
96 | $nextSeparator = $openBracket; |
||
97 | while (($nextComma = $phpcsFile->findNext(T_COMMA, ($nextSeparator + 1), $closeBracket)) !== false) { |
||
98 | // Find every argument along the way, check if there are parenthesis around them |
||
99 | |||
100 | View Code Duplication | if ($tokens[$nextSeparator + 1]['code'] == T_OPEN_PARENTHESIS && $tokens[$nextComma - 1]['code'] == T_CLOSE_PARENTHESIS) { |
|
1 ignored issue
–
show
|
|||
101 | $error = 'Parentheses around function parameters throws warning in PHP 7.0'; |
||
102 | $phpcsFile->addWarning($error, $nextSeparator + 1); |
||
103 | } |
||
104 | |||
105 | $nextSeparator = $nextComma; |
||
106 | }//end while |
||
107 | |||
108 | $nextOpen = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $nextSeparator + 1, $closeBracket); |
||
109 | View Code Duplication | if ($nextOpen !== false) { |
|
1 ignored issue
–
show
|
|||
110 | if ($tokens[$nextOpen]['code'] == T_OPEN_PARENTHESIS && $tokens[$closeBracket - 1]['code'] == T_CLOSE_PARENTHESIS) { |
||
111 | $error = 'Parentheses around function parameters throws warning in PHP 7.0'; |
||
112 | $phpcsFile->addWarning($error, $nextSeparator + 1); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | }//end process() |
||
117 | |||
119 |
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.