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 |
||
| 17 | class ForecastOfOpportunities |
||
| 18 | { |
||
| 19 | /** @var RegistryInterface */ |
||
| 20 | protected $doctrine; |
||
| 21 | |||
| 22 | /** @var NumberFormatter */ |
||
| 23 | protected $numberFormatter; |
||
| 24 | |||
| 25 | /** @var DateTimeFormatter */ |
||
| 26 | protected $dateTimeFormatter; |
||
| 27 | |||
| 28 | /** @var AclHelper */ |
||
| 29 | protected $aclHelper; |
||
| 30 | |||
| 31 | /** @var TranslatorInterface */ |
||
| 32 | protected $translator; |
||
| 33 | |||
| 34 | /** @var array */ |
||
| 35 | protected $ownersValues; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param RegistryInterface $doctrine |
||
| 39 | * @param NumberFormatter $numberFormatter |
||
| 40 | * @param DateTimeFormatter $dateTimeFormatter |
||
| 41 | * @param AclHelper $aclHelper |
||
| 42 | * @param TranslatorInterface $translator |
||
| 43 | */ |
||
| 44 | public function __construct( |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param WidgetOptionBag $widgetOptions |
||
| 60 | * @param string $getterName |
||
| 61 | * @param string $dataType |
||
| 62 | * @param bool $lessIsBetter |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getForecastOfOpportunitiesValues( |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param array $ownerIds |
||
| 97 | * @param null $compareToDate |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | protected function getInProgressValues($ownerIds, $compareToDate = null) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $ownerIds |
||
| 109 | * @param null $compareToDate |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | protected function getTotalForecastValues($ownerIds, $compareToDate = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $ownerIds |
||
| 121 | * @param null $compareToDate |
||
| 122 | * @return int |
||
| 123 | */ |
||
| 124 | protected function getWeightedForecastValues($ownerIds, $compareToDate = null) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param array $ownerIds |
||
| 133 | * @param \DateTime|string|int $date |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | protected function getOwnersValues(array $ownerIds, $date) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param mixed $value |
||
| 150 | * @param string $type |
||
| 151 | * @param bool $isDeviant |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | protected function formatValue($value, $type = '', $isDeviant = false) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param $dataType |
||
| 185 | * @param $lessIsBetter |
||
| 186 | * @param $pastResult |
||
| 187 | * @param $deviation |
||
| 188 | * @param $result |
||
| 189 | * |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | protected function prepareData($dataType, $lessIsBetter, $pastResult, $deviation, $result) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get is positive value |
||
| 216 | * |
||
| 217 | * @param $lessIsBetter |
||
| 218 | * @param $deviation |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | protected function isPositive($lessIsBetter, $deviation) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param WidgetOptionBag $widgetOptions |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | protected function getOwnerIds(WidgetOptionBag $widgetOptions) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param WidgetOptionBag $widgetOptions |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | protected function getBusinessUnitsIds(WidgetOptionBag $widgetOptions) |
||
| 292 | } |
||
| 293 |
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: