Library::getPostsByName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.0175
1
<?php
2
3
namespace Matks\MarkdownBlogBundle\Blog;
4
5
class Library
6
{
7
    /**
8
     * @var string[]
9
     */
10
    private $categoryIndex = [];
11
12
    /**
13
     * @var string[]
14
     */
15
    private $dateIndex = [];
16
17
    /**
18
     * @var string[]
19
     */
20
    private $tagsIndex = [];
21
22
    /**
23
     * Posts indexed by names.
24
     *
25
     * @var Post[]
26
     */
27
    private $posts = [];
28
29
    /**
30
     * @param Post[] $postNames
0 ignored issues
show
Bug introduced by
There is no parameter named $postNames. 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...
31
     */
32
    public function __construct(array $posts)
33
    {
34 1
        foreach ($posts as $post) {
35 1
            $this->addPost($post);
36 1
        }
37 1
    }
38
39
    /**
40
     * @return Post[]
41
     */
42
    public function getAllPosts()
43
    {
44 1
        return $this->posts;
45
    }
46
47
    /**
48
     * @param string $postName
49
     *
50
     * @return bool
51
     */
52
    public function isPostRegistered($postName)
53
    {
54 1
        return true === isset($this->posts[$postName]);
55
    }
56
57
    /**
58
     * @param Post $post
59
     *
60
     * @throws \InvalidArgumentException if post is already registerd in Library
61
     */
62
    public function addPost(Post $post)
63
    {
64 1
        $postName = $post->getName();
65
66 1
        if ($this->isPostRegistered($postName)) {
67 1
            throw new \InvalidArgumentException("Duplicate post $postName");
68
        }
69
70 1
        $this->registerPost($post);
71 1
    }
72
73
    /**
74
     * @param string $postName
75
     *
76
     * @return Post|null
77
     */
78
    public function getPostByName($postName)
79
    {
80 1
        if (false === $this->isPostRegistered($postName)) {
81
            return;
82
        }
83
84 1
        return $this->posts[$postName];
85
    }
86
87
    /**
88
     * @param string[] $postNames
89
     *
90
     * @return Post[]
91
     */
92 1
    public function getPostsByName(array $postNames)
93
    {
94 1
        $posts = [];
95 1
        foreach ($postNames as $postName) {
96 1
            if (false === $this->isPostRegistered($postName)) {
97
                continue;
98
            }
99 1
            $posts[$postName] = $this->posts[$postName];
100 1
        }
101
102 1
        return $posts;
103
    }
104
105
    /**
106
     * @param string $date expected format: YYYY-MM-DD
107
     *
108
     * @return Post[]
109
     */
110
    public function getPostsByDate($date)
111
    {
112 1
        if (false === array_key_exists($date, $this->dateIndex)) {
113
            return [];
114
        }
115
116 1
        $postNames = $this->dateIndex[$date];
117
118 1
        return $this->getPostsByName($postNames);
0 ignored issues
show
Documentation introduced by
$postNames is of type string, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
    }
120
121
    /**
122
     * @param string $category
123
     *
124
     * @return Post[]
125
     */
126
    public function getPostsByCategory($category)
127
    {
128
        if (false === array_key_exists($category, $this->categoryIndex)) {
129
            return [];
130
        }
131
132
        $postNames = $this->categoryIndex[$category];
133
134
        return $this->getPostsByName($postNames);
0 ignored issues
show
Documentation introduced by
$postNames is of type string, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
    }
136
137
    /**
138
     * @param string $tag
139
     *
140
     * @return Post[]
141
     */
142
    public function getPostsByTag($tag)
143
    {
144
        if (false === array_key_exists($tag, $this->tagsIndex)) {
145
            return [];
146
        }
147
148
        $postNames = $this->tagsIndex[$tag];
149
150
        return $this->getPostsByName($postNames);
0 ignored issues
show
Documentation introduced by
$postNames is of type string, but the function expects a array<integer,string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
151
    }
152
153
    /**
154
     * @param Post $post
155
     */
156
    private function registerPost(Post $post)
157
    {
158 1
        $postName = $post->getName();
159
160 1
        $this->posts[$postName] = $post;
161
162 1
        $this->registerPostInDateIndex($post);
163
164 1
        if ($post->getCategory() !== null) {
165
            $this->registerPostInCategoryIndex($post);
166
        }
167
168 1
        $postTags = $post->getTags();
169 1
        if (false === empty($postTags)) {
170
            $this->registerPostInTagsIndex($post);
171
        }
172 1
    }
173
174
    /**
175
     * @param Post $post
176
     */
177
    private function registerPostInDateIndex(Post $post)
178
    {
179 1
        $postDate = $post->getPublishDate();
180
181 1
        if (array_key_exists($postDate, $this->dateIndex)) {
182 1
            $this->dateIndex[$postDate][] = $post->getName();
183 1
        } else {
184 1
            $this->dateIndex[$postDate] = [$post->getName()];
185
        }
186 1
    }
187
188
    /**
189
     * @param Post $post
190
     */
191
    private function registerPostInCategoryIndex(Post $post)
192
    {
193
        $category = $post->getCategory();
194
195
        if (array_key_exists($category, $this->categoryIndex)) {
196
            $this->categoryIndex[$category][] = $post->getName();
197
        } else {
198
            $this->categoryIndex[$category] = [$post->getName()];
199
        }
200
    }
201
202
    /**
203
     * @param Post $post
204
     */
205
    private function registerPostInTagsIndex(Post $post)
206
    {
207
        foreach ($post->getTags() as $tag) {
208
            if (array_key_exists($tag, $this->tagsIndex)) {
209
                $this->tagsIndex[$tag][] = $post->getName();
210
            } else {
211
                $this->tagsIndex[$tag] = [$post->getName()];
212
            }
213
        }
214
    }
215
}
216