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 |
||
23 | class PlatesStrategy extends ApplicationStrategy implements StrategyInterface |
||
24 | { |
||
25 | use HasLayoutTrait; |
||
26 | |||
27 | /** @var PlatesEngine $viewEngine */ |
||
28 | private $viewEngine; |
||
29 | |||
30 | /** @var NotFoundDecorator $notFoundDecorator */ |
||
31 | private $notFoundDecorator; |
||
32 | |||
33 | /** @var NotAllowedDecorator $notAllowedDecorator */ |
||
34 | private $notAllowedDecorator; |
||
35 | |||
36 | /** @var ExceptionDecorator $exceptionDecorator */ |
||
37 | private $exceptionDecorator; |
||
38 | |||
39 | /** @var bool */ |
||
40 | private $i18nEnabled = false; |
||
41 | |||
42 | /** @var array */ |
||
43 | private $supportedLocales = []; |
||
44 | |||
45 | /** |
||
46 | * PlatesStrategy constructor. |
||
47 | * @param PlatesEngine $viewEngine |
||
48 | * @param NotFoundDecorator $notFound |
||
49 | * @param NotAllowedDecorator $notAllowed |
||
50 | * @param string $layout |
||
51 | */ |
||
52 | public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator) |
||
53 | { |
||
54 | $this->viewEngine = $viewEngine; |
||
55 | $this->notFoundDecorator = $notFound; |
||
56 | $this->notAllowedDecorator = $notAllowed; |
||
57 | $this->exceptionDecorator = $exceptionDecorator; |
||
58 | $this->setLayout($layout); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param bool $i18nEnabled |
||
63 | */ |
||
64 | public function setI18nEnabled(bool $i18nEnabled): void |
||
68 | |||
69 | /** |
||
70 | * @param array $locales |
||
71 | */ |
||
72 | public function setSupportedLocales(array $locales): void |
||
76 | |||
77 | |||
78 | /** |
||
79 | * Invoke the route callable based on the strategy. |
||
80 | * |
||
81 | * @param \League\Route\Route $route |
||
82 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
83 | * |
||
84 | * @return \Psr\Http\Message\ResponseInterface |
||
85 | */ |
||
86 | public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface |
||
115 | |||
116 | /** |
||
117 | * @param ResponseInterface $response |
||
118 | * @param string $body |
||
119 | * @param int $status |
||
120 | * @return \Psr\Http\Message\MessageInterface|Response |
||
121 | */ |
||
122 | View Code Duplication | private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200) |
|
130 | |||
131 | /** |
||
132 | * Get a middleware that will decorate a NotFoundException |
||
133 | * |
||
134 | * @param \League\Route\Http\Exception\NotFoundException $exception |
||
135 | * |
||
136 | * @return \Psr\Http\Server\MiddlewareInterface |
||
137 | */ |
||
138 | public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface |
||
142 | |||
143 | /** |
||
144 | * Get a middleware that will decorate a NotAllowedException |
||
145 | * |
||
146 | * @param \League\Route\Http\Exception\NotFoundException $e |
||
147 | * |
||
148 | * @return \Psr\Http\Server\MiddlewareInterface |
||
149 | */ |
||
150 | public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface |
||
154 | |||
155 | /** |
||
156 | * @return MiddlewareInterface |
||
157 | */ |
||
158 | public function getExceptionHandler(): MiddlewareInterface |
||
162 | } |
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.