Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like StringConditionTree often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StringConditionTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types = 1); |
||
| 14 | class StringConditionTree |
||
| 15 | { |
||
| 16 | /** Tree node root element identifier, needed for recursion */ |
||
| 17 | const ROOT_NAME = ''; |
||
| 18 | |||
| 19 | /** Final tree node branch identifier */ |
||
| 20 | const SELF_NAME = '@self'; |
||
| 21 | |||
| 22 | /** String parameter start marker */ |
||
| 23 | const PARAMETER_START = '{'; |
||
| 24 | |||
| 25 | /** String parameter end marker */ |
||
| 26 | const PARAMETER_END = '}'; |
||
| 27 | |||
| 28 | /** Parameter sorting length value for counting */ |
||
| 29 | const PARAMETER_COF = 2000; |
||
| 30 | |||
| 31 | /** @var TreeNode Resulting collection for debugging */ |
||
| 32 | protected $debug; |
||
| 33 | |||
| 34 | /** @var array Collection of input string => identifier */ |
||
| 35 | protected $source; |
||
| 36 | |||
| 37 | /** @var string Parametrized string start marker */ |
||
| 38 | protected $parameterStartMarker = self::PARAMETER_START; |
||
| 39 | |||
| 40 | /** @var string Parametrized string end marker */ |
||
| 41 | protected $parameterEndMarker = self::PARAMETER_END; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * StringConditionTree constructor. |
||
| 45 | * |
||
| 46 | * @param string $parameterStartMarker Parametrized string start marker |
||
| 47 | * @param string $parameterEndMarker Parametrized string end marker |
||
| 48 | */ |
||
| 49 | public function __construct(string $parameterStartMarker = self::PARAMETER_START, string $parameterEndMarker = self::PARAMETER_END) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Build similarity strings tree. |
||
| 57 | * |
||
| 58 | * @param array $input Collection of strings |
||
| 59 | * |
||
| 60 | * @return TreeNode Resulting similarity strings tree |
||
| 61 | */ |
||
| 62 | public function process(array $input): TreeNode |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Build string character group structure considering parametrized |
||
| 79 | * and not parametrized character groups and their length(PCG, NPCG). |
||
| 80 | * |
||
| 81 | * @param string $prefix Prefix string |
||
| 82 | * |
||
| 83 | * @return array String character groups structure |
||
| 84 | */ |
||
| 85 | protected function getPrefixStructure(string $prefix): array |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Compare string structures. |
||
| 130 | * |
||
| 131 | * @param array $initial Initial string structure |
||
| 132 | * @param array $compared Compared string structure |
||
| 133 | * |
||
| 134 | * @return int Result of array elements comparison |
||
| 135 | */ |
||
| 136 | protected function compareStringStructure(array $initial, array $compared): int |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Sort strings array considering PCG and NPCG string structure. |
||
| 214 | * |
||
| 215 | * @param array $input Input array for sorting |
||
| 216 | * @return array Sorted array |
||
| 217 | */ |
||
| 218 | protected function sortArrayByKeys(array &$input) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Add only unique value to array. |
||
| 234 | * |
||
| 235 | * @param mixed $value Unique value |
||
| 236 | * @param array $array Array for adding unique value |
||
| 237 | * @param bool $strict Strict uniqueness check |
||
| 238 | * |
||
| 239 | * @return bool True if unique value was added |
||
| 240 | * @see in_array(); |
||
| 241 | * |
||
| 242 | */ |
||
| 243 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Find longest matching prefix between two strings. |
||
| 262 | * |
||
| 263 | * @param string $initialString Initial string |
||
| 264 | * @param string $comparedString Compared string |
||
| 265 | * |
||
| 266 | * @return string Longest matching prefix |
||
| 267 | */ |
||
| 268 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Remove key string from the beginning of all sub-array strings. |
||
| 325 | * |
||
| 326 | * @param array $array Input array of key => [keyStrings...] |
||
| 327 | * |
||
| 328 | * @return array Processed array with removed keys from beginning of sub arrays |
||
| 329 | */ |
||
| 330 | protected function removeKeyFromArrayStrings(array $array): array |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Find all duplication of source array values in compared array and remove them. |
||
| 355 | * |
||
| 356 | * @param array $source Source array |
||
| 357 | * @param array $compared Compared array for filtering duplicates |
||
| 358 | */ |
||
| 359 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
| 372 | * |
||
| 373 | * @param array $prefixes LMP collection, returning value |
||
| 374 | */ |
||
| 375 | protected function filterLMPStrings(array &$prefixes) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get collection of grouped longest matching prefixes with strings sub-array. |
||
| 387 | * |
||
| 388 | * @param array $input Input strings array |
||
| 389 | * |
||
| 390 | * @return array Longest matching prefixes array |
||
| 391 | */ |
||
| 392 | protected function getLMPCollection(array $input): array |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Recursive string similarity tree builder. |
||
| 420 | * |
||
| 421 | * @param string $prefix |
||
| 422 | * @param array $input |
||
| 423 | * @param TreeNode $result |
||
| 424 | * @param string $selfMarker |
||
| 425 | */ |
||
| 426 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
| 460 | } |
||
| 461 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.