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 |
||
20 | class AbstractTransformerFactory implements AbstractFactoryInterface |
||
21 | { |
||
22 | private static $pluginManagers = [ |
||
23 | 'HydratorManager' => HydratorPluginManager::class, |
||
24 | 'ValidatorManager' => ValidatorPluginManager::class, |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * Can the factory create an instance for the service? |
||
29 | * |
||
30 | * @param ServiceLocatorInterface $container |
||
31 | * @param string $name |
||
32 | * @param string $requestedName |
||
33 | * @return bool |
||
34 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
35 | */ |
||
36 | public function canCreateServiceWithName(ServiceLocatorInterface $container, $name, $requestedName) |
||
44 | |||
45 | /** |
||
46 | * Create an object |
||
47 | * |
||
48 | * @param ServiceLocatorInterface $container |
||
49 | * @param string $name |
||
50 | * @param string $requestedName |
||
51 | * @return Transformer |
||
52 | * @throws \Zend\Validator\Exception\InvalidArgumentException |
||
53 | * @throws \Assert\AssertionFailedException |
||
54 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
55 | * @internal param array|null $options |
||
56 | */ |
||
57 | public function createServiceWithName(ServiceLocatorInterface $container, $name, $requestedName) |
||
79 | |||
80 | /** |
||
81 | * @param ServiceLocatorInterface $container |
||
82 | * @param string $requestedName |
||
83 | * @return mixed[] |
||
84 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
85 | */ |
||
86 | private function getTransformerConfig(ServiceLocatorInterface $container, $requestedName) |
||
92 | |||
93 | /** |
||
94 | * @param ServiceLocatorInterface $container |
||
95 | * @param string $service |
||
96 | * @param string $validateIsInstanceOf |
||
97 | * @return ValidatorInterface |
||
98 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
99 | * @throws \Assert\AssertionFailedException |
||
100 | * @throws \Zend\Validator\Exception\InvalidArgumentException |
||
101 | * @internal param TransformerConfig $config |
||
102 | */ |
||
103 | private function getValidator(ServiceLocatorInterface $container, $service, $validateIsInstanceOf = null) |
||
125 | |||
126 | /** |
||
127 | * @param ServiceLocatorInterface $container |
||
128 | * @param string $service |
||
129 | * @return ExtractionInterface |
||
130 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
131 | * @throws \Assert\AssertionFailedException |
||
132 | */ |
||
133 | View Code Duplication | private function getExtractor(ServiceLocatorInterface $container, $service) |
|
149 | |||
150 | /** |
||
151 | * @param ServiceLocatorInterface $container |
||
152 | * @param string $service |
||
153 | * @return HydrationInterface |
||
154 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
155 | * @throws \Assert\AssertionFailedException |
||
156 | */ |
||
157 | View Code Duplication | private function getHydrator(ServiceLocatorInterface $container, $service) |
|
173 | |||
174 | /** |
||
175 | * @param ServiceLocatorInterface $container |
||
176 | * @param string $pluginManagerName |
||
177 | * @param string $service |
||
178 | * @return Object |
||
179 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
180 | * @throws \Assert\AssertionFailedException |
||
181 | */ |
||
182 | private function getService(ServiceLocatorInterface $container, $service, $pluginManagerName) |
||
207 | |||
208 | /** |
||
209 | * @param ServiceLocatorInterface $container |
||
210 | * @param TransformerConfig $config |
||
211 | * @return callable |
||
212 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
213 | */ |
||
214 | private function getTransformer(ServiceLocatorInterface $container, TransformerConfig $config) |
||
238 | } |
||
239 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.