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 |
||
29 | abstract class AbstractEntity implements \ArrayAccess |
||
30 | { |
||
31 | public function offsetExists($offset) |
||
40 | |||
41 | public function offsetSet($offset, $value) |
||
44 | |||
45 | public function offsetGet($offset) |
||
59 | |||
60 | public function offsetUnset($offset) |
||
63 | |||
64 | /** |
||
65 | * 引数の連想配列を元にプロパティを設定します. |
||
66 | * DBから取り出した連想配列を, プロパティへ設定する際に使用します. |
||
67 | * |
||
68 | * @param array $arrProps プロパティの情報を格納した連想配列 |
||
69 | * @param \ReflectionClass $parentClass 親のクラス. 本メソッドの内部的に使用します. |
||
70 | * @param string[] $excludeAttribute 除外したいフィールド名の配列 |
||
71 | */ |
||
72 | public function setPropertiesFromArray(array $arrProps, array $excludeAttribute = [], \ReflectionClass $parentClass = null) |
||
96 | |||
97 | /** |
||
98 | * Convert to associative array. |
||
99 | * |
||
100 | * Symfony Serializer Component is expensive, and hard to implementation. |
||
101 | * Use for encoder only. |
||
102 | * |
||
103 | * @param \ReflectionClass $parentClass parent class. Use internally of this method.. |
||
104 | * @param array $excludeAttribute Array of field names to exclusion. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function toArray(array $excludeAttribute = ['__initializer__', '__cloner__', '__isInitialized__'], \ReflectionClass $parentClass = null) |
||
141 | |||
142 | /** |
||
143 | * Convert to associative array, and normalize to association properties. |
||
144 | * |
||
145 | * The type conversion such as: |
||
146 | * - Datetime :: W3C datetime format string |
||
147 | * - AbstractEntity :: associative array such as [id => value] |
||
148 | * - PersistentCollection :: associative array of [[id => value], [id => value], ...] |
||
149 | * |
||
150 | * @param array $excludeAttribute Array of field names to exclusion. |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function toNormalizedArray(array $excludeAttribute = ['__initializer__', '__cloner__', '__isInitialized__']) |
||
177 | |||
178 | /** |
||
179 | * Convert to JSON. |
||
180 | * |
||
181 | * @param array $excludeAttribute Array of field names to exclusion. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | public function toJSON(array $excludeAttribute = ['__initializer__', '__cloner__', '__isInitialized__']) |
||
189 | |||
190 | /** |
||
191 | * Convert to XML. |
||
192 | * |
||
193 | * @param array $excludeAttribute Array of field names to exclusion. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function toXML(array $excludeAttribute = ['__initializer__', '__cloner__', '__isInitialized__']) |
||
198 | 1 | { |
|
199 | $ReflectionClass = new \ReflectionClass($this); |
||
200 | 1 | $serializer = new Serializer([new PropertyNormalizer()], [new XmlEncoder($ReflectionClass->getShortName())]); |
|
201 | 1 | ||
202 | $xml = $serializer->serialize($this->toNormalizedArray($excludeAttribute), 'xml'); |
||
203 | 1 | if ('\\' === DIRECTORY_SEPARATOR) { |
|
204 | 1 | // The m modifier of the preg functions converts the end-of-line to '\n' |
|
205 | $xml = StringUtil::convertLineFeed($xml, "\r\n"); |
||
206 | } |
||
207 | |||
208 | 1 | return $xml; |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * コピー元のオブジェクトのフィールド名を指定して、同名のフィールドに値をコピー |
||
213 | * |
||
214 | * @param object $srcObject コピー元のオブジェクト |
||
215 | * @param string[] $excludeAttribute 除外したいフィールド名の配列 |
||
216 | * |
||
217 | * @return AbstractEntity |
||
218 | */ |
||
219 | 209 | public function copyProperties($srcObject, array $excludeAttribute = []) |
|
225 | |||
226 | /** |
||
227 | * Convert to Entity of Identity value to associative array. |
||
228 | * |
||
229 | * @param AbstractEntity $Entity |
||
230 | * |
||
231 | * @return array associative array of [[id => value], [id => value], ...] |
||
232 | */ |
||
233 | 1020 | public function getEntityIdentifierAsArray(AbstractEntity $Entity) |
|
254 | } |
||
255 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.