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:
| 1 | <?php |
||
| 36 | class PageTreeRouteSubscriber implements EventSubscriberInterface |
||
| 37 | { |
||
| 38 | const ROUTE_PROPERTY = 'routePath'; |
||
| 39 | const TAG_NAME = 'sulu_article.article_route'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var DocumentManagerInterface |
||
| 43 | */ |
||
| 44 | protected $documentManager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var PropertyEncoder |
||
| 48 | */ |
||
| 49 | protected $properyEncoder; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var DocumentInspector |
||
| 53 | */ |
||
| 54 | protected $documentInspector; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var StructureMetadataFactoryInterface |
||
| 58 | */ |
||
| 59 | protected $metadataFactory; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var SessionInterface |
||
| 63 | */ |
||
| 64 | protected $liveSession; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param DocumentManagerInterface $documentManager |
||
| 68 | * @param PropertyEncoder $properyEncoder |
||
| 69 | * @param DocumentInspector $documentInspector |
||
| 70 | * @param StructureMetadataFactoryInterface $metadataFactory |
||
| 71 | * @param SessionInterface $liveSession |
||
| 72 | */ |
||
| 73 | public function __construct( |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public static function getSubscribedEvents() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Update route-paths of articles which are linked to the given page-document. |
||
| 102 | * |
||
| 103 | * @param AbstractMappingEvent $event |
||
| 104 | */ |
||
| 105 | public function handlePublish(AbstractMappingEvent $event) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Update route-paths of articles which are linked to the given page-document. |
||
| 121 | * |
||
| 122 | * @param MoveEvent $event |
||
| 123 | */ |
||
| 124 | public function handleMove(MoveEvent $event) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Find articles linked to the given page. |
||
| 142 | * |
||
| 143 | * @param string $uuid |
||
| 144 | * @param string $locale |
||
| 145 | * |
||
| 146 | * @return ArticleInterface[] |
||
| 147 | */ |
||
| 148 | private function findLinkedArticles($uuid, $locale) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Update route of given article. |
||
| 179 | * |
||
| 180 | * @param ArticleDocument $article |
||
| 181 | * @param string $resourceSegment |
||
| 182 | * @param string $locale |
||
| 183 | */ |
||
| 184 | private function updateArticle(ArticleDocument $article, $resourceSegment, $locale) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns true if the resource-segment was changed in the draft page. |
||
| 206 | * |
||
| 207 | * @param PageDocument $document |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | private function hasChangedResourceSegment(PageDocument $document) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns "routePath" property. |
||
| 225 | * |
||
| 226 | * @param string $structureType |
||
| 227 | * |
||
| 228 | * @return PropertyMetadata |
||
| 229 | */ |
||
| 230 | View Code Duplication | private function getRoutePathPropertyNameByStructureType($structureType) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Returns "routePath" property. |
||
| 242 | * |
||
| 243 | * @param StructureMetadata $metadata |
||
| 244 | * |
||
| 245 | * @return PropertyMetadata |
||
| 246 | */ |
||
| 247 | private function getRoutePathPropertyName(StructureMetadata $metadata) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Returns the live node for given document. |
||
| 262 | * |
||
| 263 | * @param PathBehavior $document |
||
| 264 | * |
||
| 265 | * @return NodeInterface |
||
| 266 | */ |
||
| 267 | private function getLiveNode(PathBehavior $document) |
||
| 271 | } |
||
| 272 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.