Complex classes like RoutableSubscriber 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 RoutableSubscriber, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class RoutableSubscriber implements EventSubscriberInterface |
||
| 42 | { |
||
| 43 | const ROUTE_FIELD = 'routePath'; |
||
| 44 | |||
| 45 | const ROUTES_PROPERTY = 'suluRoutes'; |
||
| 46 | |||
| 47 | const TAG_NAME = 'sulu_article.article_route'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var ChainRouteGeneratorInterface |
||
| 51 | */ |
||
| 52 | private $chainRouteGenerator; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var RouteManagerInterface |
||
| 56 | */ |
||
| 57 | private $routeManager; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var RouteRepositoryInterface |
||
| 61 | */ |
||
| 62 | private $routeRepository; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var EntityManagerInterface |
||
| 66 | */ |
||
| 67 | private $entityManager; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var DocumentManagerInterface |
||
| 71 | */ |
||
| 72 | private $documentManager; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var DocumentInspector |
||
| 76 | */ |
||
| 77 | private $documentInspector; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var PropertyEncoder |
||
| 81 | */ |
||
| 82 | private $propertyEncoder; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var StructureMetadataFactoryInterface |
||
| 86 | */ |
||
| 87 | private $metadataFactory; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var ConflictResolverInterface |
||
| 91 | */ |
||
| 92 | private $conflictResolver; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param ChainRouteGeneratorInterface $chainRouteGenerator |
||
| 96 | * @param RouteManagerInterface $routeManager |
||
| 97 | * @param RouteRepositoryInterface $routeRepository |
||
| 98 | * @param EntityManagerInterface $entityManager |
||
| 99 | * @param DocumentManagerInterface $documentManager |
||
| 100 | * @param DocumentInspector $documentInspector |
||
| 101 | * @param PropertyEncoder $propertyEncoder |
||
| 102 | * @param StructureMetadataFactoryInterface $metadataFactory |
||
| 103 | * @param ConflictResolverInterface $conflictResolver |
||
| 104 | */ |
||
| 105 | 62 | public function __construct( |
|
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | 55 | public static function getSubscribedEvents() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Generate route and save route-path. |
||
| 149 | * |
||
| 150 | * @param AbstractMappingEvent $event |
||
| 151 | */ |
||
| 152 | public function handlePersist(AbstractMappingEvent $event) |
||
| 162 | 54 | ||
| 163 | /** |
||
| 164 | 54 | * Regenerate routes for siblings on reorder. |
|
| 165 | 54 | * |
|
| 166 | 20 | * @param ReorderEvent $event |
|
| 167 | */ |
||
| 168 | 54 | public function handleReorder(ReorderEvent $event) |
|
| 182 | 55 | ||
| 183 | /** |
||
| 184 | 55 | * Handle publish event and generate route and the child-routes. |
|
| 185 | 55 | * |
|
| 186 | * @param PublishEvent $event |
||
| 187 | 55 | */ |
|
| 188 | 55 | public function handlePublish(PublishEvent $event) |
|
| 211 | 2 | ||
| 212 | 2 | /** |
|
| 213 | 2 | * Removes route. |
|
| 214 | * |
||
| 215 | 2 | * @param RemoveEvent $event |
|
| 216 | 2 | */ |
|
| 217 | public function handleRemove(RemoveEvent $event) |
||
| 231 | 24 | ||
| 232 | 24 | /** |
|
| 233 | 9 | * Update routes for copied article. |
|
| 234 | * |
||
| 235 | * @param CopyEvent $event |
||
| 236 | 18 | */ |
|
| 237 | public function handleCopy(CopyEvent $event) |
||
| 253 | |||
| 254 | /** |
||
| 255 | 18 | * Create or update for given document. |
|
| 256 | 18 | * |
|
| 257 | * @param RoutablePageBehavior $document |
||
| 258 | 18 | * @param string $locale |
|
| 259 | 18 | * |
|
| 260 | * @return RouteInterface |
||
| 261 | 18 | */ |
|
| 262 | private function createOrUpdatePageRoute(RoutablePageBehavior $document, $locale) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Reallocates existing route to given document. |
||
| 293 | * |
||
| 294 | * @param RoutablePageBehavior $document |
||
| 295 | * @param string $locale |
||
| 296 | * |
||
| 297 | * @return RouteInterface |
||
| 298 | */ |
||
| 299 | private function reallocateExistingRoute(RoutablePageBehavior $document, $locale) |
||
| 331 | 18 | ||
| 332 | private function updateRoute(RoutablePageBehavior $document) |
||
| 343 | 18 | ||
| 344 | private function updateChildRoutes(ChildrenBehavior $document) |
||
| 354 | |||
| 355 | 18 | /** |
|
| 356 | * Generates child routes. |
||
| 357 | * |
||
| 358 | * @param ChildrenBehavior $document |
||
| 359 | * @param string $locale |
||
| 360 | * |
||
| 361 | * @return string[] |
||
| 362 | 18 | */ |
|
| 363 | 18 | private function generateChildRoutes(ChildrenBehavior $document, $locale) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Removes old-routes where the node does not exists anymore. |
||
| 388 | * |
||
| 389 | * @param SessionInterface $session |
||
| 390 | * @param array $oldRoutes |
||
| 391 | * @param string $locale |
||
| 392 | */ |
||
| 393 | 18 | private function removeOldChildRoutes(SessionInterface $session, array $oldRoutes, $locale) |
|
| 404 | 6 | ||
| 405 | 2 | /** |
|
| 406 | * Iterate over children and remove routes. |
||
| 407 | * |
||
| 408 | 4 | * @param ChildrenBehavior $document |
|
| 409 | 4 | * @param string $locale |
|
| 410 | 4 | */ |
|
| 411 | private function removeChildRoutes(ChildrenBehavior $document, $locale) |
||
| 423 | 2 | ||
| 424 | 2 | /** |
|
| 425 | * Removes route if exists. |
||
| 426 | * |
||
| 427 | * @param RoutablePageBehavior $document |
||
| 428 | 4 | * @param string $locale |
|
| 429 | 4 | */ |
|
| 430 | private function removeChildRoute(RoutablePageBehavior $document, $locale) |
||
| 437 | |||
| 438 | 1 | /** |
|
| 439 | 1 | * Returns encoded "routePath" property-name. |
|
| 440 | * |
||
| 441 | * @param string $structureType |
||
| 442 | * @param string $locale |
||
| 443 | 1 | * |
|
| 444 | 1 | * @return string |
|
| 445 | 1 | */ |
|
| 446 | private function getRoutePathPropertyName($structureType, $locale) |
||
| 456 | 1 | ||
| 457 | 1 | /** |
|
| 458 | 1 | * Returns encoded property-name. |
|
| 459 | * |
||
| 460 | 1 | * @param string $locale |
|
| 461 | 1 | * @param string $field |
|
| 462 | * |
||
| 463 | * @return string |
||
| 464 | 1 | */ |
|
| 465 | private function getPropertyName($locale, $field) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns true if given uuid exists. |
||
| 472 | 1 | * |
|
| 473 | * @param SessionInterface $session |
||
| 474 | 1 | * @param string $uuid |
|
| 475 | 1 | * |
|
| 476 | 1 | * @return bool |
|
| 477 | */ |
||
| 478 | private function nodeExists(SessionInterface $session, $uuid) |
||
| 488 | } |
||
| 489 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.