| Conditions | 12 |
| Paths | 52 |
| Total Lines | 70 |
| Code Lines | 44 |
| 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 |
||
| 64 | protected function processTokenWithinScope( |
||
| 65 | File $phpcsFile, |
||
| 66 | $stackPtr, |
||
| 67 | $currScope |
||
| 68 | ) { |
||
| 69 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
||
| 70 | $className = $phpcsFile->getDeclarationName($currScope); |
||
| 71 | |||
| 72 | $isPhp4Constructor = strcasecmp($methodName, $className) === 0; |
||
| 73 | $isPhp5Constructor = strcasecmp($methodName, '__construct') === 0; |
||
| 74 | if ($this->php5Constructors != '0') { |
||
| 75 | if ($isPhp4Constructor) { |
||
| 76 | $error = "PHP4 style constructors are not allowed; use \"__construct\" instead"; |
||
| 77 | $phpcsFile->addError($error, $stackPtr); |
||
| 78 | } |
||
| 79 | } else { |
||
| 80 | if ($isPhp5Constructor) { |
||
| 81 | $error = "PHP5 style constructors are not allowed; use \"$className\" instead"; |
||
| 82 | $phpcsFile->addError($error, $stackPtr); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | if ( ! $isPhp4Constructor && ! $isPhp5Constructor ) { |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | $tokens = $phpcsFile->getTokens(); |
||
| 90 | |||
| 91 | $parentClassName = $phpcsFile->findExtendedClassName($currScope); |
||
| 92 | $wrongConstructor = ''; |
||
| 93 | // prepares the error message and wrong constructor |
||
| 94 | if ($this->php5Constructors != '0') { |
||
| 95 | $error = 'PHP4 style calls to parent constructors are not allowed.'; |
||
| 96 | $error = "$error Please use \"parent::__construct\" instead."; |
||
| 97 | if (false !== $parentClassName) { |
||
| 98 | $wrongConstructor = $parentClassName; |
||
| 99 | } |
||
| 100 | // Else $wrongConstructor will be empty |
||
| 101 | // and the test expression will always be false. |
||
| 102 | // It doesn't check that no parent method should be called |
||
| 103 | // when no parent class is defined. |
||
| 104 | } else { |
||
| 105 | $error = 'PHP5 style calls to parent constructors are not allowed.'; |
||
| 106 | if (false !== $parentClassName) { |
||
| 107 | $error = "$error Please use \"parent::$parentClassName\" instead."; |
||
| 108 | } |
||
| 109 | $wrongConstructor = '__construct'; |
||
| 110 | } |
||
| 111 | |||
| 112 | // looks for the use of a wrong constructor. |
||
| 113 | $endFunctionIndex = $tokens[$stackPtr]['scope_closer']; |
||
| 114 | $doubleColonIndex = $phpcsFile->findNext( |
||
| 115 | array(T_DOUBLE_COLON), |
||
| 116 | $stackPtr, |
||
| 117 | $endFunctionIndex |
||
| 118 | ); |
||
| 119 | while ($doubleColonIndex) { |
||
| 120 | if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING |
||
| 121 | && $tokens[($doubleColonIndex + 1)]['content'] === $wrongConstructor |
||
| 122 | ) { |
||
| 123 | $phpcsFile->addError($error, ($doubleColonIndex + 1)); |
||
| 124 | } |
||
| 125 | |||
| 126 | $doubleColonIndex = $phpcsFile->findNext( |
||
| 127 | array(T_DOUBLE_COLON), |
||
| 128 | $doubleColonIndex + 1, |
||
| 129 | $endFunctionIndex |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | }//end processTokenWithinScope() |
||
| 134 | |||
| 143 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.