ContentParser   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 172
Duplicated Lines 5.23 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 9
loc 172
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
C __invoke() 0 40 7
A getMarkdownFileIterator() 0 4 1
A getContentCreator() 0 4 1
A getMarkdownDocumentParser() 0 4 1
A getMarkdownOptions() 0 4 1
A sortByDate() 0 4 1
A getRelativePath() 0 5 1
A sortByEntryCountDescending() 0 7 3

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
 * Lichtenwallner  (https://lichtenwallner.at)
4
 *
5
 * @see https://github.com/jolicht/markdown-cms for the canonical source repository
6
 * @license https://github.com/jolicht/markdown-cms/blob/master/LICENSE MIT
7
 * @copyright Copyright (c) Johannes Lichtenwallner
8
 */
9
declare(strict_types = 1);
10
namespace Jolicht\MarkdownCms\Parser;
11
12
use Jolicht\MarkdownCms\Iterator\MarkdownFileIterator;
13
use Jolicht\MarkdownCms\ContentType\ContentCreator;
14
use Jolicht\MarkdownCms\Markdown\MarkdownDocumentParser;
15
16
class ContentParser
17
{
18
    /**
19
     * Markdown file iterator
20
     *
21
     * @var MarkdownFileIterator
22
     */
23
    private $markdownFileIterator;
24
25
    /**
26
     * Content creator
27
     *
28
     * @var ContentCreator
29
     */
30
    private $contentCreator;
31
32
    /**
33
     * Markdown document parser
34
     *
35
     * @var MarkdownDocumentParser
36
     */
37
    private $mdDocumentParser;
38
39
    /**
40
     * Markdown options
41
     *
42
     * @var array
43
     */
44
    private $markdownOptions;
45
46
    /**
47
     * Content path
48
     *
49
     * @var string
50
     */
51
    private $contentDir;
52
53
    /**
54
     * Constructor
55
     *
56
     * @param MarkdownFileIterator $markdownFileIterator
57
     * @param ContentCreator $contentCreator
58
     * @param MarkdownDocumentParser $mdDocumentParser
59
     */
60 View Code Duplication
    public function __construct(MarkdownFileIterator $markdownFileIterator, ContentCreator $contentCreator,
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...
61
        MarkdownDocumentParser $mdDocumentParser, array $markdownOptions)
62
    {
63
        $this->markdownFileIterator = $markdownFileIterator;
64
        $this->contentCreator = $contentCreator;
65
        $this->mdDocumentParser = $mdDocumentParser;
66
        $this->markdownOptions = $markdownOptions;
67
        $this->contentDir = $markdownOptions['content_dir'];
68
    }
69
70
    public function __invoke()
71
    {
72
73
        $config = [
74
            'all' => [],
75
            'content_types' => [
76
                'blogEntry' => [
77
                    'all' => [],
78
                    'tags' => [],
79
                ],
80
            ]
81
        ];
82
        $mdDocumentParser = $this->getMarkdownDocumentParser();
83
        $contentCreator = $this->getContentCreator();
84
        foreach ($this->getMarkdownFileIterator() as $file) {
85
            $entry = $contentCreator($mdDocumentParser(file_get_contents($file->getPathname())));
86
            $config['all'][$entry->getId()] = [
87
                'src' => $this->getRelativePath($file)
88
            ];
89
            if (true === $entry->isDraft()) {
90
                continue;
91
            }
92
            $entryDate = $entry->getCreated()->format('Y-m-d\TH:i:s');
93
            $config['content_types'][$entry->getType()]['all'][$entryDate] = $entry->getId();
94
            if ('blogEntry' == $entry->getType()) {
95
                foreach ($entry->getTags() as $tag) {
96
                    $config['content_types']['blogEntry']['tags'][$tag->getId()][$entryDate] = $entry->getId();
97
                }
98
            }
99
        }
100
        foreach ($config['content_types'] as $type => $entry) {
101
            $this->sortByDate($config['content_types'][$type]['all']);
102
        }
103
        ksort($config['content_types']['blogEntry']['tags']);
104
        uasort($config['content_types']['blogEntry']['tags'], array($this, 'sortByEntryCountDescending'));
105
        foreach ($config['content_types']['blogEntry']['tags'] as $tag => $entries) {
106
            $this->sortByDate($config['content_types']['blogEntry']['tags'][$tag]);
107
        }
108
        return $config;
109
    }
110
111
    /**
112
     * Get markdown file iterator
113
     *
114
     * @return MarkdownFileIterator
115
     */
116
    public function getMarkdownFileIterator() : MarkdownFileIterator
117
    {
118
        return $this->markdownFileIterator;
119
    }
120
121
    /**
122
     * Get content creator
123
     *
124
     * @return ContentCreator
125
     */
126
    public function getContentCreator() : ContentCreator
127
    {
128
        return $this->contentCreator;
129
    }
130
131
    /**
132
     * Get markdown document parser
133
     *
134
     * @return MarkdownDocumentParser
135
     */
136
    public function getMarkdownDocumentParser() : MarkdownDocumentParser
137
    {
138
        return $this->mdDocumentParser;
139
    }
140
141
    /**
142
     * Get markdown option
143
     *
144
     * @return array
145
     */
146
    public function getMarkdownOptions() : array
147
    {
148
        return $this->markdownOptions;
149
    }
150
151
    /**
152
     * Sort by date
153
     *
154
     * @param array $config
155
     */
156
    private function sortByDate(array &$config)
157
    {
158
        krsort($config);
159
    }
160
161
    /**
162
     * Get relative path
163
     *
164
     * @param \SplFileInfo $file
165
     * @return string
166
     */
167
    private function getRelativePath(\SplFileInfo $file) : string
168
    {
169
        $path = $file->getPathname();
170
        return substr($path, strrpos($path, $this->contentDir) + strlen($this->contentDir) + 1);
171
    }
172
173
    /**
174
     * Sort by entry count descending
175
     *
176
     * @param array $a
0 ignored issues
show
Bug introduced by
There is no parameter named $a. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
177
     * @param array $b
0 ignored issues
show
Bug introduced by
There is no parameter named $b. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
     * @return number
179
     */
180
    private function sortByEntryCountDescending(array $configEntryA, array $configEntryB)
181
    {
182
        if (count($configEntryA) == count($configEntryB)) {
183
            return 0;
184
        }
185
        return (count($configEntryA) >count($configEntryB)) ? -1 : 1;
186
    }
187
}