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 Processor |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $handlers = []; |
||
21 | |||
22 | /** |
||
23 | * @var MapperInterface |
||
24 | */ |
||
25 | private $mapper; |
||
26 | |||
27 | /** |
||
28 | * @var Invoker |
||
29 | */ |
||
30 | private $invoker; |
||
31 | |||
32 | /** |
||
33 | * @var Interceptor |
||
34 | */ |
||
35 | private $preProcess; |
||
36 | |||
37 | /** |
||
38 | * Processor constructor. |
||
39 | */ |
||
40 | public function __construct() |
||
46 | |||
47 | /** |
||
48 | * @param mixed $object |
||
49 | */ |
||
50 | public function addHandler($object) |
||
59 | |||
60 | /** |
||
61 | * @param MapperInterface $mapper |
||
62 | */ |
||
63 | public function setMapper(MapperInterface $mapper) |
||
67 | |||
68 | /** |
||
69 | * @param InvokeSpec $specifier |
||
70 | * |
||
71 | * @return ResultSpec |
||
72 | */ |
||
73 | public function process(InvokeSpec $specifier): ResultSpec |
||
92 | |||
93 | /** |
||
94 | * @return Interceptor |
||
95 | */ |
||
96 | public function onPreProcess(): Interceptor |
||
100 | |||
101 | /** |
||
102 | * @param AbstractInvoke $invoke |
||
103 | * |
||
104 | * @return AbstractInvoke |
||
105 | */ |
||
106 | View Code Duplication | private function preProcess(AbstractInvoke $invoke): AbstractInvoke |
|
116 | |||
117 | /** |
||
118 | * @param Invoke\Invoke $unit |
||
119 | * |
||
120 | * @return Result\AbstractResult |
||
121 | */ |
||
122 | private function handleCallUnit(Invoke\Invoke $unit): Result\AbstractResult |
||
133 | |||
134 | /** |
||
135 | * @param Invoke\Notification $unit |
||
136 | * |
||
137 | * @return Result\AbstractResult |
||
138 | */ |
||
139 | private function handleNotificationUnit(Invoke\Notification $unit): Result\AbstractResult |
||
150 | |||
151 | /** |
||
152 | * @param Invoke\Error $unit |
||
153 | * |
||
154 | * @return Result\AbstractResult |
||
155 | */ |
||
156 | private function handleErrorUnit(Invoke\Error $unit): Result\AbstractResult |
||
160 | |||
161 | /** |
||
162 | * @param string $requestedMethod |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | private function getClassAndMethod(string $requestedMethod) |
||
176 | } |
||
177 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.