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 |
||
20 | class DocumentContext extends AbstractContext implements LocalContextInterface |
||
21 | { |
||
22 | /** |
||
23 | * @var Document |
||
24 | */ |
||
25 | protected $context; |
||
26 | |||
27 | /** |
||
28 | * @var ContextInterface |
||
29 | */ |
||
30 | private $parent; |
||
31 | |||
32 | /** |
||
33 | * LocalContext constructor. |
||
34 | * @param ContextInterface $parent |
||
35 | * @param Document $document |
||
36 | */ |
||
37 | public function __construct(ContextInterface $parent, Document $document) |
||
42 | |||
43 | /** |
||
44 | * @return Document |
||
45 | */ |
||
46 | public function getDocument(): Document |
||
50 | |||
51 | /** |
||
52 | * @return Readable |
||
53 | */ |
||
54 | public function getFile(): Readable |
||
58 | |||
59 | /** |
||
60 | * @return ContextInterface|GlobalContext |
||
61 | */ |
||
62 | public function getParent(): ContextInterface |
||
66 | |||
67 | /** |
||
68 | * @param string $type |
||
69 | * @param array|ValueInterface[] $variables |
||
70 | * @return TypeDefinition |
||
71 | */ |
||
72 | View Code Duplication | public function get(string $type, array $variables = []): TypeDefinition |
|
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function __toString(): string |
||
88 | } |
||
89 |
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.