| Total Complexity | 40 | 
| Total Lines | 254 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like RuleConfigBuilder 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.
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 RuleConfigBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 19 | class RuleConfigBuilder extends RuleConfigResolver implements RuleBuilderInterface | ||
| 20 | { | ||
| 21 | /** @var ArrayCollection */ | ||
| 22 | private $injuries; | ||
| 23 | /** @var ArrayCollection|Skill[] */ | ||
| 24 | private $skills; | ||
| 25 | /** @var ArrayCollection|InducementType[] */ | ||
| 26 | private $inducementTypes; | ||
| 27 | /** @var ArrayCollection */ | ||
| 28 | private $sppLevels; | ||
| 29 | /** @var ArrayCollection|InducementInterface[] */ | ||
| 30 | private $inducements; | ||
| 31 | /** @var ArrayCollection|RosterInterface[] */ | ||
| 32 | private $rosters; | ||
| 33 | |||
| 34 | protected function build(string $ruleKey, array $rule) | ||
| 35 |     { | ||
| 36 | $treeResolver = new ConfigResolver($this); | ||
| 37 | $rule = $treeResolver->resolve($rule); | ||
| 38 | $this->prepareInjuriesTable($ruleKey, $rule); | ||
| 39 | $this->prepareSppTable($ruleKey, $rule); | ||
| 40 | $this->prepareSkillsTable($ruleKey, $rule); | ||
| 41 | $this->prepareInducementTypes($ruleKey); | ||
| 42 | $this->prepareInducementTable($ruleKey, $rule); | ||
| 43 | $this->prepareRosterTable($ruleKey, $rule); | ||
| 44 | } | ||
| 45 | |||
| 46 | public function getInjuries():ArrayCollection | ||
| 49 | } | ||
| 50 | |||
| 51 | public function setInjuries(ArrayCollection $injuries):self | ||
| 52 |     { | ||
| 53 | $this->injuries = $injuries; | ||
| 54 | return $this; | ||
| 55 | } | ||
| 56 | |||
| 57 | public function getInducementTypes():ArrayCollection | ||
| 58 |     { | ||
| 59 | return $this->inducementTypes; | ||
| 60 | } | ||
| 61 | |||
| 62 | public function setInducementTypes(ArrayCollection $inducementTypes):self | ||
| 63 |     { | ||
| 64 | $this->inducementTypes = $inducementTypes; | ||
| 65 | return $this; | ||
| 66 | } | ||
| 67 | |||
| 68 | public function getInducementType(string $type):InducementType | ||
| 69 |     { | ||
| 70 | return $this->getInducementTypes()->get($type); | ||
|  | |||
| 71 | } | ||
| 72 | |||
| 73 | public function getSppLevels():ArrayCollection | ||
| 74 |     { | ||
| 75 | return $this->sppLevels; | ||
| 76 | } | ||
| 77 | |||
| 78 | public function setSppLevels(ArrayCollection $sppLevels):self | ||
| 79 |     { | ||
| 80 | $this->sppLevels = $sppLevels; | ||
| 81 | return $this; | ||
| 82 | } | ||
| 83 | |||
| 84 | public function getInducementTable():ArrayCollection | ||
| 85 |     { | ||
| 86 | return $this->inducements; | ||
| 87 | } | ||
| 88 | |||
| 89 | public function setInducementTable(ArrayCollection $inducements):self | ||
| 90 |     { | ||
| 91 | $this->inducements = $inducements; | ||
| 92 | return $this; | ||
| 93 | } | ||
| 94 | |||
| 95 | public function getRosters():ArrayCollection | ||
| 96 |     { | ||
| 97 | return $this->rosters; | ||
| 98 | } | ||
| 99 | |||
| 100 | public function setRosters(ArrayCollection $rosters):self | ||
| 101 |     { | ||
| 102 | $this->rosters = $rosters; | ||
| 103 | return $this; | ||
| 104 | } | ||
| 105 | |||
| 106 | public function getSkills():ArrayCollection | ||
| 107 |     { | ||
| 108 | return $this->skills; | ||
| 109 | } | ||
| 110 | |||
| 111 | public function setSkills(ArrayCollection $skills):self | ||
| 112 |     { | ||
| 113 | $this->skills = $skills; | ||
| 114 | return $this; | ||
| 115 | } | ||
| 116 | |||
| 117 | private function prepareInjuriesTable(string $ruleKey, array $rule) | ||
| 118 |     { | ||
| 119 | $this->injuries = new ArrayCollection(); | ||
| 120 |         foreach ($rule['injuries'] as $key => $injury) { | ||
| 121 | $label = CoreTranslation::getInjuryKey($ruleKey, $key); | ||
| 122 | $effectLabel = CoreTranslation::getInjuryEffect($ruleKey, $key); | ||
| 123 |             if (isset($injury['to'])) { | ||
| 124 |                 for ($i = $injury['from']; $i <= $injury['to']; $i++) { | ||
| 125 | $this->injuries->set( | ||
| 126 | $i, | ||
| 127 | (object) ['value' => $i, 'label' => $label, 'effect_label' => $effectLabel, 'effects' => $injury['effects']] | ||
| 128 | ); | ||
| 129 | } | ||
| 130 |             } else { | ||
| 131 | $this->injuries->set( | ||
| 132 | $injury['from'], | ||
| 133 | (object) ['value' => $injury['from'], 'label' => $label, 'effect_label' => $effectLabel, 'effects' => $injury['effects']] | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | private function prepareInducementTypes(string $ruleKey) | ||
| 140 |     { | ||
| 141 | $this->inducementTypes = new ArrayCollection(); | ||
| 142 |         $this->inducementTypes->set('star_players', new InducementType([ | ||
| 143 | 'key' => 'star_players', | ||
| 144 | 'name' => CoreTranslation::getStarPlayerTitle($ruleKey), | ||
| 145 | 'translation_domain' => $ruleKey, | ||
| 146 | ])); | ||
| 147 |         $this->inducementTypes->set('inducements', new InducementType([ | ||
| 148 | 'key' => 'inducements', | ||
| 149 | 'name' => CoreTranslation::getInducementTitle($ruleKey), | ||
| 150 | 'translation_domain' => $ruleKey, | ||
| 151 | ])); | ||
| 152 |         $this->inducementTypes->set('mercenary', new InducementType([ | ||
| 153 | 'key' => 'mercenary', | ||
| 154 | 'name' => CoreTranslation::getMercenaryTitle($ruleKey), | ||
| 155 | 'translation_domain' => $ruleKey, | ||
| 156 | ])); | ||
| 157 | } | ||
| 158 | |||
| 159 | private function prepareSppTable(string $ruleKey, array $rule) | ||
| 160 |     { | ||
| 161 | $spps = new ArrayCollection($rule['spp_levels']); | ||
| 162 |         foreach ($spps as $from => $level) { | ||
| 163 | $to = $spps->next(); | ||
| 164 |             if ($to) { | ||
| 165 |                 for ($i = $from; $i < $spps->indexOf($to); $i++) { | ||
| 166 |                     if (!isset($spps[$i])) { | ||
| 167 | $spps[$i] = $level; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | $spps = $spps->toArray(); | ||
| 173 | ksort($spps); | ||
| 174 | $this->sppLevels = new ArrayCollection($spps); | ||
| 175 | } | ||
| 176 | |||
| 177 | private function prepareSkillsTable(string $ruleKey, array $rule) | ||
| 192 | ]) | ||
| 193 | ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | private function prepareInducementTable(string $ruleKey, array $rule) | ||
| 199 |     { | ||
| 200 | $this->inducements = new ArrayCollection(); | ||
| 201 | |||
| 202 |         foreach ($rule['inducements'] as $key => $value) { | ||
| 203 |             if ($key !== 'star_players') { | ||
| 204 | $inducement = new Inducement([ | ||
| 205 | 'type' => $this->inducementTypes['inducements'], | ||
| 206 | 'key' => join(CoreTranslation::TRANSLATION_GLUE, [$ruleKey, 'inducements', $key]), | ||
| 207 | 'translation_domain' => $ruleKey, | ||
| 208 | 'name' => CoreTranslation::getInducementName($ruleKey, $key), | ||
| 209 | 'max' => $value['max'] ?? 0, | ||
| 210 | 'rosters' => $value['rosters'] ?? null, | ||
| 211 | 'value' => $value['cost'], | ||
| 212 | 'discount_value' => $value['discount_cost'] ?? null, | ||
| 213 | ]); | ||
| 214 |                 if (!$this->inducements->contains($inducement)) { | ||
| 215 | $this->inducements->add($inducement); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | } | ||
| 219 |         foreach ($rule['star_players'] as $key => $starPlayer) { | ||
| 220 | $inducement = $this->createStarPlayerInducement($ruleKey, $key, $starPlayer); | ||
| 221 |             if (!$this->inducements->contains($inducement)) { | ||
| 222 | $this->inducements->add($inducement); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | private function prepareRosterTable(string $ruleKey, array $rule) | ||
| 241 | ]) | ||
| 242 | ); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | private function createStarPlayerInducement(string $ruleKey, string $key, array $starPlayer):InducementInterface | ||
| 247 |     { | ||
| 248 | $options = [ | ||
| 249 | 'type' => $this->inducementTypes['star_players'], | ||
| 250 | 'key' => join(CoreTranslation::TRANSLATION_GLUE, [$ruleKey, 'star_players', $key]), | ||
| 273 | } | ||
| 274 | } | ||
| 275 |