| Conditions | 16 |
| Paths | 28 |
| Total Lines | 94 |
| Code Lines | 51 |
| Lines | 7 |
| Ratio | 7.45 % |
| 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 |
||
| 68 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 69 | { |
||
| 70 | $tokens = $phpcsFile->getTokens(); |
||
| 71 | $functionLc = strtolower($tokens[$stackPtr]['content']); |
||
| 72 | if (isset($this->targetFunctions[$functionLc]) === false) { |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Next non-empty token should be the open parenthesis. |
||
| 77 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true); |
||
| 78 | if ($nextNonEmpty === false || $tokens[$nextNonEmpty]['code'] !== T_OPEN_PARENTHESIS) { |
||
| 79 | return; |
||
| 80 | } |
||
| 81 | |||
| 82 | $ignore = array( |
||
| 83 | T_DOUBLE_COLON => true, |
||
| 84 | T_OBJECT_OPERATOR => true, |
||
| 85 | T_FUNCTION => true, |
||
| 86 | T_NEW => true, |
||
| 87 | ); |
||
| 88 | |||
| 89 | $prevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
| 90 | View Code Duplication | if (isset($ignore[$tokens[$prevNonEmpty]['code']]) === true) { |
|
| 91 | // Not a call to a PHP function. |
||
| 92 | return; |
||
| 93 | } elseif ($tokens[$prevNonEmpty]['code'] === T_NS_SEPARATOR && $tokens[$prevNonEmpty - 1]['code'] === T_STRING) { |
||
| 94 | // Namespaced function. |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | $data = $tokens[$stackPtr]['content']; |
||
| 99 | |||
| 100 | /* |
||
| 101 | * Check for usage of the functions in the global scope. |
||
| 102 | * |
||
| 103 | * As PHPCS can not determine whether a file is included from within a function in |
||
| 104 | * another file, so always throw a warning/error. |
||
| 105 | */ |
||
| 106 | if ($phpcsFile->hasCondition($stackPtr, array(T_FUNCTION, T_CLOSURE)) === false) { |
||
| 107 | $isError = false; |
||
| 108 | $message = 'Use of %s() outside of a user-defined function is only supported if the file is included from within a user-defined function in another file prior to PHP 5.3.'; |
||
| 109 | |||
| 110 | if ($this->supportsAbove('5.3') === true) { |
||
| 111 | $isError = true; |
||
| 112 | $message .= ' As of PHP 5.3, it is no longer supported at all.'; |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->addMessage($phpcsFile, $message, $stackPtr, $isError, 'OutsideFunctionScope', $data); |
||
| 116 | } |
||
| 117 | |||
| 118 | /* |
||
| 119 | * Check for usage of the functions as a parameter in a function call. |
||
| 120 | */ |
||
| 121 | if ($this->supportsBelow('5.2') === false) { |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) { |
||
| 126 | return; |
||
| 127 | } |
||
| 128 | |||
| 129 | $throwError = false; |
||
|
|
|||
| 130 | |||
| 131 | $closer = end($tokens[$stackPtr]['nested_parenthesis']); |
||
| 132 | if (isset($tokens[$closer]['parenthesis_owner']) |
||
| 133 | && $tokens[$tokens[$closer]['parenthesis_owner']]['type'] === 'T_CLOSURE' |
||
| 134 | ) { |
||
| 135 | $throwError = true; |
||
| 136 | } else { |
||
| 137 | $opener = key($tokens[$stackPtr]['nested_parenthesis']); |
||
| 138 | $prevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($opener - 1), null, true); |
||
| 139 | if ($tokens[$prevNonEmpty]['code'] !== T_STRING) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | $prevPrevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true); |
||
| 144 | if ($tokens[$prevPrevNonEmpty]['code'] === T_FUNCTION) { |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | $throwError = true; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($throwError === false) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | |||
| 155 | $phpcsFile->addError( |
||
| 156 | '%s() could not be used in parameter lists prior to PHP 5.3.', |
||
| 157 | $stackPtr, |
||
| 158 | 'InParameterList', |
||
| 159 | $data |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 164 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.