Complex classes like WordsList 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 WordsList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class WordsList |
||
11 | { |
||
12 | const SORT_ALPHA = 'text'; |
||
13 | const SORT_COUNT = 'count'; |
||
14 | const SORT_ANGLE = 'angle'; |
||
15 | |||
16 | const SORT_ASC = 'asc'; |
||
17 | const SORT_DESC = 'desc'; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | * @JMS\Type("string") |
||
22 | */ |
||
23 | protected $name; |
||
24 | |||
25 | /** |
||
26 | * @var ArrayCollection |
||
27 | * @JMS\Type("ArrayCollection<SixtyNine\Cloud\Model\Word>") |
||
28 | */ |
||
29 | protected $words; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | * @JMS\Exclude() |
||
34 | */ |
||
35 | protected $allowedSortBy = array(self::SORT_ALPHA, self::SORT_ANGLE, self::SORT_COUNT); |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | * @JMS\Exclude() |
||
40 | */ |
||
41 | protected $allowedSortOrder = array(self::SORT_ASC, self::SORT_DESC); |
||
42 | |||
43 | /** |
||
44 | * Constructor |
||
45 | */ |
||
46 | 8 | public function __construct() |
|
50 | |||
51 | /** |
||
52 | * @param string $name |
||
53 | * @return $this |
||
54 | */ |
||
55 | 7 | public function setName($name) |
|
60 | |||
61 | /** |
||
62 | * @return string |
||
63 | */ |
||
64 | 3 | public function getName() |
|
68 | |||
69 | /** |
||
70 | * Check if a Word with the same text already exists in the Cloud |
||
71 | * @param string $text |
||
72 | * @return null|Word |
||
73 | */ |
||
74 | 7 | public function getWordForText($text) |
|
84 | |||
85 | /** |
||
86 | * @param Word $word |
||
87 | * @return $this |
||
88 | */ |
||
89 | 7 | public function addWord(Word $word) |
|
97 | |||
98 | /** |
||
99 | * @param Word $word |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function removeWord(Word $word) |
||
107 | |||
108 | /** |
||
109 | * @return ArrayCollection |
||
110 | */ |
||
111 | 6 | public function getWords() |
|
115 | |||
116 | /** |
||
117 | * @return ArrayCollection |
||
118 | */ |
||
119 | 3 | public function getWordsOrdered() |
|
127 | |||
128 | /** |
||
129 | * @param string $sortBy |
||
130 | * @param string $sortOrder |
||
131 | * @throws \InvalidArgumentException |
||
132 | */ |
||
133 | public function sortWords($sortBy, $sortOrder) |
||
169 | |||
170 | /** |
||
171 | * @return int |
||
172 | * @JMS\VirtualProperty |
||
173 | * @JMS\SerializedName("max-count") |
||
174 | */ |
||
175 | 4 | public function getWordsMaxCount() |
|
186 | |||
187 | /** |
||
188 | * @return int |
||
189 | * @JMS\VirtualProperty |
||
190 | * @JMS\SerializedName("count") |
||
191 | */ |
||
192 | 2 | public function getWordsCount() |
|
196 | |||
197 | /** |
||
198 | * @param string $words |
||
199 | * @param Filters $filters |
||
200 | * @param int $maxWords |
||
201 | */ |
||
202 | 7 | public function importWords($words, Filters $filters = null, $maxWords = 100) |
|
213 | |||
214 | /** |
||
215 | * @param string $word |
||
216 | * @param Filters $filters |
||
217 | */ |
||
218 | 7 | public function importWord($word, Filters $filters = null) |
|
243 | |||
244 | /** |
||
245 | * @param string $html |
||
246 | * @param Filters $filters |
||
247 | * @param int $maxWords |
||
248 | */ |
||
249 | 1 | public function importHtml($html, Filters $filters = null, $maxWords = 100) |
|
269 | |||
270 | /** |
||
271 | * @param string $url |
||
272 | * @param Filters $filters |
||
273 | * @param int $maxWords |
||
274 | */ |
||
275 | 1 | public function importUrl($url, Filters $filters = null, $maxWords = 100) |
|
279 | |||
280 | /** |
||
281 | * Apply the given $filters to the $list. |
||
282 | * @param Filters $filters |
||
283 | */ |
||
284 | public function filterWords(Filters $filters) |
||
296 | |||
297 | /** |
||
298 | * @param int $verticalProbability |
||
299 | */ |
||
300 | 3 | public function randomizeOrientation($verticalProbability = 50) |
|
313 | |||
314 | /** |
||
315 | * @param ColorGeneratorInterface $colorGenerator |
||
316 | */ |
||
317 | 3 | public function randomizeColors(ColorGeneratorInterface $colorGenerator) |
|
324 | |||
325 | } |
||
326 | |||
327 |
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: