1 | <?php |
||
40 | class Post |
||
41 | { |
||
42 | /** |
||
43 | * Use constants to define configuration options that rarely change instead |
||
44 | * of specifying them under parameters section in config/services.yaml file. |
||
45 | * |
||
46 | * See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options |
||
47 | */ |
||
48 | const NUM_ITEMS = 10; |
||
49 | |||
50 | /** |
||
51 | * @var PostId |
||
52 | */ |
||
53 | private $id; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | private $title; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | private $slug; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private $summary; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | private $content; |
||
74 | |||
75 | /** |
||
76 | * @var DateTimeImmutable |
||
77 | */ |
||
78 | private $publishedAt; |
||
79 | |||
80 | /** |
||
81 | * @var UserId |
||
82 | */ |
||
83 | private $authorId; |
||
84 | |||
85 | /** |
||
86 | * @var Comment[] |
||
87 | */ |
||
88 | private $comments = []; |
||
89 | |||
90 | /** |
||
91 | * We don't want to have any reference to Doctrine in the Domain, so we remove the Collection type hint from here. |
||
92 | * |
||
93 | * @var Tag[] |
||
94 | */ |
||
95 | private $tags = []; |
||
96 | |||
97 | /** |
||
98 | * @var bool |
||
99 | */ |
||
100 | private $isNewPost = false; |
||
101 | |||
102 | public function __construct() |
||
108 | |||
109 | public function getId(): PostId |
||
113 | |||
114 | public function getTitle(): ?string |
||
118 | |||
119 | public function setTitle(string $title): void |
||
127 | |||
128 | public function getSlug(): ?string |
||
132 | |||
133 | public function postfixSlug(string $suffix): void |
||
143 | |||
144 | public function getContent(): ?string |
||
148 | |||
149 | public function setContent(string $content): void |
||
153 | |||
154 | public function getPublishedAt(): DateTimeImmutable |
||
158 | |||
159 | public function setPublishedAt(DateTimeInterface $publishedAt): void |
||
168 | |||
169 | public function getAuthorId(): UserId |
||
173 | |||
174 | public function setAuthorId(UserId $authorId): void |
||
178 | |||
179 | /** |
||
180 | * We don't want to have here any reference to doctrine, so we remove the Collection type hint from everywhere. |
||
181 | * The safest is to treat it as an array but we can't type hint it with 'array' because we might actually |
||
182 | * return an Collection. |
||
183 | * |
||
184 | * @return Comment[] |
||
185 | */ |
||
186 | public function getComments() |
||
190 | |||
191 | public function addComment(Comment $comment): void |
||
198 | |||
199 | /** |
||
200 | * This method is not used, but I will leave it here as an example |
||
201 | */ |
||
202 | public function removeComment(Comment $comment): void |
||
210 | |||
211 | public function getSummary(): ?string |
||
215 | |||
216 | public function setSummary(string $summary): void |
||
220 | |||
221 | public function addTag(Tag ...$tags): void |
||
229 | |||
230 | /** |
||
231 | * This method is not used, but I will leave it here as an example |
||
232 | */ |
||
233 | public function removeTag(Tag $tag): void |
||
239 | |||
240 | /** |
||
241 | * @return Tag[] |
||
242 | */ |
||
243 | public function getTags(): array |
||
253 | |||
254 | /** |
||
255 | * Since we don't type hint `tags` as a Doctrine collection, the `clear()` method below is |
||
256 | * not recognized by the IDE, as it is not even possible to call a method on an array. |
||
257 | * |
||
258 | * So we create this method here to encapsulate that operation, and minimize the issue, treating the `Post` entity |
||
259 | * as an aggregate root. |
||
260 | * |
||
261 | * It is also a good practise to encapsulate these chained operations, |
||
262 | * from an object calisthenics point of view. |
||
263 | */ |
||
264 | public function clearTags(): void |
||
274 | |||
275 | private function contains($item, $list): bool |
||
280 | |||
281 | /** |
||
282 | * @return false|int|string |
||
283 | */ |
||
284 | private function getKey($item, $list) |
||
289 | } |
||
290 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.