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( |
|
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) |
|
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.