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 |
||
13 | final class TypedContainer implements Container |
||
14 | { |
||
15 | /** |
||
16 | * @var Dictionary |
||
17 | */ |
||
18 | private $values = []; |
||
19 | |||
20 | private function __construct(Dictionary $values) |
||
24 | |||
25 | public static function create() : self |
||
29 | |||
30 | /** |
||
31 | * Finds an entry of the container by its identifier and returns it. |
||
32 | * |
||
33 | * @param string $id Identifier of the entry to look for. |
||
34 | * |
||
35 | * @return Result |
||
36 | */ |
||
37 | public function get($id) : Result |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function has($id) : bool |
||
63 | |||
64 | public function add(string $id, ValueFactory $factory) : Container |
||
68 | } |
||
69 |
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.