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 declare(strict_types = 1); |
||
17 | class UrlGenerator implements UrlGeneratorContract |
||
18 | { |
||
19 | /** |
||
20 | * @var ServerRequestInterface |
||
21 | */ |
||
22 | private $request; |
||
23 | |||
24 | /** |
||
25 | * @var RouteCollectionContract |
||
26 | */ |
||
27 | private $routes; |
||
28 | |||
29 | /** |
||
30 | * @var UriInterface |
||
31 | */ |
||
32 | private $uri; |
||
33 | |||
34 | /** |
||
35 | * UrlGenerator constructor. |
||
36 | * |
||
37 | * @param ServerRequestInterface $request |
||
38 | * @param RouteCollectionContract $routes |
||
39 | * @param UriInterface $uri |
||
40 | */ |
||
41 | 5 | public function __construct(ServerRequestInterface $request, RouteCollectionContract $routes, UriInterface $uri) |
|
47 | |||
48 | /** |
||
49 | * @inheritDoc |
||
50 | */ |
||
51 | 1 | public function toAsset(string $asset): UriInterface |
|
60 | |||
61 | /** |
||
62 | * @inheritDoc |
||
63 | */ |
||
64 | 2 | View Code Duplication | public function toCurrent(array $variables = [], array $query = []): UriInterface |
76 | |||
77 | /** |
||
78 | * @inheritDoc |
||
79 | */ |
||
80 | 2 | View Code Duplication | public function toRoute(string $routeName, array $variables = [], array $query = []): UriInterface |
92 | |||
93 | /** |
||
94 | * Adds port to URI if it is needed. |
||
95 | * |
||
96 | * @param UriInterface $uri |
||
97 | * @return UriInterface |
||
98 | */ |
||
99 | 3 | private function addPortToUri(UriInterface $uri): UriInterface |
|
109 | |||
110 | /** |
||
111 | * Builds URI for provided route instance. |
||
112 | * |
||
113 | * @param RouteContract $route |
||
114 | * @param array $variables |
||
115 | * @param array $query |
||
116 | * @return UriInterface |
||
117 | */ |
||
118 | 2 | private function buildRouteUri(RouteContract $route, array $variables = [], array $query = []): UriInterface |
|
131 | } |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.