Complex classes like BaseContent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BaseContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class BaseContent implements ContentInterface |
||
20 | { |
||
21 | use AuthorsAwareTrait; |
||
22 | |||
23 | /** |
||
24 | * @var mixed |
||
25 | */ |
||
26 | protected $id; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $guid; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $headline; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $byline; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $slugline; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $language; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $subjects = []; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $type; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $places = []; |
||
67 | |||
68 | /** |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $services = []; |
||
72 | |||
73 | /** |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $located; |
||
77 | |||
78 | /** |
||
79 | * @var int |
||
80 | */ |
||
81 | protected $urgency = 0; |
||
82 | |||
83 | /** |
||
84 | * @var int |
||
85 | */ |
||
86 | protected $priority; |
||
87 | |||
88 | /** |
||
89 | * @var int |
||
90 | */ |
||
91 | protected $version; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $genre; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $edNote; |
||
102 | |||
103 | /** |
||
104 | * @var string |
||
105 | */ |
||
106 | protected $description; |
||
107 | |||
108 | /** |
||
109 | * @var array |
||
110 | */ |
||
111 | protected $keywords = []; |
||
112 | |||
113 | /** |
||
114 | * @var string |
||
115 | */ |
||
116 | protected $pubStatus = ContentInterface::STATUS_USABLE; |
||
117 | |||
118 | /** |
||
119 | * @var string|null |
||
120 | */ |
||
121 | protected $evolvedFrom; |
||
122 | |||
123 | /** |
||
124 | * @var string|null |
||
125 | */ |
||
126 | protected $source; |
||
127 | |||
128 | /** |
||
129 | * @var array |
||
130 | */ |
||
131 | protected $extra = []; |
||
132 | |||
133 | /** |
||
134 | * @var \DateTimeInterface|null |
||
135 | */ |
||
136 | protected $firstPublishedAt; |
||
137 | |||
138 | /** |
||
139 | * @var string|null |
||
140 | */ |
||
141 | protected $copyrightNotice; |
||
142 | |||
143 | /** |
||
144 | * @var string|null |
||
145 | */ |
||
146 | protected $copyrightHolder; |
||
147 | |||
148 | public function __construct() |
||
152 | |||
153 | /** |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function getId() |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function setId($id) |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function getByline() |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function setByline($byline) |
||
184 | |||
185 | /** |
||
186 | * {@inheritdoc} |
||
187 | */ |
||
188 | public function getSlugline() |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function setSlugline($slugline) |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function getLanguage() |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | public function setLanguage($language) |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | */ |
||
220 | public function getSubjects() |
||
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | public function setSubjects(array $subjects = []) |
||
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | public function getType() |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function setType($type) |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function getPlaces() |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function setPlaces($places) |
||
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | public function getLocated() |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function setLocated($located) |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function getUrgency() |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | public function setUrgency($urgency) |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function getPriority() |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | public function setPriority($priority) |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function getVersion() |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function setVersion($version) |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function getHeadline() |
||
336 | |||
337 | /** |
||
338 | * {@inheritdoc} |
||
339 | */ |
||
340 | public function setHeadline($headline) |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | public function getGuid() |
||
352 | |||
353 | /** |
||
354 | * {@inheritdoc} |
||
355 | */ |
||
356 | public function setGuid($guid) |
||
360 | |||
361 | /** |
||
362 | * {@inheritdoc} |
||
363 | */ |
||
364 | public function getServices() |
||
368 | |||
369 | /** |
||
370 | * {@inheritdoc} |
||
371 | */ |
||
372 | public function getServicesNames(): array |
||
376 | |||
377 | /** |
||
378 | * {@inheritdoc} |
||
379 | */ |
||
380 | public function getServicesCodes(): array |
||
384 | |||
385 | public function getSubjectsSchemes(): array |
||
389 | |||
390 | public function getSubjectsNames(): array |
||
394 | |||
395 | private function mapNames(array $values): array |
||
405 | |||
406 | private function mapCodes(array $values): array |
||
416 | |||
417 | private function mapSchemes(array $values): array |
||
427 | |||
428 | /** |
||
429 | * {@inheritdoc} |
||
430 | */ |
||
431 | public function setServices(array $services = []) |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | public function getEdNote() |
||
443 | |||
444 | /** |
||
445 | * {@inheritdoc} |
||
446 | */ |
||
447 | public function setEdNote($edNote) |
||
451 | |||
452 | /** |
||
453 | * {@inheritdoc} |
||
454 | */ |
||
455 | public function getGenre() |
||
459 | |||
460 | /** |
||
461 | * {@inheritdoc} |
||
462 | */ |
||
463 | public function setGenre($genre) |
||
467 | |||
468 | /** |
||
469 | * @return string |
||
470 | */ |
||
471 | public function getDescription() |
||
475 | |||
476 | /** |
||
477 | * @param string $description |
||
478 | */ |
||
479 | public function setDescription($description) |
||
483 | |||
484 | /** |
||
485 | * {@inheritdoc} |
||
486 | */ |
||
487 | public function getMetadata() |
||
504 | |||
505 | /** |
||
506 | * {@inheritdoc} |
||
507 | */ |
||
508 | public function getKeywords() |
||
512 | |||
513 | /** |
||
514 | * {@inheritdoc} |
||
515 | */ |
||
516 | public function setKeywords(array $keywords) |
||
520 | |||
521 | /** |
||
522 | * {@inheritdoc} |
||
523 | */ |
||
524 | public function getPubStatus() |
||
528 | |||
529 | /** |
||
530 | * {@inheritdoc} |
||
531 | */ |
||
532 | public function setPubStatus(string $pubStatus) |
||
536 | |||
537 | /** |
||
538 | * {@inheritdoc} |
||
539 | */ |
||
540 | public function getEvolvedFrom() |
||
544 | |||
545 | /** |
||
546 | * {@inheritdoc} |
||
547 | */ |
||
548 | public function setEvolvedFrom(string $evolvedFrom) |
||
552 | |||
553 | /** |
||
554 | * {@inheritdoc} |
||
555 | */ |
||
556 | public function getSource() |
||
560 | |||
561 | /** |
||
562 | * {@inheritdoc} |
||
563 | */ |
||
564 | public function setSource($source) |
||
568 | |||
569 | /** |
||
570 | * {@inheritdoc} |
||
571 | */ |
||
572 | public function getExtra(): array |
||
580 | |||
581 | /** |
||
582 | * {@inheritdoc} |
||
583 | */ |
||
584 | public function setExtra(?array $extra): void |
||
588 | |||
589 | public function getFirstPublishedAt(): ?\DateTimeInterface |
||
593 | |||
594 | public function setFirstPublishedAt(?\DateTimeInterface $firstPublishedAt): void |
||
598 | |||
599 | public function getCopyrightNotice(): ?string |
||
603 | |||
604 | public function setCopyrightNotice(?string $copyrightNotice): void |
||
608 | |||
609 | public function getCopyrightHolder(): ?string |
||
613 | |||
614 | public function setCopyrightHolder(?string $copyrightHolder): void |
||
618 | } |
||
619 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.