Complex classes like FilterComponentFactory 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 FilterComponentFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | final class FilterComponentFactory implements ComponentFactoryInterface |
||
| 19 | { |
||
| 20 | private const OPT_COLLECTION_SATISFIED_BY = 'collection_satisfied_by'; |
||
| 21 | private const OPT_COMPOSITE_SATISFIED_BY = 'composite_satisfied_by'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var FilterUriManagerInterface |
||
| 25 | */ |
||
| 26 | private $uriManager; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * FilterComponentFactory constructor. |
||
| 30 | * @param array $options - This component doesn't have options yet, but let's keep the same signature, just in case |
||
| 31 | * @param FilterUriManagerInterface|null $uriManager |
||
| 32 | * @throws \Symfony\Component\OptionsResolver\Exception\AccessException |
||
| 33 | * @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
||
| 34 | * @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException |
||
| 35 | * @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException |
||
| 36 | * @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException |
||
| 37 | * @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
||
| 38 | */ |
||
| 39 | public function __construct(array $options = [], FilterUriManagerInterface $uriManager = null) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @inheritDoc |
||
| 46 | */ |
||
| 47 | public function supports(string $name): bool |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @inheritDoc |
||
| 54 | * @return FilterComponent |
||
| 55 | */ |
||
| 56 | public function createComponent(UriInterface $uri, array $options = []): ComponentInterface |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $key |
||
| 70 | * @param $value |
||
| 71 | * @param UriInterface $baseUri |
||
| 72 | * @param bool $applied |
||
| 73 | * @param array $options |
||
| 74 | * @return Filter |
||
| 75 | * @throws \InvalidArgumentException |
||
| 76 | */ |
||
| 77 | private function createFilter(string $key, $value, UriInterface $baseUri, bool $applied, array $options): Filter |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $key |
||
| 131 | * @param array $filters |
||
| 132 | * @param UriInterface $baseUri |
||
| 133 | * @param bool $applied |
||
| 134 | * @param array $options |
||
| 135 | * @return Filter |
||
| 136 | * @throws \InvalidArgumentException |
||
| 137 | */ |
||
| 138 | private function createCompositeFilter(string $key, array $filters, UriInterface $baseUri, bool $applied, array $options): Filter |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $key |
||
| 148 | * @param $left |
||
| 149 | * @param $right |
||
| 150 | * @param UriInterface $baseUri |
||
| 151 | * @param bool $applied |
||
| 152 | * @param array $options |
||
| 153 | * @return Filter |
||
| 154 | */ |
||
| 155 | private function createRangeFilter(string $key, $left, $right, UriInterface $baseUri, bool $applied, array $options): Filter |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $key |
||
| 167 | * @param $value |
||
| 168 | * @param UriInterface $baseUri |
||
| 169 | * @param bool $applied |
||
| 170 | * @param array $options |
||
| 171 | * @return Filter |
||
| 172 | */ |
||
| 173 | private function createSimpleFilter(string $key, $value, UriInterface $baseUri, bool $applied, array $options): Filter |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $key |
||
| 188 | * @param $value |
||
| 189 | * @param $operator |
||
| 190 | * @param UriInterface $baseUri |
||
| 191 | * @param bool $applied |
||
| 192 | * @param array $options |
||
| 193 | * @return Filter |
||
| 194 | * @throws \InvalidArgumentException |
||
| 195 | */ |
||
| 196 | private function createStringMatchFilter($key, $value, $operator, UriInterface $baseUri, bool $applied, array $options): Filter |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $key |
||
| 213 | * @param array $values |
||
| 214 | * @param UriInterface $baseUri |
||
| 215 | * @param bool $applied |
||
| 216 | * @param array $options |
||
| 217 | * @return Filter |
||
| 218 | * @throws \InvalidArgumentException |
||
| 219 | */ |
||
| 220 | private function createCollectionFilter(string $key, array $values, UriInterface $baseUri, bool $applied, array $options): Filter |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param $value |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | private function hasNegation($value): bool |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $value |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | private function hasSatisfiedByClause($value, &$satisfiedClause = null): bool |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $value |
||
| 257 | * @param null $operator |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | private function hasMatchOperator($value, &$operator = null): bool |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param $value |
||
| 276 | * @param null $matches |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | private function isRangeFilter($value, &$matches = null): bool |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $values |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | private function containsRangeFilter(array $values): bool |
||
| 298 | } |
||
| 299 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.