| Conditions | 15 |
| Paths | 10 |
| Total Lines | 65 |
| Code Lines | 28 |
| Lines | 35 |
| Ratio | 53.85 % |
| 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 |
||
| 46 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 47 | { |
||
| 48 | if ($this->supportsBelow('5.2') !== true) { |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | $tokens = $phpcsFile->getTokens(); |
||
| 53 | |||
| 54 | $equalSign = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true); |
||
| 55 | if ($equalSign === false || $tokens[$equalSign]['code'] !== T_EQUAL) { |
||
| 56 | // Not an assignment. |
||
| 57 | return; |
||
| 58 | } |
||
| 59 | |||
| 60 | $prevNonEmpty = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($equalSign - 1), null, true, null, true); |
||
| 61 | if ($prevNonEmpty === false || |
||
| 62 | ($tokens[$prevNonEmpty]['code'] !== T_VARIABLE && $tokens[$prevNonEmpty]['code'] !== T_STRING) |
||
| 63 | ) { |
||
| 64 | // Not a variable or constant assignment. |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | |||
| 69 | switch ($tokens[$prevNonEmpty]['type']) { |
||
| 70 | /* |
||
| 71 | * Check class constant assignments. |
||
| 72 | */ |
||
| 73 | View Code Duplication | case 'T_STRING': |
|
|
1 ignored issue
–
show
|
|||
| 74 | // Walk back to check for the const keyword. |
||
| 75 | $constPtr = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true, null, true); |
||
| 76 | if ($constPtr === false || $tokens[$constPtr]['code'] !== T_CONST) { |
||
| 77 | // Not a constant assignment. |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | if ($this->isClassConstant($phpcsFile, $constPtr) === true) { |
||
| 82 | $this->throwError($phpcsFile, $stackPtr, 'const'); |
||
| 83 | } |
||
| 84 | return; |
||
| 85 | |||
| 86 | View Code Duplication | case 'T_VARIABLE': |
|
|
1 ignored issue
–
show
|
|||
| 87 | /* |
||
| 88 | * Check class property assignments. |
||
| 89 | */ |
||
| 90 | if ($this->isClassProperty($phpcsFile, $prevNonEmpty) === true) { |
||
| 91 | $this->throwError($phpcsFile, $stackPtr, 'property'); |
||
| 92 | } |
||
| 93 | |||
| 94 | /* |
||
| 95 | * Check static variable assignments. |
||
| 96 | */ |
||
| 97 | else { |
||
| 98 | // Walk back to check this is a static variable `static $var =`. |
||
| 99 | $staticPtr = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($prevNonEmpty - 1), null, true, null, true); |
||
| 100 | if ($staticPtr === false || $tokens[$staticPtr]['code'] !== T_STATIC) { |
||
| 101 | // Not a static variable assignment. |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | // Still here ? Then we have a static variable assignment |
||
| 106 | $this->throwError($phpcsFile, $stackPtr, 'staticvar'); |
||
| 107 | } |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 153 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.