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 | class Schema |
||
14 | { |
||
15 | private $definitions; |
||
16 | |||
17 | public function __construct(array $extensions) |
||
23 | |||
24 | /** |
||
25 | * Ensure that a given key is valid. |
||
26 | * |
||
27 | * This is called when the user attempts to access a descriptor. |
||
28 | */ |
||
29 | View Code Duplication | public function validateKey(string $key) |
|
39 | |||
40 | /** |
||
41 | * Validate the descriptor key AND ensures that the given |
||
42 | * descriptor is of the correct type according to the schema. |
||
43 | * |
||
44 | * This is called when a descriptor is set on the description. |
||
45 | */ |
||
46 | public function validate(string $key, DescriptorInterface $descriptor) |
||
60 | |||
61 | /** |
||
62 | * Return the schema definitions. |
||
63 | * |
||
64 | * @return Definition[] |
||
65 | */ |
||
66 | public function getDefinitions(): array |
||
70 | |||
71 | public function getDefinition($key): Definition |
||
81 | |||
82 | private function register(ExtensionInterface $extension) |
||
100 | } |
||
101 |
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.