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:
Complex classes like Context 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 Context, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class Context extends ApiContext |
||
31 | { |
||
32 | /** FQCN of an entity */ |
||
33 | const CLASS_NAME = 'class'; |
||
34 | |||
35 | /** a prefix for all configuration sections */ |
||
36 | const CONFIG_PREFIX = 'config_'; |
||
37 | |||
38 | /** a list of requests for configuration data */ |
||
39 | const CONFIG_EXTRAS = 'configExtras'; |
||
40 | |||
41 | /** metadata of an entity */ |
||
42 | const METADATA = 'metadata'; |
||
43 | |||
44 | /** a list of requests for additional metadata info */ |
||
45 | const METADATA_EXTRAS = 'metadataExtras'; |
||
46 | |||
47 | /** a query is used to get result data */ |
||
48 | const QUERY = 'query'; |
||
49 | |||
50 | /** the Criteria object is used to add additional restrictions to a query is used to get result data */ |
||
51 | const CRITERIA = 'criteria'; |
||
52 | |||
53 | /** the response status code */ |
||
54 | const RESPONSE_STATUS_CODE = 'responseStatusCode'; |
||
55 | |||
56 | /** |
||
57 | * this header can be used to request additional data like "total count" |
||
58 | * that will be returned in a response headers |
||
59 | */ |
||
60 | const INCLUDE_HEADER = 'X-Include'; |
||
61 | |||
62 | /** a list of filters is used to add additional restrictions to a query is used to get result data */ |
||
63 | const FILTERS = 'filters'; |
||
64 | |||
65 | /** @var FilterValueAccessorInterface */ |
||
66 | private $filterValues; |
||
67 | |||
68 | /** @var Error[] */ |
||
69 | private $errors; |
||
70 | |||
71 | /** @var ConfigProvider */ |
||
72 | protected $configProvider; |
||
73 | |||
74 | /** @var MetadataProvider */ |
||
75 | protected $metadataProvider; |
||
76 | |||
77 | /** @var ParameterBagInterface */ |
||
78 | private $requestHeaders; |
||
79 | |||
80 | /** @var ParameterBagInterface */ |
||
81 | private $responseHeaders; |
||
82 | |||
83 | /** |
||
84 | * @param ConfigProvider $configProvider |
||
85 | * @param MetadataProvider $metadataProvider |
||
86 | */ |
||
87 | public function __construct(ConfigProvider $configProvider, MetadataProvider $metadataProvider) |
||
88 | { |
||
89 | parent::__construct(); |
||
90 | $this->configProvider = $configProvider; |
||
91 | $this->metadataProvider = $metadataProvider; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Checks whether a configuration of filters for an entity exists. |
||
96 | * |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function hasConfigOfFilters() |
||
103 | |||
104 | /** |
||
105 | * Gets a configuration of filters for an entity. |
||
106 | * |
||
107 | * @return FiltersConfig|null |
||
108 | */ |
||
109 | public function getConfigOfFilters() |
||
113 | |||
114 | /** |
||
115 | * Sets a configuration of filters for an entity. |
||
116 | * |
||
117 | * @param FiltersConfig|null $config |
||
118 | */ |
||
119 | public function setConfigOfFilters(FiltersConfig $config = null) |
||
123 | |||
124 | /** |
||
125 | * Checks whether a configuration of sorters for an entity exists. |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function hasConfigOfSorters() |
||
133 | |||
134 | /** |
||
135 | * Gets a configuration of sorters for an entity. |
||
136 | * |
||
137 | * @return SortersConfig|null |
||
138 | */ |
||
139 | public function getConfigOfSorters() |
||
143 | |||
144 | /** |
||
145 | * Sets a configuration of sorters for an entity. |
||
146 | * |
||
147 | * @param SortersConfig|null $config |
||
148 | */ |
||
149 | public function setConfigOfSorters(SortersConfig $config = null) |
||
153 | |||
154 | /** |
||
155 | * Gets a list of filters is used to add additional restrictions to a query is used to get result data. |
||
156 | * |
||
157 | * @return FilterCollection |
||
158 | */ |
||
159 | public function getFilters() |
||
167 | |||
168 | /** |
||
169 | * Gets a collection of the FilterValue objects that contains all incoming filters. |
||
170 | * |
||
171 | * @return FilterValueAccessorInterface |
||
172 | */ |
||
173 | public function getFilterValues() |
||
181 | |||
182 | /** |
||
183 | * Sets an object that will be used to accessing incoming filters. |
||
184 | * |
||
185 | * @param FilterValueAccessorInterface $accessor |
||
186 | */ |
||
187 | public function setFilterValues(FilterValueAccessorInterface $accessor) |
||
191 | |||
192 | /** |
||
193 | * Gets a key of a main section of an entity configuration. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function getConfigKey() |
||
201 | |||
202 | /** |
||
203 | * Loads an entity configuration. |
||
204 | */ |
||
205 | protected function loadConfig() |
||
237 | |||
238 | /** |
||
239 | * Makes sure that all config sections are added to the context. |
||
240 | */ |
||
241 | protected function ensureAllConfigSectionsSet() |
||
253 | |||
254 | /** |
||
255 | * Loads an entity metadata. |
||
256 | */ |
||
257 | protected function loadMetadata() |
||
278 | |||
279 | /** |
||
280 | * Gets request headers. |
||
281 | * |
||
282 | * @return ParameterBagInterface |
||
283 | */ |
||
284 | public function getRequestHeaders() |
||
292 | |||
293 | /** |
||
294 | * Sets an object that will be used to accessing request headers. |
||
295 | * |
||
296 | * @param ParameterBagInterface $parameterBag |
||
297 | */ |
||
298 | public function setRequestHeaders(ParameterBagInterface $parameterBag) |
||
302 | |||
303 | /** |
||
304 | * Gets response headers. |
||
305 | * |
||
306 | * @return ParameterBagInterface |
||
307 | */ |
||
308 | public function getResponseHeaders() |
||
316 | |||
317 | /** |
||
318 | * Sets an object that will be used to accessing response headers. |
||
319 | * |
||
320 | * @param ParameterBagInterface $parameterBag |
||
321 | */ |
||
322 | public function setResponseHeaders(ParameterBagInterface $parameterBag) |
||
326 | |||
327 | /** |
||
328 | * Gets the response status code. |
||
329 | * |
||
330 | * @return int|null |
||
331 | */ |
||
332 | public function getResponseStatusCode() |
||
336 | |||
337 | /** |
||
338 | * Sets the response status code. |
||
339 | * |
||
340 | * @param $statusCode |
||
341 | */ |
||
342 | public function setResponseStatusCode($statusCode) |
||
346 | |||
347 | /** |
||
348 | * Gets FQCN of an entity. |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | public function getClassName() |
||
356 | |||
357 | /** |
||
358 | * Sets FQCN of an entity. |
||
359 | * |
||
360 | * @param string $className |
||
361 | */ |
||
362 | public function setClassName($className) |
||
366 | |||
367 | /** |
||
368 | * Checks whether a configuration of an entity exists. |
||
369 | * |
||
370 | * @return bool |
||
371 | */ |
||
372 | public function hasConfig() |
||
376 | |||
377 | /** |
||
378 | * Gets a configuration of an entity. |
||
379 | * |
||
380 | * @return EntityDefinitionConfig|null |
||
381 | */ |
||
382 | public function getConfig() |
||
391 | |||
392 | /** |
||
393 | * Sets a configuration of an entity. |
||
394 | * |
||
395 | * @param EntityDefinitionConfig|null $definition |
||
396 | */ |
||
397 | public function setConfig(EntityDefinitionConfig $definition = null) |
||
404 | |||
405 | /** |
||
406 | * Checks whether a configuration of the given section exists. |
||
407 | * |
||
408 | * @param string $configSection |
||
409 | * |
||
410 | * @return bool |
||
411 | * |
||
412 | * @throws \InvalidArgumentException if undefined configuration section is specified |
||
413 | */ |
||
414 | public function hasConfigOf($configSection) |
||
420 | |||
421 | /** |
||
422 | * Gets a configuration from the given section. |
||
423 | * |
||
424 | * @param string $configSection |
||
425 | * |
||
426 | * @return mixed |
||
427 | * |
||
428 | * @throws \InvalidArgumentException if undefined configuration section is specified |
||
429 | */ |
||
430 | View Code Duplication | public function getConfigOf($configSection) |
|
445 | |||
446 | /** |
||
447 | * Sets a configuration for the given section. |
||
448 | * |
||
449 | * @param string $configSection |
||
450 | * @param mixed $config |
||
451 | * |
||
452 | * @throws \InvalidArgumentException if undefined configuration section is specified |
||
453 | */ |
||
454 | View Code Duplication | public function setConfigOf($configSection, $config) |
|
467 | |||
468 | /** |
||
469 | * Gets a list of requests for configuration data. |
||
470 | * |
||
471 | * @return ConfigExtraInterface[] |
||
472 | */ |
||
473 | public function getConfigExtras() |
||
481 | |||
482 | /** |
||
483 | * Sets a list of requests for configuration data. |
||
484 | * |
||
485 | * @param ConfigExtraInterface[] $extras |
||
486 | * |
||
487 | * @throws \InvalidArgumentException if $extras has invalid elements |
||
488 | */ |
||
489 | View Code Duplication | public function setConfigExtras(array $extras) |
|
505 | |||
506 | /** |
||
507 | * Checks whether some configuration data is requested. |
||
508 | * |
||
509 | * @param string $extraName |
||
510 | * |
||
511 | * @return bool |
||
512 | */ |
||
513 | public function hasConfigExtra($extraName) |
||
524 | |||
525 | /** |
||
526 | * Adds a request for some configuration data. |
||
527 | * |
||
528 | * @param ConfigExtraInterface $extra |
||
529 | * |
||
530 | * @throws \InvalidArgumentException if a config extra with the same name already exists |
||
531 | */ |
||
532 | public function addConfigExtra(ConfigExtraInterface $extra) |
||
543 | |||
544 | /** |
||
545 | * Removes a request for some configuration data. |
||
546 | * |
||
547 | * @param string $extraName |
||
548 | */ |
||
549 | public function removeConfigExtra($extraName) |
||
560 | |||
561 | /** |
||
562 | * Checks whether metadata of an entity exists. |
||
563 | * |
||
564 | * @return bool |
||
565 | */ |
||
566 | public function hasMetadata() |
||
570 | |||
571 | /** |
||
572 | * Gets metadata of an entity. |
||
573 | * |
||
574 | * @return EntityMetadata|null |
||
575 | */ |
||
576 | public function getMetadata() |
||
584 | |||
585 | /** |
||
586 | * Sets metadata of an entity. |
||
587 | * |
||
588 | * @param EntityMetadata|null $metadata |
||
589 | */ |
||
590 | public function setMetadata(EntityMetadata $metadata = null) |
||
594 | |||
595 | /** |
||
596 | * Gets a list of requests for additional metadata info. |
||
597 | * |
||
598 | * @return MetadataExtraInterface[] |
||
599 | */ |
||
600 | public function getMetadataExtras() |
||
608 | |||
609 | /** |
||
610 | * Sets a list of requests for additional metadata info. |
||
611 | * |
||
612 | * @param MetadataExtraInterface[] $extras |
||
613 | * |
||
614 | * @throws \InvalidArgumentException if $extras has invalid elements |
||
615 | */ |
||
616 | View Code Duplication | public function setMetadataExtras(array $extras) |
|
632 | |||
633 | /** |
||
634 | * Checks whether some additional metadata info is requested. |
||
635 | * |
||
636 | * @param string $extraName |
||
637 | * |
||
638 | * @return bool |
||
639 | */ |
||
640 | public function hasMetadataExtra($extraName) |
||
651 | |||
652 | /** |
||
653 | * Adds a request for some additional metadata info. |
||
654 | * |
||
655 | * @param MetadataExtraInterface $extra |
||
656 | * |
||
657 | * @throws \InvalidArgumentException if a metadata extra with the same name already exists |
||
658 | */ |
||
659 | public function addMetadataExtra(MetadataExtraInterface $extra) |
||
670 | |||
671 | /** |
||
672 | * Removes a request for some additional metadata info. |
||
673 | * |
||
674 | * @param string $extraName |
||
675 | */ |
||
676 | public function removeMetadataExtra($extraName) |
||
687 | |||
688 | /** |
||
689 | * Checks whether a query is used to get result data exists. |
||
690 | * |
||
691 | * @return bool |
||
692 | */ |
||
693 | public function hasQuery() |
||
697 | |||
698 | /** |
||
699 | * Gets a query is used to get result data. |
||
700 | * |
||
701 | * @return mixed |
||
702 | */ |
||
703 | public function getQuery() |
||
707 | |||
708 | /** |
||
709 | * Sets a query is used to get result data. |
||
710 | * |
||
711 | * @param mixed $query |
||
712 | */ |
||
713 | public function setQuery($query) |
||
717 | |||
718 | /** |
||
719 | * Gets the Criteria object is used to add additional restrictions to a query is used to get result data. |
||
720 | * |
||
721 | * @return Criteria |
||
722 | */ |
||
723 | public function getCriteria() |
||
727 | |||
728 | /** |
||
729 | * Sets the Criteria object is used to add additional restrictions to a query is used to get result data. |
||
730 | * |
||
731 | * @param Criteria $criteria |
||
732 | */ |
||
733 | public function setCriteria($criteria) |
||
737 | |||
738 | /** |
||
739 | * Whether any error happened during the processing of an action. |
||
740 | * |
||
741 | * @return bool |
||
742 | */ |
||
743 | public function hasErrors() |
||
747 | |||
748 | /** |
||
749 | * Gets all errors happened during the processing of an action. |
||
750 | * |
||
751 | * @return Error[] |
||
752 | */ |
||
753 | public function getErrors() |
||
759 | |||
760 | /** |
||
761 | * Registers an error. |
||
762 | * |
||
763 | * @param Error $error |
||
764 | */ |
||
765 | public function addError(Error $error) |
||
772 | |||
773 | /** |
||
774 | * Removes all errors. |
||
775 | */ |
||
776 | public function resetErrors() |
||
780 | |||
781 | /** |
||
782 | * @param string $configSection |
||
783 | * |
||
784 | * @throws \InvalidArgumentException if undefined configuration section is specified |
||
785 | */ |
||
786 | protected function assertConfigSection($configSection) |
||
800 | } |
||
801 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..