Complex classes like ForecastOfOpportunities 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 ForecastOfOpportunities, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ForecastOfOpportunities |
||
| 19 | { |
||
| 20 | /** @var RegistryInterface */ |
||
| 21 | protected $doctrine; |
||
| 22 | |||
| 23 | /** @var NumberFormatter */ |
||
| 24 | protected $numberFormatter; |
||
| 25 | |||
| 26 | /** @var DateTimeFormatter */ |
||
| 27 | protected $dateTimeFormatter; |
||
| 28 | |||
| 29 | /** @var AclHelper */ |
||
| 30 | protected $aclHelper; |
||
| 31 | |||
| 32 | /** @var TranslatorInterface */ |
||
| 33 | protected $translator; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | protected $ownersValues; |
||
| 37 | |||
| 38 | /** @var array */ |
||
| 39 | protected $ownerIds = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param RegistryInterface $doctrine |
||
| 43 | * @param NumberFormatter $numberFormatter |
||
| 44 | * @param DateTimeFormatter $dateTimeFormatter |
||
| 45 | * @param AclHelper $aclHelper |
||
| 46 | * @param TranslatorInterface $translator |
||
| 47 | */ |
||
| 48 | public function __construct( |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param WidgetOptionBag $widgetOptions |
||
| 64 | * @param string $getterName |
||
| 65 | * @param string $dataType |
||
| 66 | * @param bool $lessIsBetter |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function getForecastOfOpportunitiesValues( |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param array $ownerIds |
||
| 101 | * @param null $compareToDate |
||
| 102 | * @return int |
||
| 103 | */ |
||
| 104 | protected function getInProgressValues($ownerIds, $compareToDate = null) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array $ownerIds |
||
| 113 | * @param null $compareToDate |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | protected function getTotalForecastValues($ownerIds, $compareToDate = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $ownerIds |
||
| 125 | * @param null $compareToDate |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | protected function getWeightedForecastValues($ownerIds, $compareToDate = null) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param array $ownerIds |
||
| 137 | * @param \DateTime|string|int $date |
||
| 138 | * @return mixed |
||
| 139 | */ |
||
| 140 | protected function getOwnersValues(array $ownerIds, $date) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param mixed $value |
||
| 154 | * @param string $type |
||
| 155 | * @param bool $isDeviant |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | protected function formatValue($value, $type = '', $isDeviant = false) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param $dataType |
||
| 189 | * @param $lessIsBetter |
||
| 190 | * @param $pastResult |
||
| 191 | * @param $deviation |
||
| 192 | * @param $result |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | protected function prepareData($dataType, $lessIsBetter, $pastResult, $deviation, $result) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get is positive value |
||
| 220 | * |
||
| 221 | * @param $lessIsBetter |
||
| 222 | * @param $deviation |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | protected function isPositive($lessIsBetter, $deviation) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param WidgetOptionBag $widgetOptions |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | protected function getOwnerIds(WidgetOptionBag $widgetOptions) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param int[] $businessUnitIds |
||
| 266 | * |
||
| 267 | * @return int[] |
||
| 268 | */ |
||
| 269 | protected function getUserOwnerIds(array $businessUnitIds) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param WidgetOptionBag $widgetOptions |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | protected function getBusinessUnitsIds(WidgetOptionBag $widgetOptions) |
||
| 307 | } |
||
| 308 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: