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 PHPClass 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 PHPClass, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class PHPClass |
||
5 | { |
||
6 | |||
7 | protected $name; |
||
8 | |||
9 | protected $namespace; |
||
10 | |||
11 | protected $doc; |
||
12 | |||
13 | 16 | public static function createFromFQCN($className) |
|
21 | |||
22 | /** |
||
23 | * @param bool $onlyParent |
||
24 | * @return PHPProperty |
||
25 | */ |
||
26 | 8 | public function isSimpleType($onlyParent = false) |
|
41 | |||
42 | 8 | public function getPhpType() |
|
52 | |||
53 | 8 | View Code Duplication | public function isNativeType() |
66 | |||
67 | |||
68 | 30 | public function __construct($name = null, $namespace = null) |
|
73 | |||
74 | 24 | public function getName() |
|
78 | |||
79 | 30 | public function setName($name) |
|
84 | |||
85 | 24 | public function getNamespace() |
|
89 | |||
90 | 30 | public function setNamespace($namespace) |
|
95 | |||
96 | 8 | public function getDoc() |
|
100 | |||
101 | 30 | public function setDoc($doc) |
|
106 | |||
107 | public function __toString() |
||
111 | |||
112 | 30 | public function getFullName() |
|
116 | |||
117 | protected $checks = array(); |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * @var PHPConstant[] |
||
122 | */ |
||
123 | protected $constants = array(); |
||
124 | |||
125 | /** |
||
126 | * |
||
127 | * @var PHPProperty[] |
||
128 | */ |
||
129 | protected $properties = array(); |
||
130 | |||
131 | /** |
||
132 | * |
||
133 | * @param |
||
134 | * $property |
||
135 | * @return array |
||
136 | */ |
||
137 | public function getChecks($property) |
||
141 | |||
142 | /** |
||
143 | * |
||
144 | * @param |
||
145 | * $property |
||
146 | * @param |
||
147 | * $check |
||
148 | * @param |
||
149 | * $value |
||
150 | * @return $this |
||
151 | */ |
||
152 | 1 | public function addCheck($property, $check, $value) |
|
157 | |||
158 | /** |
||
159 | * |
||
160 | * @return PHPProperty[] |
||
161 | */ |
||
162 | 19 | public function getProperties() |
|
166 | |||
167 | /** |
||
168 | * |
||
169 | * @param string $name |
||
170 | * @return boolean |
||
171 | */ |
||
172 | 8 | public function hasProperty($name) |
|
176 | |||
177 | /** |
||
178 | * |
||
179 | * @param string $name |
||
180 | * @return bool |
||
181 | */ |
||
182 | 8 | public function hasPropertyInHierarchy($name) |
|
192 | |||
193 | /** |
||
194 | * |
||
195 | * @param string $name |
||
196 | * @return PHPProperty |
||
197 | */ |
||
198 | 2 | public function getPropertyInHierarchy($name) |
|
208 | |||
209 | /** |
||
210 | * |
||
211 | * @param string $name |
||
212 | * @return PHPProperty |
||
213 | */ |
||
214 | 2 | public function getPropertiesInHierarchy() |
|
224 | |||
225 | /** |
||
226 | * |
||
227 | * @param string $name |
||
228 | * @return PHPProperty |
||
229 | */ |
||
230 | 8 | public function getProperty($name) |
|
234 | |||
235 | /** |
||
236 | * |
||
237 | * @param PHPProperty $property |
||
238 | * @return $this |
||
239 | */ |
||
240 | 30 | public function addProperty(PHPProperty $property) |
|
245 | |||
246 | /** |
||
247 | * |
||
248 | * @var boolean |
||
249 | */ |
||
250 | protected $abstract; |
||
251 | |||
252 | /** |
||
253 | * |
||
254 | * @var PHPClass |
||
255 | */ |
||
256 | protected $extends; |
||
257 | |||
258 | /** |
||
259 | * |
||
260 | * @return PHPClass |
||
261 | */ |
||
262 | 19 | public function getExtends() |
|
266 | |||
267 | /** |
||
268 | * |
||
269 | * @param PHPClass $extends |
||
270 | * @return PHPClass |
||
271 | */ |
||
272 | 17 | public function setExtends(PHPClass $extends) |
|
277 | |||
278 | public function getAbstract() |
||
282 | |||
283 | 25 | public function setAbstract($abstract) |
|
288 | } |
||
289 |
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.