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 |
||
26 | class CollectionManager extends TrackingManager |
||
27 | { |
||
28 | /** @var string[][] A copy of the collection definitions to be available for later usage. */ |
||
29 | private $collectionDefinitions; |
||
30 | private $markupEngineManager; |
||
31 | private $configuration; |
||
32 | private $eventDispatcher; |
||
33 | private $templateBridge; |
||
34 | private $logger; |
||
35 | |||
36 | /** |
||
37 | * CollectionManager constructor. |
||
38 | */ |
||
39 | 36 | View Code Duplication | public function __construct(MarkupEngineManager $markupEngineManager, Configuration $configuration, TemplateBridgeInterface $templateBridge, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 6 | public function compileManager() |
|
62 | |||
63 | /** |
||
64 | * Get all of the ContentItems grouped by Collection name. |
||
65 | * |
||
66 | * ```php |
||
67 | * [ |
||
68 | * 'collection name' => [ |
||
69 | * new ContentItem(), |
||
70 | * new ContentItem(), |
||
71 | * ] |
||
72 | * ] |
||
73 | * ``` |
||
74 | * |
||
75 | * @return ContentItem[][] |
||
76 | */ |
||
77 | 15 | public function &getCollections() |
|
81 | |||
82 | /** |
||
83 | * Get a ContentItem from a Collection passed on it's path. |
||
84 | * |
||
85 | * @param string $filePath |
||
86 | * |
||
87 | * @throws TrackedItemNotFoundException |
||
88 | * |
||
89 | * @return ContentItem |
||
90 | */ |
||
91 | 1 | public function &getContentItem($filePath) |
|
100 | |||
101 | /** |
||
102 | * A jailed representation of CollectionManager::getCollections(). |
||
103 | * |
||
104 | * @return JailedDocument[][] |
||
105 | */ |
||
106 | public function getJailedCollections() |
||
112 | |||
113 | /** |
||
114 | * Parse every collection and store them in the manager. |
||
115 | * |
||
116 | * @param string[][] $collections An array of definitions for collections |
||
117 | */ |
||
118 | 36 | public function parseCollections($collections) |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | 1 | public function createNewItem($filePath) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function refreshItem($filePath) |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 36 | protected function handleTrackableItem(File $filePath, array $options = []) |
|
211 | |||
212 | /** |
||
213 | * Get the name of the Collection this ContentItem belongs to based on its location. |
||
214 | * |
||
215 | * @param File $file |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 1 | private function getCollectionNameFromPath(File $file) |
|
231 | } |
||
232 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.