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 |
||
| 11 | class WordsList |
||
| 12 | { |
||
| 13 | const SORT_ALPHA = 'text'; |
||
| 14 | const SORT_COUNT = 'count'; |
||
| 15 | const SORT_ANGLE = 'angle'; |
||
| 16 | |||
| 17 | const SORT_ASC = 'asc'; |
||
| 18 | const SORT_DESC = 'desc'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | * @JMS\Type("string") |
||
| 23 | */ |
||
| 24 | protected $name; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var ArrayCollection |
||
| 28 | * @JMS\Type("ArrayCollection<SixtyNine\Cloud\Model\Word>") |
||
| 29 | */ |
||
| 30 | protected $words; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | * @JMS\Exclude() |
||
| 35 | */ |
||
| 36 | protected $allowedSortBy = array(self::SORT_ALPHA, self::SORT_ANGLE, self::SORT_COUNT); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | * @JMS\Exclude() |
||
| 41 | */ |
||
| 42 | protected $allowedSortOrder = array(self::SORT_ASC, self::SORT_DESC); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | */ |
||
| 47 | 8 | public function __construct() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $name |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | 7 | public function setName($name) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @return string |
||
| 64 | */ |
||
| 65 | 4 | public function getName() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Check if a Word with the same text already exists in the Cloud |
||
| 72 | * @param string $text |
||
| 73 | * @return null|Word |
||
| 74 | */ |
||
| 75 | 7 | public function getWordForText($text) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param Word $word |
||
| 88 | * @return $this |
||
| 89 | */ |
||
| 90 | 7 | public function addWord(Word $word) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param Word $word |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function removeWord(Word $word) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return ArrayCollection |
||
| 111 | */ |
||
| 112 | 7 | public function getWords() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return ArrayCollection |
||
| 119 | */ |
||
| 120 | 3 | public function getWordsOrdered() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $sortBy |
||
| 131 | * @param string $sortOrder |
||
| 132 | * @throws \InvalidArgumentException |
||
| 133 | */ |
||
| 134 | 1 | public function sortWords($sortBy, $sortOrder) |
|
| 135 | { |
||
| 136 | 1 | Assert::oneOf($sortBy, $this->allowedSortBy, 'Invalid sort by: ' . $sortBy); |
|
| 137 | 1 | Assert::oneOf($sortOrder, $this->allowedSortOrder, 'Invalid sort order: ' . $sortOrder); |
|
| 138 | |||
| 139 | 1 | $sorter = function ($a, $b) use ($sortBy, $sortOrder) { |
|
| 140 | 1 | $method = $sortBy === self::SORT_ANGLE |
|
| 141 | ? 'getOrientation' |
||
| 142 | 1 | : 'get' . ucfirst($sortBy) |
|
| 143 | ; |
||
| 144 | |||
| 145 | 1 | $attr1 = $a->$method(); |
|
| 146 | 1 | $attr2 = $b->$method(); |
|
| 147 | |||
| 148 | 1 | if ($sortOrder === self::SORT_ASC) { |
|
| 149 | return $attr1 > $attr2; |
||
| 150 | } |
||
| 151 | |||
| 152 | 1 | return $attr1 < $attr2; |
|
| 153 | 1 | }; |
|
| 154 | |||
| 155 | |||
| 156 | 1 | $iterator = $this->words->getIterator(); |
|
| 157 | 1 | $iterator->uasort($sorter); |
|
| 158 | |||
| 159 | 1 | $index = 0; |
|
| 160 | 1 | foreach ($iterator as $word) { |
|
| 161 | 1 | $word->setPosition($index); |
|
| 162 | 1 | $index++; |
|
| 163 | } |
||
| 164 | 1 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @return int |
||
| 168 | * @JMS\VirtualProperty |
||
| 169 | * @JMS\SerializedName("max-count") |
||
| 170 | */ |
||
| 171 | 5 | public function getWordsMaxCount() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @return int |
||
| 185 | * @JMS\VirtualProperty |
||
| 186 | * @JMS\SerializedName("count") |
||
| 187 | */ |
||
| 188 | 3 | public function getWordsCount() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $words |
||
| 195 | * @param Filters $filters |
||
| 196 | * @param int $maxWords |
||
| 197 | */ |
||
| 198 | 7 | public function importWords($words, Filters $filters = null, $maxWords = 100) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $word |
||
| 212 | * @param Filters $filters |
||
| 213 | */ |
||
| 214 | 7 | public function importWord($word, Filters $filters = null) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $html |
||
| 242 | * @param Filters $filters |
||
| 243 | * @param int $maxWords |
||
| 244 | */ |
||
| 245 | 1 | public function importHtml($html, Filters $filters = null, $maxWords = 100) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $url |
||
| 268 | * @param Filters $filters |
||
| 269 | * @param int $maxWords |
||
| 270 | */ |
||
| 271 | 1 | public function importUrl($url, Filters $filters = null, $maxWords = 100) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Apply the given $filters to the $list. |
||
| 278 | * @param Filters $filters |
||
| 279 | */ |
||
| 280 | public function filterWords(Filters $filters) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param int $verticalProbability |
||
| 295 | */ |
||
| 296 | 3 | public function randomizeOrientation($verticalProbability = 50) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param ColorGeneratorInterface $colorGenerator |
||
| 312 | */ |
||
| 313 | 3 | public function randomizeColors(ColorGeneratorInterface $colorGenerator) |
|
| 320 | |||
| 321 | } |
||
| 322 | |||
| 323 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.