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 |
||
5 | class ReflectionPropertyFactory implements ReflectionFactoryInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var \ReflectionProperty |
||
9 | */ |
||
10 | private $reflectionProperty; |
||
11 | |||
12 | View Code Duplication | public function __construct($class, $name) |
|
|
|||
13 | { |
||
14 | if (!is_string($class) && !class_exists($class)) { |
||
15 | throw Exception\ReflectionExceptionFactory::invalidArgument( |
||
16 | sprintf("Parameter 1 of %s must be either string and valid class name.", __METHOD__) |
||
17 | ); |
||
18 | } |
||
19 | |||
20 | $this->reflectionProperty = new \ReflectionProperty($class, $name); |
||
21 | } |
||
22 | |||
23 | public static function create($class, $name) |
||
27 | |||
28 | public static function export($class, $name, $return = false) |
||
32 | |||
33 | public function getDeclaringClass() |
||
39 | |||
40 | public function getDocComment() |
||
44 | |||
45 | public function getModifiers() |
||
49 | |||
50 | public function getName() |
||
54 | |||
55 | public function getValue($object = null) |
||
75 | |||
76 | public function isDefault() |
||
80 | |||
81 | public function isPrivate() |
||
85 | |||
86 | public function isProtected() |
||
90 | |||
91 | public function isPublic() |
||
95 | |||
96 | public function isStatic() |
||
100 | |||
101 | public function setAccessible($accessible = false) |
||
102 | { |
||
103 | $this->reflectionProperty->setAccessible($accessible); |
||
104 | } |
||
105 | |||
106 | public function setValue($object, $value) |
||
131 | |||
132 | public function __toString() |
||
136 | |||
137 | public function getReflector() |
||
138 | { |
||
141 | } |
||
142 |
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.