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 |
||
7 | class ServiceStorage implements StorageInterface |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * @var callable |
||
12 | */ |
||
13 | private $callable; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $arguments; |
||
19 | |||
20 | private $objectTransformer; |
||
21 | private $objectFactory; |
||
22 | |||
23 | 3 | public function __construct(callable $callable, $arguments=array(), $objectMapper=null) |
|
34 | |||
35 | /** |
||
36 | * @return callable |
||
37 | */ |
||
38 | public function getCallable() |
||
42 | |||
43 | 3 | public function setObjectFactory($objectFactory) |
|
47 | |||
48 | 3 | public function setObjectTransformer($objectTransformer) |
|
52 | |||
53 | /* |
||
54 | * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::reader() |
||
55 | */ |
||
56 | 1 | public function reader() |
|
66 | |||
67 | /* |
||
68 | * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::writer() |
||
69 | */ |
||
70 | 1 | public function writer() |
|
79 | |||
80 | /* |
||
81 | * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::info() |
||
82 | */ |
||
83 | View Code Duplication | public function info() |
|
92 | |||
93 | /* |
||
94 | * (non-PHPdoc) @see \Mathielen\ImportEngine\Storage\StorageInterface::getFields() |
||
95 | */ |
||
96 | public function getFields() |
||
100 | } |
||
101 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.