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