| Conditions | 14 |
| Paths | 22 |
| Total Lines | 64 |
| Code Lines | 35 |
| 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 |
||
| 96 | private static function makeIndentsMap(string $string, bool $ignoreSingleSpaces): array |
||
| 97 | { |
||
| 98 | $indents = []; |
||
| 99 | |||
| 100 | // Remember the size of previous line's indentation |
||
| 101 | $previousSize = 0; |
||
| 102 | $previousIndentType = null; |
||
| 103 | |||
| 104 | // Indents key (ident type + size of the indents/unindents) |
||
| 105 | $key = null; |
||
| 106 | |||
| 107 | $lines = \preg_split('/\R/', $string); |
||
| 108 | if ($lines === false) { |
||
| 109 | throw new \InvalidArgumentException('Invalid string'); |
||
| 110 | } |
||
| 111 | |||
| 112 | foreach ($lines as $line) { |
||
| 113 | if ($line === '') { |
||
| 114 | // Ignore empty lines |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment |
||
| 119 | if (\preg_match('/^(?:( )+|\t+)/', $line, $matches) !== 1) { |
||
| 120 | $previousSize = 0; |
||
| 121 | $previousIndentType = ''; |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | |||
| 125 | $indent = \strlen($matches[0]); |
||
| 126 | $indentType = isset($matches[1]) ? self::TYPE_SPACE : self::TYPE_TAB; |
||
| 127 | // Ignore single space unless it's the only indent detected to prevent common false positives |
||
| 128 | if ($ignoreSingleSpaces && $indentType === self::TYPE_SPACE && $indent === 1) { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($indentType !== $previousIndentType) { |
||
| 133 | $previousSize = 0; |
||
| 134 | } |
||
| 135 | |||
| 136 | $previousIndentType = $indentType; |
||
| 137 | $weight = 0; |
||
| 138 | $indentDifference = $indent - $previousSize; |
||
| 139 | $previousSize = $indent; |
||
| 140 | |||
| 141 | // Previous line have same indent? |
||
| 142 | if ($indentDifference === 0) { |
||
| 143 | $weight++; |
||
| 144 | // We use the key from previous loop |
||
| 145 | \assert(isset($key) && \is_string($key)); |
||
| 146 | } else { |
||
| 147 | $key = self::encodeIndentsKey($indentType, $indentDifference > 0 ? $indentDifference : -$indentDifference); |
||
| 148 | } |
||
| 149 | |||
| 150 | // Update the stats |
||
| 151 | if (! isset($indents[$key])) { |
||
| 152 | $indents[$key] = [1, 0]; |
||
| 153 | } else { |
||
| 154 | $indents[$key][0]++; |
||
| 155 | $indents[$key][1] += $weight; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return $indents; |
||
| 160 | } |
||
| 212 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..