Total Complexity | 11 |
Total Lines | 102 |
Duplicated Lines | 4.9 % |
Changes | 0 |
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 | class Block |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $id; |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $content; |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $lang; |
||
22 | /** |
||
23 | * @var array|string[] |
||
24 | */ |
||
25 | private $tags; |
||
26 | |||
27 | /** |
||
28 | * @param string[] $tags |
||
29 | */ |
||
30 | public function __construct(string $id, string $content, string $lang, array $tags) |
||
31 | { |
||
32 | $this->id = $id; |
||
33 | $this->content = $content; |
||
34 | $this->lang = $lang; |
||
35 | $this->tags = $tags; |
||
36 | } |
||
37 | |||
38 | public static function fromFile(SplFileInfo $file): self |
||
39 | { |
||
40 | if (!is_readable($file->getRealPath())) { |
||
41 | throw new UnableToLoadFileException('Cannot read file '.$file); |
||
42 | } |
||
43 | |||
44 | $extension = strtolower($file->getExtension()); |
||
45 | |||
46 | switch ($extension) { |
||
47 | case 'md': |
||
48 | $parseMarkDown = true; |
||
49 | break; |
||
50 | case 'html': |
||
51 | $parseMarkDown = false; |
||
52 | break; |
||
53 | default: |
||
54 | throw new InvalidExtensionException(sprintf('Invalid extension for block %s. Valid extensions are .md and .html', $file)); |
||
55 | } |
||
56 | |||
57 | $parser = new Parser(); |
||
58 | |||
59 | $document = $parser->parse(file_get_contents($file->getRealPath()), $parseMarkDown); |
||
60 | |||
61 | $yaml = $document->getYAML(); |
||
62 | |||
63 | $compulsoryFields = ['id', 'lang']; |
||
64 | |||
65 | View Code Duplication | foreach ($compulsoryFields as $field) { |
|
|
|||
66 | if (!isset($yaml[$field])) { |
||
67 | throw new UnableToLoadFileException('Missing field '.$field.' in YAML front matter of file '.$file); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return new self( |
||
72 | $yaml['id'], |
||
73 | $document->getContent(), |
||
74 | $yaml['lang'], |
||
75 | $yaml['tags'] ?? [] |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getId(): string |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getContent(): string |
||
91 | { |
||
92 | return $this->content; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | public function getLang(): string |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @return array|string[] |
||
105 | */ |
||
106 | public function getTags() |
||
109 | } |
||
110 | } |
||
111 |
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.