| Conditions | 13 |
| Paths | 9 |
| Total Lines | 66 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 55 | public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 56 | { |
||
| 57 | $tokens = $phpcsFile->getTokens(); |
||
| 58 | |||
| 59 | if ($tokens[$stackPtr]['code'] === T_NEW && $this->supportsBelow('5.3') !== true) { |
||
| 60 | return; |
||
| 61 | } elseif ($tokens[$stackPtr]['code'] === T_CLONE && $this->supportsBelow('5.6') !== true) { |
||
| 62 | return; |
||
| 63 | } |
||
| 64 | |||
| 65 | if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) { |
||
| 66 | // The `new className/clone $a` has to be in parentheses, without is not supported. |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | $parenthesisCloser = end($tokens[$stackPtr]['nested_parenthesis']); |
||
| 71 | $parenthesisOpener = key($tokens[$stackPtr]['nested_parenthesis']); |
||
| 72 | |||
| 73 | if (isset($tokens[$parenthesisOpener]['parenthesis_owner']) === true) { |
||
| 74 | // If there is an owner, these parentheses are for a different purpose. |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $prevBeforeParenthesis = $phpcsFile->findPrevious( |
||
| 79 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 80 | ($parenthesisOpener - 1), |
||
| 81 | null, |
||
| 82 | true |
||
| 83 | ); |
||
| 84 | if ($prevBeforeParenthesis !== false && $tokens[$prevBeforeParenthesis]['code'] === T_STRING) { |
||
| 85 | // This is most likely a function call with the new/cloned object as a parameter. |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | $nextAfterParenthesis = $phpcsFile->findNext( |
||
| 90 | \PHP_CodeSniffer_Tokens::$emptyTokens, |
||
| 91 | ($parenthesisCloser + 1), |
||
| 92 | null, |
||
| 93 | true |
||
| 94 | ); |
||
| 95 | if ($nextAfterParenthesis === false) { |
||
| 96 | // Live coding. |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($tokens[$nextAfterParenthesis]['code'] !== T_OBJECT_OPERATOR |
||
| 101 | && $tokens[$nextAfterParenthesis]['code'] !== T_OPEN_SQUARE_BRACKET |
||
| 102 | ) { |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | $data = array('instantiation', '5.3'); |
||
| 107 | $errorCode = 'OnNewFound'; |
||
| 108 | |||
| 109 | if ($tokens[$stackPtr]['code'] === T_CLONE) { |
||
| 110 | $data = array('cloning', '5.6'); |
||
| 111 | $errorCode = 'OnCloneFound'; |
||
| 112 | } |
||
| 113 | |||
| 114 | $phpcsFile->addError( |
||
| 115 | 'Class member access on object %s was not supported in PHP %s or earlier', |
||
| 116 | $parenthesisCloser, |
||
| 117 | $errorCode, |
||
| 118 | $data |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | } |
||
| 122 |