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 Api |
||
| 23 | { |
||
| 24 | /** @var RouteResolverInterface */ |
||
| 25 | private $routeResolver; |
||
| 26 | |||
| 27 | /** @var RouteContainer */ |
||
| 28 | private $routes; |
||
| 29 | |||
| 30 | /** @var Request */ |
||
| 31 | private $request; |
||
| 32 | |||
| 33 | /** @var Response */ |
||
| 34 | private $response; |
||
| 35 | |||
| 36 | /** @var Container */ |
||
| 37 | private $container; |
||
| 38 | |||
| 39 | /** @var ValidatorInterface */ |
||
| 40 | private $validator; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param Request $request |
||
| 44 | * @param Response $response |
||
| 45 | * @param Container $container |
||
| 46 | * @param RouteResolverInterface $routeResolver |
||
| 47 | * @param ValidatorInterface $validator |
||
| 48 | */ |
||
| 49 | 26 | public function __construct( |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Add api call |
||
| 66 | * @param string $method |
||
| 67 | * @param string $pattern |
||
| 68 | * @param string $handler |
||
| 69 | * @param array $params |
||
| 70 | */ |
||
| 71 | 26 | public function add($method, $pattern, $handler, $params = []) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Add get api call |
||
| 78 | * @param string $pattern |
||
| 79 | * @param string $handler |
||
| 80 | * @param array $params |
||
| 81 | */ |
||
| 82 | 24 | public function get($pattern, $handler, $params = []) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Add post api call |
||
| 89 | * @param string $pattern |
||
| 90 | * @param string $handler |
||
| 91 | * @param array $params |
||
| 92 | */ |
||
| 93 | 4 | public function post($pattern, $handler, $params = []) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Add put api call |
||
| 100 | * @param string $pattern |
||
| 101 | * @param string $handler |
||
| 102 | * @param array $params |
||
| 103 | */ |
||
| 104 | 2 | public function put($pattern, $handler, $params = []) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Add patch api call |
||
| 111 | * @param string $pattern |
||
| 112 | * @param string $handler |
||
| 113 | * @param array $params |
||
| 114 | */ |
||
| 115 | 2 | public function patch($pattern, $handler, $params = []) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Add delete api call |
||
| 122 | * @param string $pattern |
||
| 123 | * @param string $handler |
||
| 124 | * @param array $params |
||
| 125 | */ |
||
| 126 | 2 | public function delete($pattern, $handler, $params = []) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Add options api call |
||
| 133 | * @param string $pattern |
||
| 134 | * @param string $handler |
||
| 135 | * @param array $params |
||
| 136 | */ |
||
| 137 | 2 | public function options($pattern, $handler, $params = []) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Returns all registered routes |
||
| 144 | * @return RouteContainer |
||
| 145 | */ |
||
| 146 | 2 | public function getRoutes() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $url |
||
| 153 | * @param Logger $logger |
||
| 154 | * @return ApiResponse |
||
| 155 | * @throws UnresolvedHandlerException |
||
| 156 | * @throws UnresolvedRouteException |
||
| 157 | * @throws ValidationFailedException |
||
| 158 | */ |
||
| 159 | 24 | public function run($url, Logger $logger) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get service by type or name from container |
||
| 190 | * @param string $entry |
||
| 191 | * @return bool|object |
||
| 192 | */ |
||
| 193 | 22 | View Code Duplication | private function getFromContainer($entry) |
| 205 | } |
||
| 206 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: