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 | /** |
||
| 40 | * PlatesStrategy constructor. |
||
| 41 | * @param PlatesEngine $viewEngine |
||
| 42 | * @param NotFoundDecorator $notFound |
||
| 43 | * @param NotAllowedDecorator $notAllowed |
||
| 44 | * @param string $layout |
||
| 45 | */ |
||
| 46 | public function __construct(PlatesEngine $viewEngine, NotFoundDecorator $notFound, NotAllowedDecorator $notAllowed, string $layout, ExceptionDecorator $exceptionDecorator) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Invoke the route callable based on the strategy. |
||
| 57 | * |
||
| 58 | * @param \League\Route\Route $route |
||
| 59 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
| 60 | * |
||
| 61 | * @return \Psr\Http\Message\ResponseInterface |
||
| 62 | */ |
||
| 63 | public function invokeRouteCallable(Route $route, ServerRequestInterface $request): ResponseInterface |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param ResponseInterface $response |
||
| 97 | * @param string $body |
||
| 98 | * @param int $status |
||
| 99 | * @return \Psr\Http\Message\MessageInterface|Response |
||
| 100 | */ |
||
| 101 | View Code Duplication | private function getResponseWithBodyAndStatus(Response $response, string $body, int $status = 200) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Get a middleware that will decorate a NotFoundException |
||
| 112 | * |
||
| 113 | * @param \League\Route\Http\Exception\NotFoundException $exception |
||
| 114 | * |
||
| 115 | * @return \Psr\Http\Server\MiddlewareInterface |
||
| 116 | */ |
||
| 117 | public function getNotFoundDecorator(NotFoundException $e): MiddlewareInterface |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get a middleware that will decorate a NotAllowedException |
||
| 124 | * |
||
| 125 | * @param \League\Route\Http\Exception\NotFoundException $e |
||
| 126 | * |
||
| 127 | * @return \Psr\Http\Server\MiddlewareInterface |
||
| 128 | */ |
||
| 129 | public function getMethodNotAllowedDecorator(MethodNotAllowedException $e): MiddlewareInterface |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return MiddlewareInterface |
||
| 136 | */ |
||
| 137 | public function getExceptionHandler(): MiddlewareInterface |
||
| 141 | } |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: