| Conditions | 19 |
| Paths | 923 |
| Total Lines | 77 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 101 | protected function applyFix(\SplFileInfo $file, Tokens $tokens): void |
||
| 102 | { |
||
| 103 | $tokensAnalyzer = new TokensAnalyzer($tokens); |
||
| 104 | $propertyTypeDeclarationKinds = [T_STRING, T_NS_SEPARATOR, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT, CT::T_TYPE_ALTERNATION]; |
||
| 105 | |||
| 106 | foreach (array_reverse($tokensAnalyzer->getClassyElements(), true) as $index => $element) { |
||
| 107 | if (!\in_array($element['type'], $this->configuration['elements'], true)) { |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (\PHP_VERSION_ID < 70100 && 'const' === $element['type']) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | $abstractFinalIndex = null; |
||
| 116 | $visibilityIndex = null; |
||
| 117 | $staticIndex = null; |
||
| 118 | $typeIndex = null; |
||
| 119 | $prevIndex = $tokens->getPrevMeaningfulToken($index); |
||
| 120 | $expectedKinds = [T_ABSTRACT, T_FINAL, T_PRIVATE, T_PROTECTED, T_PUBLIC, T_STATIC, T_VAR]; |
||
| 121 | |||
| 122 | if ('property' === $element['type']) { |
||
| 123 | $expectedKinds = array_merge($expectedKinds, $propertyTypeDeclarationKinds); |
||
| 124 | } |
||
| 125 | |||
| 126 | while ($tokens[$prevIndex]->isGivenKind($expectedKinds)) { |
||
| 127 | if ($tokens[$prevIndex]->isGivenKind([T_ABSTRACT, T_FINAL])) { |
||
| 128 | $abstractFinalIndex = $prevIndex; |
||
| 129 | } elseif ($tokens[$prevIndex]->isGivenKind(T_STATIC)) { |
||
| 130 | $staticIndex = $prevIndex; |
||
| 131 | } elseif ($tokens[$prevIndex]->isGivenKind($propertyTypeDeclarationKinds)) { |
||
| 132 | $typeIndex = $prevIndex; |
||
| 133 | } else { |
||
| 134 | $visibilityIndex = $prevIndex; |
||
| 135 | } |
||
| 136 | |||
| 137 | $prevIndex = $tokens->getPrevMeaningfulToken($prevIndex); |
||
|
|
|||
| 138 | } |
||
| 139 | |||
| 140 | if (null !== $typeIndex) { |
||
| 141 | $index = $typeIndex; |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($tokens[$prevIndex]->equals(',')) { |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (null !== $staticIndex) { |
||
| 149 | if ($this->isKeywordPlacedProperly($tokens, $staticIndex, $index)) { |
||
| 150 | $index = $staticIndex; |
||
| 151 | } else { |
||
| 152 | $this->moveTokenAndEnsureSingleSpaceFollows($tokens, $staticIndex, $index); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | if (null === $visibilityIndex) { |
||
| 157 | $tokens->insertAt($index, [new Token([T_PUBLIC, 'public']), new Token([T_WHITESPACE, ' '])]); |
||
| 158 | } else { |
||
| 159 | if ($tokens[$visibilityIndex]->isGivenKind(T_VAR)) { |
||
| 160 | $tokens[$visibilityIndex] = new Token([T_PUBLIC, 'public']); |
||
| 161 | } |
||
| 162 | if ($this->isKeywordPlacedProperly($tokens, $visibilityIndex, $index)) { |
||
| 163 | $index = $visibilityIndex; |
||
| 164 | } else { |
||
| 165 | $this->moveTokenAndEnsureSingleSpaceFollows($tokens, $visibilityIndex, $index); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | if (null === $abstractFinalIndex) { |
||
| 170 | continue; |
||
| 171 | } |
||
| 172 | |||
| 173 | if ($this->isKeywordPlacedProperly($tokens, $abstractFinalIndex, $index)) { |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->moveTokenAndEnsureSingleSpaceFollows($tokens, $abstractFinalIndex, $index); |
||
| 178 | } |
||
| 196 |