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 |
||
38 | class PageManager extends TrackingManager |
||
39 | { |
||
40 | /** @var StaticPageView[] A place to store a reference to static PageViews with titles. */ |
||
41 | private $staticPages; |
||
42 | /** @var RepeaterPageView[] A place to store a reference to repeater PageViews with titles. */ |
||
43 | private $repeaterPages; |
||
44 | private $configuration; |
||
45 | private $collectionManager; |
||
46 | private $dataManager; |
||
47 | private $eventDispatcher; |
||
48 | private $logger; |
||
49 | /** @var AssetManager */ |
||
50 | private $assetManager; |
||
51 | |||
52 | /** |
||
53 | * PageManager constructor. |
||
54 | */ |
||
55 | 20 | public function __construct( |
|
56 | Configuration $configuration, |
||
57 | CollectionManager $collectionManager, |
||
58 | DataManager $dataManager, |
||
59 | AssetManager $assetManager, |
||
60 | EventDispatcherInterface $eventDispatcher, |
||
61 | LoggerInterface $logger |
||
62 | ) { |
||
63 | 20 | $this->trackedItems = [ |
|
64 | 20 | BasePageView::STATIC_TYPE => [], |
|
65 | 20 | BasePageView::DYNAMIC_TYPE => [], |
|
66 | 20 | BasePageView::REPEATER_TYPE => [], |
|
67 | ]; |
||
68 | 20 | $this->staticPages = []; |
|
69 | 20 | $this->repeaterPages = []; |
|
70 | 20 | $this->configuration = $configuration; |
|
71 | 20 | $this->collectionManager = $collectionManager; |
|
72 | 20 | $this->dataManager = $dataManager; |
|
73 | 20 | $this->eventDispatcher = $eventDispatcher; |
|
74 | 20 | $this->logger = $logger; |
|
75 | 20 | $this->assetManager = $assetManager; |
|
76 | 20 | } |
|
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 7 | public function compileManager() |
|
85 | |||
86 | /** |
||
87 | * Go through all of the PageView directories and create a respective PageView for each and classify them by type. |
||
88 | * |
||
89 | * @param string[] $pageViewFolders |
||
90 | * |
||
91 | * @since 0.1.0 |
||
92 | */ |
||
93 | 19 | public function parsePageViews(array $pageViewFolders) |
|
115 | |||
116 | /** |
||
117 | * Get all of the PageViews in an associative array with PageView types as the keys. |
||
118 | * |
||
119 | * @since 0.1.1 |
||
120 | * |
||
121 | * @return BasePageView[][] |
||
122 | */ |
||
123 | 1 | public function &getPageViews() |
|
127 | |||
128 | /** |
||
129 | * Get all of the PageViews in flat array. |
||
130 | * |
||
131 | * @since 0.1.1 |
||
132 | * |
||
133 | * @return BasePageView[] |
||
134 | */ |
||
135 | 16 | public function &getPageViewsFlattened() |
|
139 | |||
140 | /** |
||
141 | * Get the static PageViews tracked by this manager indexed by their title. |
||
142 | * |
||
143 | * @since 0.1.0 |
||
144 | * |
||
145 | * @return StaticPageView[] |
||
146 | */ |
||
147 | 1 | public function getStaticPageViews() |
|
151 | |||
152 | /** |
||
153 | * Get the jailed version of the static PageViews indexed by their title. |
||
154 | * |
||
155 | * @since 0.1.0 |
||
156 | * |
||
157 | * @return JailedDocument[] |
||
158 | */ |
||
159 | 13 | public function getJailedStaticPageViews() |
|
170 | |||
171 | /** |
||
172 | * Get the jailed version of repeater PageViews indexed by their title. |
||
173 | */ |
||
174 | 12 | public function getJailedRepeaterPageViews() |
|
185 | |||
186 | /** |
||
187 | * Add a PageView for this PageManager to track. |
||
188 | * |
||
189 | * @param BasePageView|StaticPageView|DynamicPageView|RepeaterPageView $pageView |
||
190 | * |
||
191 | * @throws \LogicException When a PageView is treated as the wrong type |
||
192 | * @throws CollectionNotFoundException When the collection specified in a Dynamic PageView isn't found |
||
193 | * @throws \Exception The permalink could not be built correctly |
||
194 | * |
||
195 | * @since 0.2.0 |
||
196 | */ |
||
197 | 19 | public function trackNewPageView(BasePageView $pageView) |
|
198 | { |
||
199 | 19 | $namespace = $pageView->getType(); |
|
200 | |||
201 | switch ($namespace) |
||
202 | { |
||
203 | 19 | case BasePageView::STATIC_TYPE: |
|
204 | 12 | $this->handleTrackableStaticPageView($pageView); |
|
|
|||
205 | 12 | break; |
|
206 | |||
207 | 7 | case BasePageView::DYNAMIC_TYPE: |
|
208 | 4 | $this->handleTrackableDynamicPageView($pageView); |
|
209 | 3 | break; |
|
210 | |||
211 | 3 | case BasePageView::REPEATER_TYPE: |
|
212 | 3 | $this->handleTrackableRepeaterPageView($pageView); |
|
213 | 3 | break; |
|
214 | |||
215 | default: |
||
216 | break; |
||
217 | } |
||
218 | |||
219 | 18 | $event = new PageViewAdded($pageView); |
|
220 | 18 | $this->eventDispatcher->dispatch(PageViewAdded::NAME, $event); |
|
221 | |||
222 | 18 | $this->declareTrackingNamespace($namespace); |
|
223 | 18 | $this->addObjectToTracker($pageView, $namespace); |
|
224 | 18 | } |
|
225 | |||
226 | /** |
||
227 | * Add a new ContentItem to the respective parent PageView of the ContentItem. |
||
228 | * |
||
229 | * @param ContentItem $contentItem |
||
230 | * |
||
231 | * @since 0.1.0 |
||
232 | */ |
||
233 | 1 | public function trackNewContentItem(&$contentItem) |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 18 | protected function &handleTrackableItem(File $filePath, array $options = []) |
|
252 | |||
253 | /** |
||
254 | * Handle special behavior and treatment for static PageViews while we're iterating through them. |
||
255 | * |
||
256 | * @param StaticPageView $pageView |
||
257 | * |
||
258 | * @since 0.1.0 |
||
259 | */ |
||
260 | 12 | View Code Duplication | private function handleTrackableStaticPageView(&$pageView) |
273 | |||
274 | /** |
||
275 | * Handle special behavior and treatment for dynamic PageViews while we're iterating through them. |
||
276 | * |
||
277 | * @param DynamicPageView $pageView |
||
278 | * |
||
279 | * @since 0.1.0 |
||
280 | * |
||
281 | * @throws \LogicException An invalid PageView has been given as a Dynamic PageView |
||
282 | * @throws CollectionNotFoundException When a collection or dataset specified in a Dynamic PageView doesn't exist |
||
283 | * @throws \Exception When the permalink for the given PageView hasn't been set |
||
284 | */ |
||
285 | 4 | private function handleTrackableDynamicPageView(&$pageView) |
|
334 | |||
335 | /** |
||
336 | * Handle special behavior and treatment for repeater PageViews while we're iterating through them. |
||
337 | * |
||
338 | * @param RepeaterPageView $pageView |
||
339 | * |
||
340 | * @since 0.2.0 |
||
341 | */ |
||
342 | 3 | View Code Duplication | private function handleTrackableRepeaterPageView(&$pageView) |
356 | |||
357 | 3 | private function registerExplicitAssets(ContentItem $contentItem) |
|
393 | } |
||
394 |
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.