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 |
||
7 | View Code Duplication | final class NameMeta implements MetaItem |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | private $name; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $content; |
||
18 | |||
19 | /** |
||
20 | * @param string $name |
||
21 | * @param string $content |
||
22 | * |
||
23 | * @return MetaItem |
||
24 | */ |
||
25 | 45 | public static function create(string $name, string $content) : MetaItem { |
|
28 | |||
29 | /** |
||
30 | * @return string |
||
31 | */ |
||
32 | 38 | public function render(array $extra = []) : string { |
|
33 | 38 | $content = $this->content; |
|
34 | |||
35 | 38 | if ($this->isTitle() && isset($extra['title']['suffix'])) { |
|
36 | 1 | $content = "{$content}{$extra['title']['suffix']}"; |
|
37 | } |
||
38 | |||
39 | 38 | return "<meta name=\"{$this->name}\" content=\"{$content}\">"; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * NamedMeta constructor. |
||
44 | * |
||
45 | * @param string $name |
||
46 | * @param string $content |
||
47 | */ |
||
48 | 45 | public function __construct(string $name, string $content) { |
|
52 | |||
53 | 38 | private function isTitle(): bool |
|
57 | } |
||
58 |
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.