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 RestEntityTransformer implements DataTransformerInterface |
||
16 | { |
||
17 | /** @var ObjectManager */ |
||
18 | private $objectManager; |
||
19 | |||
20 | /** @var string */ |
||
21 | private $entityName; |
||
22 | |||
23 | /** |
||
24 | * @param ObjectManager $objectManager |
||
25 | * @param string $entityName |
||
26 | */ |
||
27 | 8 | public function __construct(ObjectManager $objectManager, $entityName) |
|
32 | |||
33 | /** |
||
34 | * Transforms an entity to a string: __toString or id, in that order. |
||
35 | * |
||
36 | * @param string|null $entity |
||
37 | * |
||
38 | * @return string|null |
||
39 | */ |
||
40 | 5 | public function transform($entity) |
|
55 | |||
56 | /** |
||
57 | * Transforms an id to an entity. |
||
58 | * |
||
59 | * @param string $identifier |
||
60 | * |
||
61 | * @throws TransformationFailedException if entity is not found. |
||
62 | * |
||
63 | * @return null|object |
||
64 | */ |
||
65 | 3 | public function reverseTransform($identifier) |
|
85 | |||
86 | /** |
||
87 | * @param ClassMetadata $metadata |
||
88 | * @param mixed $entity The entity. |
||
89 | * |
||
90 | * @throws RuntimeException if multiple ids. |
||
91 | * |
||
92 | * @return mixed |
||
93 | */ |
||
94 | 2 | View Code Duplication | protected function getEntityIdentifier(ClassMetadata $metadata, $entity) |
102 | } |
||
103 |
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.