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 |
||
25 | class ModelToElasticaAutoTransformer implements ModelToElasticaTransformerInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var EventDispatcherInterface |
||
29 | */ |
||
30 | protected $dispatcher; |
||
31 | |||
32 | /** |
||
33 | * Optional parameters. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $options = [ |
||
38 | 'identifier' => 'id', |
||
39 | 'index' => '', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * PropertyAccessor instance. |
||
44 | * |
||
45 | * @var PropertyAccessorInterface |
||
46 | */ |
||
47 | protected $propertyAccessor; |
||
48 | |||
49 | /** |
||
50 | * Instanciates a new Mapper. |
||
51 | * |
||
52 | * @param array $options |
||
53 | * @param EventDispatcherInterface $dispatcher |
||
54 | */ |
||
55 | 42 | View Code Duplication | public function __construct(array $options = [], EventDispatcherInterface $dispatcher = null) |
64 | |||
65 | /** |
||
66 | * Set the PropertyAccessor. |
||
67 | * |
||
68 | * @param PropertyAccessorInterface $propertyAccessor |
||
69 | */ |
||
70 | 42 | public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor) |
|
74 | |||
75 | /** |
||
76 | * Transforms an object into an elastica object having the required keys. |
||
77 | * |
||
78 | * @param object $object the object to convert |
||
79 | * @param array $fields the keys we want to have in the returned array |
||
80 | * |
||
81 | * @return Document |
||
82 | **/ |
||
83 | 24 | public function transform($object, array $fields) |
|
92 | |||
93 | /** |
||
94 | * transform a nested document or an object property into an array of ElasticaDocument. |
||
95 | * |
||
96 | * @param array|\Traversable|\ArrayAccess $objects the object to convert |
||
97 | * @param array $fields the keys we want to have in the returned array |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 6 | protected function transformNested($objects, array $fields) |
|
119 | |||
120 | /** |
||
121 | * Attempts to convert any type to a string or an array of strings. |
||
122 | * |
||
123 | * @param mixed $value |
||
124 | * |
||
125 | * @return string|array |
||
126 | */ |
||
127 | protected function normalizeValue($value) |
||
146 | |||
147 | /** |
||
148 | * Transforms the given object to an elastica document. |
||
149 | * |
||
150 | * @param object $object the object to convert |
||
151 | * @param array $fields the keys we want to have in the returned array |
||
152 | * @param string $identifier the identifier for the new document |
||
153 | * |
||
154 | * @return Document |
||
155 | */ |
||
156 | 24 | protected function transformObjectToDocument($object, array $fields, $identifier = '') |
|
219 | } |
||
220 |
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.