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 |
||
15 | class App implements ContainerInterface |
||
16 | { |
||
17 | private $services = []; |
||
18 | private $items = []; |
||
19 | private $path; |
||
20 | private $uri; |
||
21 | |||
22 | public function __construct(string $path, UriInterface $uri) |
||
27 | |||
28 | public function addServiceProvider(ServiceProviderInterface $provider): self |
||
40 | |||
41 | View Code Duplication | public function addFactory(string $id, callable $factory): self |
|
49 | |||
50 | View Code Duplication | public function addExtension($id, callable $extension): self |
|
58 | |||
59 | /** |
||
60 | * @see ContainerInterface |
||
61 | */ |
||
62 | public function has($id) |
||
74 | |||
75 | /** |
||
76 | * @see ContainerInterface |
||
77 | */ |
||
78 | public function get($id) |
||
99 | |||
100 | public function set(string $id, $value): self |
||
106 | |||
107 | /** |
||
108 | * Returns the absolute path of the app. |
||
109 | */ |
||
110 | public function getPath(string ...$dirs): string |
||
118 | |||
119 | /* |
||
120 | * Returns the base uri of the app. |
||
121 | */ |
||
122 | public function getUri(string ...$dirs): UriInterface |
||
130 | |||
131 | /** |
||
132 | * helper function to fix paths '//' or '/./' or '/foo/../' in a path. |
||
133 | */ |
||
134 | private static function canonicalize(string $base, string ...$dirs): string |
||
145 | } |
||
146 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.