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 AnnotationManager 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 AnnotationManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class AnnotationManager |
||
20 | { |
||
21 | const MEMBER_CLASS = 'class'; |
||
22 | |||
23 | const MEMBER_PROPERTY = 'property'; |
||
24 | |||
25 | const MEMBER_METHOD = 'method'; |
||
26 | |||
27 | /** |
||
28 | * @var boolean Enable PHP autoloader when searching for annotation classes (defaults to true) |
||
29 | */ |
||
30 | public $autoload = true; |
||
31 | |||
32 | /** |
||
33 | * @var string The class-name suffix for Annotation classes. |
||
34 | */ |
||
35 | public $suffix = 'Annotation'; |
||
36 | |||
37 | /** |
||
38 | * @var string The default namespace for annotations with no namespace qualifier. |
||
39 | */ |
||
40 | public $namespace = ''; |
||
41 | |||
42 | /** |
||
43 | * @var AnnotationCache|bool a cache-provider used to store annotation-data after parsing; or false to disable caching |
||
44 | * @see getAnnotationData() |
||
45 | */ |
||
46 | public $cache; |
||
47 | |||
48 | /** |
||
49 | * @var array List of registered annotation aliases. |
||
50 | */ |
||
51 | public $registry = array( |
||
52 | 'api' => false, |
||
53 | 'abstract' => false, |
||
54 | 'access' => false, |
||
55 | 'author' => false, |
||
56 | 'category' => false, |
||
57 | 'copyright' => false, |
||
58 | 'deprecated' => false, |
||
59 | 'example' => false, |
||
60 | 'filesource' => false, |
||
61 | 'final' => false, |
||
62 | 'global' => false, |
||
63 | 'ignore' => false, |
||
64 | 'internal' => false, |
||
65 | 'license' => false, |
||
66 | 'link' => false, |
||
67 | 'method' => 'mindplay\annotations\standard\MethodAnnotation', |
||
68 | 'name' => false, |
||
69 | 'package' => false, |
||
70 | 'param' => 'mindplay\annotations\standard\ParamAnnotation', |
||
71 | 'property' => 'mindplay\annotations\standard\PropertyAnnotation', |
||
72 | 'property-read' => 'mindplay\annotations\standard\PropertyReadAnnotation', |
||
73 | 'property-write' => 'mindplay\annotations\standard\PropertyWriteAnnotation', |
||
74 | 'return' => 'mindplay\annotations\standard\ReturnAnnotation', |
||
75 | 'see' => false, |
||
76 | 'since' => false, |
||
77 | 'source' => false, |
||
78 | 'static' => false, |
||
79 | 'staticvar' => false, |
||
80 | 'subpackage' => false, |
||
81 | 'todo' => false, |
||
82 | 'tutorial' => false, |
||
83 | 'throws' => false, |
||
84 | 'type' => 'mindplay\annotations\standard\TypeAnnotation', |
||
85 | 'usage' => 'mindplay\annotations\UsageAnnotation', |
||
86 | 'stop' => 'mindplay\annotations\StopAnnotation', |
||
87 | 'uses' => false, |
||
88 | 'var' => 'mindplay\annotations\standard\VarAnnotation', |
||
89 | 'version' => false, |
||
90 | ); |
||
91 | |||
92 | /** |
||
93 | * @var boolean $debug Set to TRUE to enable HTML output for debugging |
||
94 | */ |
||
95 | public $debug = false; |
||
96 | |||
97 | /** |
||
98 | * @var AnnotationParser |
||
99 | */ |
||
100 | protected $parser; |
||
101 | |||
102 | /** |
||
103 | * An internal cache for annotation-data loaded from source-code files |
||
104 | * |
||
105 | * @var AnnotationFile[] hash where absolute path to php source-file => AnnotationFile instance |
||
106 | */ |
||
107 | protected $files = array(); |
||
108 | |||
109 | /** |
||
110 | * @var array[] An internal cache for Annotation instances |
||
111 | * @see getAnnotations() |
||
112 | */ |
||
113 | protected $annotations = array(); |
||
114 | |||
115 | /** |
||
116 | * @var bool[] An array of flags indicating which annotation sets have been initialized |
||
117 | * @see getAnnotations() |
||
118 | */ |
||
119 | protected $initialized = array(); |
||
120 | |||
121 | /** |
||
122 | * @var UsageAnnotation[] An internal cache for UsageAnnotation instances |
||
123 | */ |
||
124 | protected $usage = array(); |
||
125 | |||
126 | /** |
||
127 | * @var UsageAnnotation The standard UsageAnnotation |
||
128 | */ |
||
129 | private $_usageAnnotation; |
||
130 | |||
131 | /** |
||
132 | * @var string a seed for caching - used when generating cache keys, to prevent collisions |
||
133 | * when using more than one AnnotationManager in the same application. |
||
134 | */ |
||
135 | private $_cacheSeed = ''; |
||
136 | |||
137 | /** |
||
138 | * Whether this version of PHP has support for traits. |
||
139 | */ |
||
140 | private $_traitsSupported; |
||
141 | |||
142 | 8 | /** |
|
143 | * Initialize the Annotation Manager |
||
144 | 8 | * |
|
145 | 8 | * @param string $cacheSeed only needed if using more than one AnnotationManager in the same application |
|
146 | 8 | */ |
|
147 | 8 | public function __construct($cacheSeed = '') |
|
155 | |||
156 | 10 | /** |
|
157 | 6 | * Creates and returns the AnnotationParser instance |
|
158 | 6 | * @return AnnotationParser |
|
159 | 6 | */ |
|
160 | 6 | public function getParser() |
|
170 | |||
171 | /** |
||
172 | * Retrieves annotation-data from a given source-code file. |
||
173 | * |
||
174 | * Member-names in the returned array have the following format: Class, Class::method or Class::$member |
||
175 | * |
||
176 | * @param string $path the path of the source-code file from which to obtain annotation-data. |
||
177 | * @return AnnotationFile |
||
178 | 29 | * |
|
179 | * @throws AnnotationException if cache is not configured |
||
180 | 29 | * |
|
181 | 10 | * @see $files |
|
182 | 1 | * @see $cache |
|
183 | */ |
||
184 | protected function getAnnotationFile($path) |
||
211 | |||
212 | /** |
||
213 | * Resolves a name, using built-in annotation name resolution rules, and the registry. |
||
214 | * |
||
215 | * @param string $name the annotation-name |
||
216 | 18 | * |
|
217 | * @return string|bool The fully qualified annotation class-name, or false if the |
||
218 | 18 | * requested annotation has been disabled (set to false) in the registry. |
|
219 | 2 | * |
|
220 | * @see $registry |
||
221 | */ |
||
222 | 17 | public function resolveName($name) |
|
240 | |||
241 | /** |
||
242 | * Constructs, initializes and returns IAnnotation objects |
||
243 | * |
||
244 | * @param string $class_name The name of the class from which to obtain Annotations |
||
245 | 37 | * @param string $member_type The type of member, e.g. "class", "property" or "method" |
|
246 | * @param string $member_name Optional member name, e.g. "method" or "$property" |
||
247 | 37 | * |
|
248 | * @return IAnnotation[] array of IAnnotation objects for the given class/member/name |
||
249 | 37 | * @throws AnnotationException for bad annotations |
|
250 | 28 | */ |
|
251 | 28 | protected function getAnnotations($class_name, $member_type = self::MEMBER_CLASS, $member_name = null) |
|
347 | |||
348 | /** |
||
349 | 24 | * Determines whether a class or trait has the specified member. |
|
350 | * |
||
351 | 16 | * @param string $className The name of the class or trait to check |
|
352 | 5 | * @param string $memberType The type of member, e.g. "property" or "method" |
|
353 | 3 | * @param string $memberName The member name, e.g. "method" or "$property" |
|
354 | * |
||
355 | * @return bool whether class or trait has the specified member |
||
356 | 2 | */ |
|
357 | 1 | protected function classHasMember($className, $memberType, $memberName) |
|
366 | |||
367 | 27 | /** |
|
368 | * Validates the constraints (as defined by the UsageAnnotation of each annotation) of a |
||
369 | * list of annotations for a given type of member. |
||
370 | * |
||
371 | * @param IAnnotation[] $annotations An array of IAnnotation objects to be validated (sorted with inherited annotations on top). |
||
372 | * @param string $member The type of member to validate against (e.g. "class", "property" or "method"). |
||
373 | * |
||
374 | * @return IAnnotation[] validated and filtered list of IAnnotations objects |
||
375 | * |
||
376 | * @throws AnnotationException if a constraint is violated. |
||
377 | */ |
||
378 | protected function applyConstraints(array $annotations, $member) |
||
412 | |||
413 | 28 | /** |
|
414 | 17 | * Filters annotations by class name |
|
415 | 1 | * |
|
416 | * @param IAnnotation[] $annotations An array of annotation objects |
||
417 | * @param string $type The class-name by which to filter annotation objects; or annotation |
||
418 | 16 | * type-name with a leading "@", e.g. "@var", which will be resolved through the registry |
|
419 | * |
||
420 | 16 | * @return array The filtered array of annotation objects - may return an empty array |
|
421 | 1 | */ |
|
422 | protected function filterAnnotations(array $annotations, $type) |
||
442 | |||
443 | /** |
||
444 | * Obtain the UsageAnnotation for a given Annotation class |
||
445 | * |
||
446 | 20 | * @param string $class The Annotation type class-name |
|
447 | * @return UsageAnnotation |
||
448 | 20 | * @throws AnnotationException if the given class-name is invalid; if the annotation-type has no defined usage |
|
449 | 1 | */ |
|
450 | 20 | public function getUsage($class) |
|
478 | |||
479 | /** |
||
480 | * Inspects Annotations applied to a given class |
||
481 | * |
||
482 | * @param string|object|\ReflectionClass $class A class name, an object, or a ReflectionClass instance |
||
483 | * @param string $type An optional annotation class/interface name - if specified, only annotations of the given type are returned. |
||
484 | 9 | * Alternatively, prefixing with "@" invokes name-resolution (allowing you to query by annotation name.) |
|
485 | * |
||
486 | 9 | * @return Annotation[] Annotation instances |
|
487 | 1 | * @throws AnnotationException if a given class-name is undefined |
|
488 | 9 | */ |
|
489 | 1 | public function getClassAnnotations($class, $type = null) |
|
515 | |||
516 | /** |
||
517 | * Inspects Annotations applied to a given method |
||
518 | * |
||
519 | * @param string|object|\ReflectionClass|\ReflectionMethod $class A class name, an object, a ReflectionClass, or a ReflectionMethod instance |
||
520 | * @param string $method The name of a method of the given class (or null, if the first parameter is a ReflectionMethod) |
||
521 | * @param string $type An optional annotation class/interface name - if specified, only annotations of the given type are returned. |
||
522 | * Alternatively, prefixing with "@" invokes name-resolution (allowing you to query by annotation name.) |
||
523 | * |
||
524 | 11 | * @throws AnnotationException for undefined method or class-name |
|
525 | * @return IAnnotation[] list of Annotation objects |
||
526 | 11 | */ |
|
527 | 1 | View Code Duplication | public function getMethodAnnotations($class, $method = null, $type = null) |
554 | |||
555 | /** |
||
556 | * Inspects Annotations applied to a given property |
||
557 | * |
||
558 | * @param string|object|\ReflectionClass|\ReflectionProperty $class A class name, an object, a ReflectionClass, or a ReflectionProperty instance |
||
559 | * @param string $property The name of a defined property of the given class (or null, if the first parameter is a ReflectionProperty) |
||
560 | * @param string $type An optional annotation class/interface name - if specified, only annotations of the given type are returned. |
||
561 | * Alternatively, prefixing with "@" invokes name-resolution (allowing you to query by annotation name.) |
||
562 | * |
||
563 | * @return IAnnotation[] list of Annotation objects |
||
564 | * |
||
565 | * @throws AnnotationException for undefined class-name |
||
566 | */ |
||
567 | View Code Duplication | public function getPropertyAnnotations($class, $property = null, $type = null) |
|
594 | } |
||
595 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.