1 | <?php |
||
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 |
||
|
|||
31 | */ |
||
32 | public function __construct(array $posts) |
||
38 | |||
39 | /** |
||
40 | * @return Post[] |
||
41 | */ |
||
42 | public function getAllPosts() |
||
46 | |||
47 | /** |
||
48 | * @param string $postName |
||
49 | * |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function isPostRegistered($postName) |
||
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) |
||
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); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param string $category |
||
123 | * |
||
124 | * @return Post[] |
||
125 | */ |
||
126 | public function getPostsByCategory($category) |
||
136 | |||
137 | /** |
||
138 | * @param string $tag |
||
139 | * |
||
140 | * @return Post[] |
||
141 | */ |
||
142 | public function getPostsByTag($tag) |
||
152 | |||
153 | /** |
||
154 | * @param Post $post |
||
155 | */ |
||
156 | private function registerPost(Post $post) |
||
173 | |||
174 | /** |
||
175 | * @param Post $post |
||
176 | */ |
||
177 | private function registerPostInDateIndex(Post $post) |
||
187 | |||
188 | /** |
||
189 | * @param Post $post |
||
190 | */ |
||
191 | private function registerPostInCategoryIndex(Post $post) |
||
201 | |||
202 | /** |
||
203 | * @param Post $post |
||
204 | */ |
||
205 | private function registerPostInTagsIndex(Post $post) |
||
215 | } |
||
216 |
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 methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.