| Total Complexity | 40 |
| Total Lines | 282 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Rule 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 Rule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Rule |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @ORM\Id() |
||
| 19 | * @ORM\GeneratedValue() |
||
| 20 | * @ORM\Column(type="integer") |
||
| 21 | */ |
||
| 22 | private $id; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @ORM\Column(type="string", unique=true, length=255) |
||
| 26 | */ |
||
| 27 | private $ruleKey; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 31 | */ |
||
| 32 | private $ruleDirectory; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @ORM\Column(type="string", length=255) |
||
| 36 | */ |
||
| 37 | private $template; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @ORM\Column(type="string", length=255) |
||
| 41 | */ |
||
| 42 | private $name; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @ORM\Column(type="string", length=255, nullable=true) |
||
| 46 | */ |
||
| 47 | private $description; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @ORM\Column(type="array", nullable=true) |
||
| 51 | */ |
||
| 52 | private $rule = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @ORM\Column(type="boolean") |
||
| 56 | */ |
||
| 57 | private $postBb2020; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @ORM\Column(type="boolean") |
||
| 61 | */ |
||
| 62 | private $readOnly; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @ORM\OneToMany(targetEntity=Team::class, mappedBy="rule") |
||
| 66 | * @Ignore() |
||
| 67 | */ |
||
| 68 | private $teams; |
||
| 69 | |||
| 70 | protected $injuryTable = []; |
||
| 71 | |||
| 72 | public function __construct() |
||
| 73 | { |
||
| 74 | $this->postBb2020 = false; |
||
| 75 | $this->readOnly = false; |
||
| 76 | $this->teams = new ArrayCollection(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function getId(): ?int |
||
| 80 | { |
||
| 81 | return $this->id; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function getRuleKey(): ?string |
||
| 85 | { |
||
| 86 | return $this->ruleKey; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function setRuleKey(string $ruleKey): self |
||
| 90 | { |
||
| 91 | $this->ruleKey = $ruleKey; |
||
| 92 | |||
| 93 | return $this; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function getRuleDirectory(): ?string |
||
| 97 | { |
||
| 98 | return $this->ruleDirectory; |
||
| 99 | } |
||
| 100 | |||
| 101 | public function setRuleDirectory(string $ruleDirectory): self |
||
| 102 | { |
||
| 103 | $this->ruleDirectory = $ruleDirectory; |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | public function getTemplate(): ?string |
||
| 109 | { |
||
| 110 | return $this->template; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function setTemplate(string $template): self |
||
| 114 | { |
||
| 115 | $this->template = $template; |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | public function getName(): ?string |
||
| 121 | { |
||
| 122 | return $this->name; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function setName(string $name): self |
||
| 126 | { |
||
| 127 | $this->name = $name; |
||
| 128 | |||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getDescription(): ?string |
||
| 133 | { |
||
| 134 | return $this->description; |
||
| 135 | } |
||
| 136 | |||
| 137 | public function setDescription(?string $description): self |
||
| 138 | { |
||
| 139 | $this->description = $description; |
||
| 140 | |||
| 141 | return $this; |
||
| 142 | } |
||
| 143 | |||
| 144 | public function getRule(): ?array |
||
| 145 | { |
||
| 146 | return $this->rule; |
||
| 147 | } |
||
| 148 | |||
| 149 | public function setRule(?array $rule): self |
||
| 150 | { |
||
| 151 | $this->rule = $rule; |
||
| 152 | $this->constructInjuryTable($rule); |
||
| 153 | |||
| 154 | return $this; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getPostBb2020(): ?bool |
||
| 158 | { |
||
| 159 | return $this->postBb2020; |
||
| 160 | } |
||
| 161 | |||
| 162 | public function isPostBb2020(): ?bool |
||
| 163 | { |
||
| 164 | return $this->getPostBb2020(); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function setPostBb2020(bool $postBb2020): self |
||
| 168 | { |
||
| 169 | $this->postBb2020 = $postBb2020; |
||
| 170 | |||
| 171 | return $this; |
||
| 172 | } |
||
| 173 | |||
| 174 | public function getReadOnly(): ?bool |
||
| 175 | { |
||
| 176 | return $this->readOnly; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function isReadOnly(): ?bool |
||
| 180 | { |
||
| 181 | return $this->getReadOnly(); |
||
| 182 | } |
||
| 183 | |||
| 184 | public function setReadOnly(bool $readOnly): self |
||
| 185 | { |
||
| 186 | $this->readOnly = $readOnly; |
||
| 187 | |||
| 188 | return $this; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return Collection|Team[] |
||
| 193 | * @Ignore() |
||
| 194 | */ |
||
| 195 | public function getTeams(): Collection |
||
| 196 | { |
||
| 197 | return $this->teams; |
||
| 198 | } |
||
| 199 | |||
| 200 | public function addTeam(Team $team): self |
||
| 201 | { |
||
| 202 | if (!$this->teams->contains($team)) { |
||
| 203 | $this->teams[] = $team; |
||
| 204 | $team->setRule($this); |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this; |
||
| 208 | } |
||
| 209 | |||
| 210 | public function removeTeam(Team $team): self |
||
| 221 | } |
||
| 222 | |||
| 223 | public function __toString(): ?string |
||
| 224 | { |
||
| 225 | return $this->ruleKey; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Methods |
||
| 230 | */ |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Construct Injury Table |
||
| 234 | * |
||
| 235 | * @param array $rule |
||
| 236 | * |
||
| 237 | */ |
||
| 238 | protected function constructInjuryTable($rule) |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get Experience Level For Experience Value |
||
| 253 | * |
||
| 254 | * @param integer $experience |
||
| 255 | * |
||
| 256 | * @return string|boolean |
||
| 257 | */ |
||
| 258 | public function getExperienceLevelForValue($experience) |
||
| 259 | { |
||
| 260 | $datas = $this->getRule(); |
||
| 261 | ksort($datas['experience']); |
||
| 262 | $last = false; |
||
| 263 | foreach ($datas['experience'] as $key => $level) { |
||
| 264 | if ($experience >= $key) { |
||
| 265 | $last = $level; |
||
| 266 | } |
||
| 267 | } |
||
| 268 | return $last; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get Injury For Value |
||
| 273 | * |
||
| 274 | * @param integer $value |
||
| 275 | * |
||
| 276 | * @return array|boolean |
||
| 277 | */ |
||
| 278 | public function getInjury($value) |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get Injury Effect For Injury Key Name |
||
| 288 | * |
||
| 289 | * @param string $keyName |
||
| 290 | * |
||
| 291 | * @return array|boolean |
||
| 292 | */ |
||
| 293 | public function getInjuryEffect($keyName) |
||
| 294 | { |
||
| 295 | $datas = $this->getRule(); |
||
| 296 | return ($datas['injuries'][$keyName]) ? $datas['injuries'][$keyName]['effects'] : false; |
||
| 297 | } |
||
| 298 | } |
||
| 299 |