Completed
Pull Request — master (#61)
by Vladimir
03:14
created

ContentItem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 21.98 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 67.86%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 6
dl 20
loc 91
ccs 19
cts 28
cp 0.6786
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setMarkupEngine() 0 4 1
A handleSpecialRedirects() 0 16 3
A getContent() 0 16 3
A createJail() 12 12 1
A jsonSerialize() 8 8 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\Filesystem\File;
11
use allejo\stakx\MarkupEngine\MarkupEngine;
12
use allejo\stakx\MarkupEngine\MarkupEngineManager;
13
use allejo\stakx\Templating\TemplateErrorInterface;
14
15
class ContentItem extends PermalinkFrontMatterDocument implements CollectableItem, TemplateReadyDocument
16
{
17
    use CollectableItemTrait;
18
    use TemplateEngineDependent;
19
20
    /** @var MarkupEngine */
21
    private $markupEngine;
22
23 64
    public function setMarkupEngine(MarkupEngineManager $manager)
24
    {
25 64
        $this->markupEngine = $manager->getEngineByExtension($this->getExtension());
26 64
    }
27
28
    ///
29
    // Permalink management
30
    ///
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function handleSpecialRedirects()
36
    {
37 1
        $fm = $this->getFrontMatter();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $fm. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
38
39 1
        if (isset($fm['redirect_from']))
40
        {
41
            $redirects = $fm['redirect_from'];
42
43
            if (!is_array($redirects))
44
            {
45
                $redirects = [$redirects];
46
            }
47
48
            $this->redirects = array_merge($this->redirects, $redirects);
49
        }
50 1
    }
51
52
    ///
53
    // Document body transformation
54
    ///
55
56
    /**
57
     * @throws TemplateErrorInterface
58
     *
59
     * @return string
60
     */
61 7
    public function getContent()
62
    {
63 7
        if (!$this->bodyContentEvaluated)
64
        {
65 7
            $this->bodyContent = $this->parseTemplateLanguage($this->bodyContent);
66
67 7
            if ($this->markupEngine)
68
            {
69 6
                $this->bodyContent = $this->markupEngine->parse($this->bodyContent);
70
            }
71
72 7
            $this->bodyContentEvaluated = true;
73
        }
74
75 7
        return (string)$this->bodyContent;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 25 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...
82
    {
83 25
        $whiteListedFunctions = array_merge(self::$whiteListedFunctions, [
84 25
        ]);
85
86
        $jailedFunctions = [
87 25
            'getPageView'   => 'getJailedPageView',
88
            'getCollection' => 'getNamespace',
89
        ];
90
91 25
        return (new JailedDocument($this, $whiteListedFunctions, $jailedFunctions));
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 View Code Duplication
    public function jsonSerialize()
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...
98
    {
99
        return array_merge($this->getFrontMatter(), [
100
            'content'   => $this->getContent(),
101
            'permalink' => $this->getPermalink(),
102
            'redirects' => $this->getRedirects(),
103
        ]);
104
    }
105
}
106