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 |
||
32 | class ReflectionPropertyFactory extends ReflectorFactory |
||
33 | { |
||
34 | const REFLECTION_OBJECT = ReflectionProperty::class; |
||
35 | |||
36 | /** |
||
37 | * @var PHPNativeReflectionProperty |
||
38 | */ |
||
39 | protected $reflector; |
||
40 | |||
41 | /** |
||
42 | * @var ReflectionProperty |
||
43 | */ |
||
44 | protected $object; |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | protected $acceptedParams = |
||
50 | [ |
||
51 | 'readable' => 'setBool', |
||
52 | 'writable' => 'setBool', |
||
53 | 'var' => 'setType', |
||
54 | 'construct' => 'setConstruct' |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * Returns a new ReflectionPropertyFactory using the given class and |
||
59 | * property names |
||
60 | * |
||
61 | * @param string $class The classname of the property |
||
62 | * @param string $property The property to reflect |
||
63 | * @return ReflectionPropertyFactory |
||
64 | */ |
||
65 | public static function fromName($class, $property) |
||
72 | |||
73 | /** |
||
74 | * Builds the ReflectionProperty from the provided parameters, |
||
75 | * linking to a parent ReflectionComposite |
||
76 | * |
||
77 | * @param ReflectionCompostite $parent The reflector for the class |
||
78 | * this property belongs to |
||
79 | * @param mixed $default This property's default value |
||
80 | * @return ReflectionProperty |
||
81 | */ |
||
82 | 19 | public function build(ReflectionComposite $parent, $default) |
|
96 | |||
97 | /** |
||
98 | * Sets the property's type by parsing the @type annotation |
||
99 | * |
||
100 | * @param string $name Should be 'var' |
||
101 | * @param string $value The value of the annotation |
||
102 | */ |
||
103 | 19 | protected function setType($name, $value) |
|
|
|||
104 | { |
||
105 | 19 | View Code Duplication | if ($value{0} !== '?') |
106 | { |
||
107 | 19 | $nullable = false; |
|
108 | } |
||
109 | else |
||
110 | { |
||
111 | 16 | $nullable = true; |
|
112 | 16 | $value = substr($value, 1); |
|
113 | } |
||
114 | |||
115 | 19 | View Code Duplication | if (substr($value, -2) !== '[]') |
116 | { |
||
117 | 19 | $collection = false; |
|
118 | } |
||
119 | else |
||
120 | { |
||
121 | 1 | $collection = true; |
|
122 | 1 | $value = substr($value, 0, -2); |
|
123 | } |
||
124 | |||
125 | switch ($value) |
||
126 | { |
||
127 | 19 | case 'string': |
|
128 | 17 | $class = new StringType(); |
|
129 | 17 | break; |
|
130 | 19 | case 'int': |
|
131 | 19 | case 'integer': |
|
132 | 1 | $class = new IntegerType(); |
|
133 | 1 | break; |
|
134 | 19 | case 'bool': |
|
135 | 19 | case 'boolean': |
|
136 | 18 | $class = new BooleanType(); |
|
137 | 18 | break; |
|
138 | 19 | case 'mixed': |
|
139 | 19 | case '': |
|
140 | $class = new MixedType(); |
||
141 | break; |
||
142 | 19 | case 'null': |
|
143 | $class = new NullType(); |
||
144 | break; |
||
145 | default: |
||
146 | $useStatements = |
||
147 | 19 | $this->object->owner->namespace->useStatements; |
|
148 | |||
149 | 19 | if ($useStatements->containsKey($value)) |
|
150 | { |
||
151 | 17 | $value = $useStatements[$value]->classname; |
|
152 | } |
||
153 | else |
||
154 | { |
||
155 | 19 | $value = $this->object->owner->namespace->namespace |
|
156 | 19 | . '\\' . $value; |
|
157 | } |
||
158 | |||
159 | 19 | $class = new ObjectType($value); |
|
160 | } |
||
161 | |||
162 | 19 | if ($nullable) |
|
163 | { |
||
164 | 16 | (new RawPropertyAccessor($class)) |
|
165 | 16 | ->setRawValue('nullable', true); |
|
166 | } |
||
167 | |||
168 | 19 | if ($collection) |
|
169 | { |
||
170 | 1 | $class = new CollectionType($class); |
|
171 | } |
||
172 | |||
173 | 19 | $this->accessor->setRawValue('type', $class); |
|
174 | 19 | } |
|
175 | |||
176 | /** |
||
177 | * Sets the property's constructor options by parsing the @construct |
||
178 | * annotation |
||
179 | * |
||
180 | * @param string $name Should be 'construct' |
||
181 | * @param string $value The value of the annotation |
||
182 | */ |
||
183 | 18 | protected function setConstruct($name, $value) |
|
242 | } |
||
243 | |||
244 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.