Passed
Push — master ( 4bccec...259a7c )
by David
01:44
created

Block   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 4.9 %

Importance

Changes 0
Metric Value
dl 5
loc 102
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A __construct() 0 6 1
A getLang() 0 3 1
B fromFile() 5 38 6
A getTags() 0 3 1
A getContent() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

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
2
namespace TheCodingMachine\CMS\StaticRegistry\Loaders;
3
4
use Mni\FrontYAML\Parser;
5
use \SplFileInfo;
6
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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
83
    {
84
        return $this->id;
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
99
    {
100
        return $this->lang;
101
    }
102
103
    /**
104
     * @return array|string[]
105
     */
106
    public function getTags()
107
    {
108
        return $this->tags;
109
    }
110
}
111