Completed
Push — master ( 7e0874...14a89c )
by Vladimir
04:01 queued 01:49
created

DataItem   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 182
Duplicated Lines 8.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 50.88%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 9
dl 15
loc 182
ccs 29
cts 57
cp 0.5088
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 29 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 1
A offsetGet() 4 16 4
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 array */
21
    protected $data;
22
23
    /** @var DataTransformerInterface */
24
    protected $dataTransformer;
25
26
    /**
27
     * DataItem constructor.
28
     */
29 7
    public function __construct(File $file)
30
    {
31 7
        $this->noReadOnConstructor = true;
32
33 7
        parent::__construct($file);
34 7
    }
35
36
    /**
37
     * Set the transformer used to convert the file contents into an array,.
38
     */
39 7
    public function setDataTransformer(DataTransformerManager $manager)
40
    {
41 7
        $this->dataTransformer = $manager->getTransformer($this->getExtension());
42 6
        $this->readContent();
43 6
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function evaluateFrontMatter(array $variables = [], array $complexVariables = [])
49
    {
50
        $workspace = array_merge($this->data, $variables);
51
        $parser = new FrontMatterParser($workspace, [
52
            'filename' => $this->getFileName(),
53
            'basename' => $this->getBaseName(),
54
        ]);
55
        $parser->addComplexVariables($complexVariables);
56
        $parser->parse();
57
58
        if (!is_null($parser) && $parser->hasExpansion())
59
        {
60
            throw new \LogicException('The permalink for this item has not been set.');
61
        }
62
63
        $permalink = $workspace['permalink'];
64
65 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...
66
        {
67
            $this->permalink = $permalink[0];
68
            array_shift($permalink);
69
            $this->redirects = $permalink;
70
        }
71
        else
72
        {
73
            $this->permalink = $permalink;
74
            $this->redirects = [];
75
        }
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function buildPermalink($force = false)
82
    {
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 6
    public function readContents($mixed)
89
    {
90 6
        $content = $this->file->getContents();
91 6
        $this->data = $this->dataTransformer->transformData($content);
92 6
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 4
    public function getContent()
98
    {
99 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...
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 2
    public function isDraft()
106
    {
107 2
        return false;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 4
    public function createJail()
114
    {
115 4
        $whiteListedFunctions = array_merge(FrontMatterDocument::$whiteListedFunctions, [
116 4
        ]);
117
118
        $jailedFunctions = [
119 4
            'getDataset' => 'getNamespace',
120
        ];
121
122 4
        return new JailedDocument($this, $whiteListedFunctions, $jailedFunctions);
123
    }
124
125
    ///
126
    // JsonSerializable implementation
127
    ///
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function jsonSerialize()
133
    {
134
        return $this->data;
135
    }
136
137
    ///
138
    // IteratorAggregate implementation
139
    ///
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function getIterator()
145
    {
146 1
        return new \ArrayIterator($this->data);
147
    }
148
149
    ///
150
    // ArrayAccess implementation
151
    ///
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function offsetExists($offset)
157
    {
158
        return isset($this->data[$offset]);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 2
    public function offsetGet($offset)
165
    {
166 2
        $fxnCall = 'get' . ucfirst($offset);
167
168 2 View Code Duplication
        if (in_array($fxnCall, FrontMatterDocument::$whiteListedFunctions) && method_exists($this, $fxnCall))
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...
169
        {
170
            return call_user_func_array([$this, $fxnCall], []);
171
        }
172
173 2
        if (isset($this->data[$offset]))
174
        {
175 2
            return $this->data[$offset];
176
        }
177
178 1
        return null;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function offsetSet($offset, $value)
185
    {
186
        throw new \LogicException('DataItems are read-only.');
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function offsetUnset($offset)
193
    {
194
        throw new \LogicException('DataItems are read-only.');
195
    }
196
}
197