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 |
||
15 | class RestCollectionTransformer implements DataTransformerInterface |
||
16 | { |
||
17 | /** |
||
18 | * @var ObjectManager |
||
19 | */ |
||
20 | private $entityManager; |
||
21 | |||
22 | /** |
||
23 | * The name of the entity we are working with. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $entityName; |
||
28 | |||
29 | /** |
||
30 | * @param ObjectManager $entityManager |
||
31 | * @param string $entityName |
||
32 | */ |
||
33 | 16 | public function __construct(ObjectManager $entityManager, $entityName) |
|
38 | |||
39 | /** |
||
40 | * Transforms an entity collection to an array of identifiers. |
||
41 | * |
||
42 | * @param Collection|null $collection |
||
43 | * |
||
44 | * @return string[] |
||
45 | */ |
||
46 | 8 | public function transform($collection) |
|
71 | |||
72 | /** |
||
73 | * Transforms an array of ids to an array of entities. |
||
74 | * |
||
75 | * @param Collection|array $collection |
||
76 | * |
||
77 | * @return Collection |
||
78 | * |
||
79 | * @throws TransformationFailedException if entity is not found. |
||
80 | */ |
||
81 | 7 | public function reverseTransform($collection) |
|
117 | |||
118 | /** |
||
119 | * @param ClassMetadata $metadata |
||
120 | * @param mixed $entity The entity. |
||
121 | * |
||
122 | * @throws \RuntimeException |
||
123 | * |
||
124 | * @return mixed |
||
125 | */ |
||
126 | 5 | View Code Duplication | protected function getEntityIdentifier(ClassMetadata $metadata, $entity) |
134 | } |
||
135 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.