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 | * @return Invoker |
||
| 49 | */ |
||
| 50 | public function getInvoker(): Invoker |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param mixed $object |
||
| 57 | */ |
||
| 58 | public function addHandler($object) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param MapperInterface $mapper |
||
| 70 | */ |
||
| 71 | public function setMapper(MapperInterface $mapper) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param InvokeSpec $specifier |
||
| 78 | * |
||
| 79 | * @return ResultSpec |
||
| 80 | */ |
||
| 81 | public function process(InvokeSpec $specifier): ResultSpec |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return Interceptor |
||
| 103 | */ |
||
| 104 | public function onPreProcess(): Interceptor |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param AbstractInvoke $invoke |
||
| 111 | * |
||
| 112 | * @return AbstractInvoke |
||
| 113 | */ |
||
| 114 | View Code Duplication | private function preProcess(AbstractInvoke $invoke): AbstractInvoke |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param Invoke\Invoke $unit |
||
| 127 | * |
||
| 128 | * @return Result\AbstractResult |
||
| 129 | */ |
||
| 130 | private function handleCallUnit(Invoke\Invoke $unit): Result\AbstractResult |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param Invoke\Notification $unit |
||
| 144 | * |
||
| 145 | * @return Result\AbstractResult |
||
| 146 | */ |
||
| 147 | private function handleNotificationUnit(Invoke\Notification $unit): Result\AbstractResult |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param Invoke\Error $unit |
||
| 161 | * |
||
| 162 | * @return Result\AbstractResult |
||
| 163 | */ |
||
| 164 | private function handleErrorUnit(Invoke\Error $unit): Result\AbstractResult |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $requestedMethod |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | private function getClassAndMethod(string $requestedMethod) |
||
| 184 | } |
||
| 185 |
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.