Completed
Push — master ( a6c8f9...b97be3 )
by Vladimir
03:52 queued 19s
created

DataItem   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 171
Duplicated Lines 13.45 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 9
dl 23
loc 171
ccs 27
cts 57
cp 0.4737
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
B evaluateFrontMatter() 11 28 4
A buildPermalink() 0 4 1
A readContent() 0 5 1
A getContent() 0 4 1
A isDraft() 0 4 1
A createJail() 12 12 1
A jsonSerialize() 0 4 1
A getIterator() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
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 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Document;
9
10
use allejo\stakx\DataTransformer\DataTransformer;
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 DataTransformer */
24
    protected $dataTransformer;
25
26
    /**
27
     * DataItem constructor.
28
     */
29 8
    public function __construct(File $file)
30
    {
31 8
        $this->noReadOnConstructor = true;
32
33 8
        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 = null)
49
    {
50
        $workspace = array_merge($this->data, $variables);
51
        $parser = new FrontMatterParser($workspace, array(
52
            'filename' => $this->getFileName(),
53
            'basename' => $this->getBaseName(),
54
        ));
55
        $parser->parse();
56
57
        if (!is_null($parser) && $parser->hasExpansion())
58
        {
59
            throw new \LogicException('The permalink for this item has not been set.');
60
        }
61
62
        $permalink = $workspace['permalink'];
63
64 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...
65
        {
66
            $this->permalink = $permalink[0];
67
            array_shift($permalink);
68
            $this->redirects = $permalink;
69
        }
70
        else
71
        {
72
            $this->permalink = $permalink;
73
            $this->redirects = array();
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function buildPermalink($force = false)
81
    {
82
        return;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 6
    public function readContent()
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 View Code Duplication
    public function createJail()
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...
114
    {
115 4
        $whiteListedFunctions = array_merge(FrontMatterDocument::$whiteListedFunctions, [
116 4
        ]);
117
118
        $jailedFunctions = [
119 4
            'getPageView' => 'getJailedPageView',
120 4
            'getDataset'  => 'getNamespace',
121 4
        ];
122
123 4
        return (new JailedDocument($this, $whiteListedFunctions, $jailedFunctions));
124
    }
125
126
    ///
127
    // JsonSerializable implementation
128
    ///
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function jsonSerialize()
134
    {
135
        return $this->data;
136
    }
137
138
    ///
139
    // IteratorAggregate implementation
140
    ///
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function getIterator()
146
    {
147 1
        return new \ArrayIterator($this->data);
148
    }
149
150
    ///
151
    // ArrayAccess implementation
152
    ///
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function offsetExists($offset)
158
    {
159
        return isset($this->data[$offset]);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165 2
    public function offsetGet($offset)
166
    {
167 2
        return $this->data[$offset];
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function offsetSet($offset, $value)
174
    {
175
        throw new \LogicException('DataItems are read-only.');
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function offsetUnset($offset)
182
    {
183
        throw new \LogicException('DataItems are read-only.');
184
    }
185
}
186