Complex classes like ArrayMatcher 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 ArrayMatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | final class ArrayMatcher extends Matcher |
||
| 12 | { |
||
| 13 | const UNBOUNDED_PATTERN = '@...@'; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var ValueMatcher |
||
| 17 | */ |
||
| 18 | private $propertyMatcher; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var PropertyAccessor |
||
| 22 | */ |
||
| 23 | private $accessor; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Parser |
||
| 27 | */ |
||
| 28 | private $parser; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param ValueMatcher $propertyMatcher |
||
| 32 | */ |
||
| 33 | public function __construct(ValueMatcher $propertyMatcher, Parser $parser) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritDoc} |
||
| 41 | */ |
||
| 42 | public function match($value, $pattern) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritDoc} |
||
| 66 | */ |
||
| 67 | public function canMatch($pattern) |
||
| 71 | |||
| 72 | private function isArrayPattern($pattern) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param array $values |
||
| 83 | * @param array $patterns |
||
| 84 | * @param string $parentPath |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | private function iterateMatch(array $values, array $patterns, $parentPath = "") |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if pattern elements exist in value array |
||
| 138 | * |
||
| 139 | * @param array $pattern |
||
| 140 | * @param array $values |
||
| 141 | * @param $parentPath |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | private function isPatternValid(array $pattern, array $values, $parentPath) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param $value |
||
| 162 | * @param $pattern |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | private function valueMatchPattern($value, $pattern) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param $path |
||
| 179 | * @param $haystack |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | private function valueExist($path, array $haystack) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $property |
||
| 204 | * @param array $objectOrArray |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | private function arrayPropertyExists($property, array $objectOrArray) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $array |
||
| 215 | * @param $path |
||
| 216 | * @return mixed |
||
| 217 | */ |
||
| 218 | private function getValueByPath($array, $path) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return \Symfony\Component\PropertyAccess\PropertyAccessorInterface |
||
| 225 | */ |
||
| 226 | private function getPropertyAccessor() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $place |
||
| 240 | * @param $path |
||
| 241 | */ |
||
| 242 | private function setMissingElementInError($place, $path) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param $key |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | private function formatAccessPath($key) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * @param $parentPath |
||
| 258 | * @param $path |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | private function formatFullPath($parentPath, $path) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param $lastPattern |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | private function shouldSkippValueMatchingFor($lastPattern) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param $value |
||
| 277 | * @param $pattern |
||
| 278 | * @return bool |
||
| 279 | * @throws \Coduo\PHPMatcher\Exception\UnknownExpanderException |
||
| 280 | */ |
||
| 281 | private function allExpandersMatch($value, $pattern) |
||
| 291 | } |
||
| 292 |