Completed
Push — master ( 31b686...ee6a28 )
by Vladimir
02:12
created

DataItem   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 191
Duplicated Lines 16.75 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 9
dl 32
loc 191
ccs 30
cts 60
cp 0.5
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setDataTransformer() 0 5 1
A evaluateFrontMatter() 11 30 4
A buildPermalink() 0 3 1
A readContents() 0 5 1
A getContent() 0 4 1
A isDraft() 0 4 1
A createJail() 0 11 1
A jsonSerialize() 0 4 1
A getIterator() 0 4 1
A offsetExists() 0 4 2
A offsetGet() 21 21 5
A offsetSet() 0 4 1
A offsetUnset() 0 4 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
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\DataTransformer\DataTransformerInterface;
11
use allejo\stakx\DataTransformer\DataTransformerManager;
12
use allejo\stakx\Filesystem\File;
13
use allejo\stakx\FrontMatter\FrontMatterParser;
14
15
class DataItem extends ReadableDocument implements CollectableItem, TemplateReadyDocument, PermalinkDocument
16
{
17
    use CollectableItemTrait;
18
    use PermalinkDocumentTrait;
19
20
    /** @var DataTransformerInterface */
21
    protected $dataTransformer;
22
23
    /** @var array */
24
    protected $frontMatter;
25
26
    /** @var array */
27
    protected $data;
28
29
    /**
30
     * DataItem constructor.
31
     */
32 7
    public function __construct(File $file)
33
    {
34 7
        $this->noReadOnConstructor = true;
35
36 7
        parent::__construct($file);
37 7
    }
38
39
    /**
40
     * Set the transformer used to convert the file contents into an array,.
41
     */
42 7
    public function setDataTransformer(DataTransformerManager $manager)
43
    {
44 7
        $this->dataTransformer = $manager->getTransformer($this->getExtension());
45 6
        $this->readContent();
46 6
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function evaluateFrontMatter(array $variables = [], array $complexVariables = [])
52
    {
53
        $this->frontMatter = array_merge($this->data, $variables);
54
        $parser = new FrontMatterParser($this->frontMatter, [
55
            'basename' => $this->getBasename(),
56
            'filename' => $this->getFileName(),
57
            'filePath' => $this->getRelativeFilePath(),
58
        ]);
59
        $parser->addComplexVariables($complexVariables);
60
        $parser->parse();
61
62
        if (!is_null($parser) && $parser->hasExpansion())
63
        {
64
            throw new \LogicException('The permalink for this item has not been set.');
65
        }
66
67
        $permalink = $this->frontMatter['permalink'];
68
69 View Code Duplication
        if (is_array($permalink))
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...
70
        {
71
            $this->permalink = $permalink[0];
72
            array_shift($permalink);
73
            $this->redirects = $permalink;
74
        }
75
        else
76
        {
77
            $this->permalink = $permalink;
78
            $this->redirects = [];
79
        }
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function buildPermalink($force = false)
86
    {
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 6
    public function readContents($mixed)
93
    {
94 6
        $content = $this->file->getContents();
95 6
        $this->data = $this->dataTransformer->transformData($content);
96 6
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 4
    public function getContent()
102
    {
103 4
        return $this->data;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->data; (array) is incompatible with the return type of the parent method allejo\stakx\Document\ReadableDocument::getContent of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 2
    public function isDraft()
110
    {
111 2
        return false;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 4
    public function createJail()
118
    {
119 4
        $whiteListedFunctions = array_merge(FrontMatterDocument::$whiteListedFunctions, [
120 4
        ]);
121
122
        $jailedFunctions = [
123 4
            'getDataset' => 'getNamespace',
124
        ];
125
126 4
        return new JailedDocument($this, $whiteListedFunctions, $jailedFunctions);
127
    }
128
129
    ///
130
    // JsonSerializable implementation
131
    ///
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function jsonSerialize()
137
    {
138
        return $this->data;
139
    }
140
141
    ///
142
    // IteratorAggregate implementation
143
    ///
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 1
    public function getIterator()
149
    {
150 1
        return new \ArrayIterator($this->data);
151
    }
152
153
    ///
154
    // ArrayAccess implementation
155
    ///
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function offsetExists($offset)
161
    {
162
        return isset($this->data[$offset]) || isset($this->frontMatter[$offset]);
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 2 View Code Duplication
    public function offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
169
    {
170 2
        $fxnCall = 'get' . ucfirst($offset);
171
172 2
        if (in_array($fxnCall, FrontMatterDocument::$whiteListedFunctions) && method_exists($this, $fxnCall))
173
        {
174
            return call_user_func_array([$this, $fxnCall], []);
175
        }
176
177 2
        if (isset($this->data[$offset]))
178
        {
179 2
            return $this->data[$offset];
180
        }
181
182 1
        if (isset($this->frontMatter[$offset]))
183
        {
184
            return $this->frontMatter[$offset];
185
        }
186
187 1
        return null;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function offsetSet($offset, $value)
194
    {
195
        throw new \LogicException('DataItems are read-only.');
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function offsetUnset($offset)
202
    {
203
        throw new \LogicException('DataItems are read-only.');
204
    }
205
}
206