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 |
||
22 | class MetadataDriverAbstractFactory implements AbstractFactoryInterface |
||
23 | { |
||
24 | /** |
||
25 | * @inheritdoc |
||
26 | * |
||
27 | * @param ServiceLocatorInterface $serviceLocator |
||
28 | * @param $name |
||
29 | * @param $requestedName |
||
30 | * |
||
31 | * @return bool|void |
||
32 | */ |
||
33 | public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
||
37 | |||
38 | /** |
||
39 | * @inheritdoc |
||
40 | * |
||
41 | * @param ServiceLocatorInterface $serviceLocator |
||
42 | * @param $name |
||
43 | * @param $requestedName |
||
44 | * |
||
45 | * @return AdapterInterface |
||
46 | * @throws \Nnx\JmsSerializerModule\MetadataDriver\Exception\RuntimeException |
||
47 | * @throws \Nnx\JmsSerializerModule\Options\Exception\InvalidArgumentException |
||
48 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
49 | */ |
||
50 | public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
||
79 | } |
||
80 |
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.