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 |
||
| 19 | class Api |
||
| 20 | { |
||
| 21 | /** @var RouteResolverInterface */ |
||
| 22 | private $routeResolver; |
||
| 23 | |||
| 24 | /** @var RouteContainer */ |
||
| 25 | private $routes; |
||
| 26 | |||
| 27 | /** @var Request */ |
||
| 28 | private $request; |
||
| 29 | |||
| 30 | /** @var Response */ |
||
| 31 | private $response; |
||
| 32 | |||
| 33 | /** @var Container */ |
||
| 34 | private $container; |
||
| 35 | |||
| 36 | /** @var Validator */ |
||
| 37 | private $validator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param RouteResolverInterface $routeResolver |
||
| 41 | * @param Request $request |
||
| 42 | * @param Response $response |
||
| 43 | * @param Container $container |
||
| 44 | * @param ValidatorInterface $validator |
||
| 45 | */ |
||
| 46 | 4 | public function __construct( |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Add api call |
||
| 63 | * @param string $method |
||
| 64 | * @param string $pattern |
||
| 65 | * @param string $handler |
||
| 66 | * @param array $params |
||
| 67 | */ |
||
| 68 | 2 | public function add($method, $pattern, $handler, $params = []) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Add get api call |
||
| 75 | * @param string $pattern |
||
| 76 | * @param string $handler |
||
| 77 | * @param array $params |
||
| 78 | */ |
||
| 79 | public function get($pattern, $handler, $params = []) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Add post api call |
||
| 86 | * @param string $pattern |
||
| 87 | * @param string $handler |
||
| 88 | * @param array $params |
||
| 89 | */ |
||
| 90 | 2 | public function post($pattern, $handler, $params = []) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Add put api call |
||
| 97 | * @param string $pattern |
||
| 98 | * @param string $handler |
||
| 99 | * @param array $params |
||
| 100 | */ |
||
| 101 | public function put($pattern, $handler, $params = []) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Add patch api call |
||
| 108 | * @param string $pattern |
||
| 109 | * @param string $handler |
||
| 110 | * @param array $params |
||
| 111 | */ |
||
| 112 | public function patch($pattern, $handler, $params = []) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Add delete api call |
||
| 119 | * @param string $pattern |
||
| 120 | * @param string $handler |
||
| 121 | * @param array $params |
||
| 122 | */ |
||
| 123 | public function delete($pattern, $handler, $params = []) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Add options api call |
||
| 130 | * @param string $pattern |
||
| 131 | * @param string $handler |
||
| 132 | * @param array $params |
||
| 133 | */ |
||
| 134 | public function options($pattern, $handler, $params = []) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Run api |
||
| 141 | * @param string $url |
||
| 142 | * @return JsonApiResponse |
||
| 143 | * @throws Exception\ValidatorException |
||
| 144 | * @throws HandlerException |
||
| 145 | */ |
||
| 146 | 4 | public function run($url) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Get service by type or name from container |
||
| 170 | * @param string $entry |
||
| 171 | * @return bool|object |
||
| 172 | */ |
||
| 173 | 2 | View Code Duplication | private function getFromContainer($entry) |
| 185 | } |
||
| 186 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.