Total Complexity | 45 |
Total Lines | 334 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Team 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 Team, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Team |
||
19 | { |
||
20 | /** |
||
21 | * @ORM\Id() |
||
22 | * @ORM\GeneratedValue() |
||
23 | * @ORM\Column(type="integer") |
||
24 | */ |
||
25 | private $id; |
||
26 | |||
27 | /** |
||
28 | * @ORM\Column(type="string", length=255) |
||
29 | */ |
||
30 | private $name; |
||
31 | |||
32 | /** |
||
33 | * @ORM\ManyToOne(targetEntity=Coach::class, inversedBy="teams") |
||
34 | * @ORM\JoinColumn(nullable=false) |
||
35 | */ |
||
36 | private $coach; |
||
37 | |||
38 | /** |
||
39 | * @ORM\ManyToOne(targetEntity=Rule::class, inversedBy="teams") |
||
40 | */ |
||
41 | private $rule; |
||
42 | |||
43 | /** |
||
44 | * @ORM\Column(type="text", nullable=true) |
||
45 | */ |
||
46 | private $anthem; |
||
47 | |||
48 | /** |
||
49 | * @ORM\Column(type="text", nullable=true) |
||
50 | */ |
||
51 | private $fluff; |
||
52 | |||
53 | /** |
||
54 | * @ORM\Column(type="string", length=50) |
||
55 | */ |
||
56 | private $roster; |
||
57 | |||
58 | /** |
||
59 | * @ORM\OneToMany(targetEntity=Player::class, fetch="EAGER", mappedBy="team", orphanRemoval=true, cascade={"persist", "remove"}) |
||
60 | * @ORM\OrderBy({"number"="ASC"}) |
||
61 | */ |
||
62 | private $players; |
||
63 | |||
64 | /** |
||
65 | * @ORM\Column(type="string", nullable=true) |
||
66 | */ |
||
67 | private $logoFilename; |
||
68 | |||
69 | /** |
||
70 | * @ORM\Column(type="string", nullable=true) |
||
71 | */ |
||
72 | private $logoMimeType; |
||
73 | |||
74 | /** |
||
75 | * @ORM\Column(type="string", nullable=true) |
||
76 | */ |
||
77 | private $coverFilename; |
||
78 | |||
79 | /** |
||
80 | * @ORM\Column(type="string", nullable=true) |
||
81 | */ |
||
82 | private $coverMimeType; |
||
83 | |||
84 | /** |
||
85 | * @ORM\Column(type="boolean") |
||
86 | */ |
||
87 | private $ready = false; |
||
88 | |||
89 | /** |
||
90 | * @ORM\Column(type="boolean") |
||
91 | */ |
||
92 | private $lockedByManagment = false; |
||
93 | |||
94 | /** |
||
95 | * @ORM\OneToMany(targetEntity=TeamVersion::class, fetch="EAGER", mappedBy="team", orphanRemoval=true, cascade={"persist", "remove"}) |
||
96 | * @ORM\OrderBy({"id"="DESC"}) |
||
97 | */ |
||
98 | private $versions; |
||
99 | |||
100 | public function __construct() |
||
101 | { |
||
102 | $this->players = new ArrayCollection(); |
||
103 | $this->versions = new ArrayCollection(); |
||
104 | } |
||
105 | |||
106 | public function getId(): ?int |
||
107 | { |
||
108 | return $this->id; |
||
109 | } |
||
110 | |||
111 | public function getName(): ?string |
||
112 | { |
||
113 | return $this->name; |
||
114 | } |
||
115 | |||
116 | public function setName(string $name): self |
||
117 | { |
||
118 | $this->name = $name; |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | public function getCoach(): ?Coach |
||
124 | { |
||
125 | return $this->coach; |
||
126 | } |
||
127 | |||
128 | public function setCoach(?Coach $coach): self |
||
129 | { |
||
130 | $this->coach = $coach; |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | public function getRule(): ?Rule |
||
136 | { |
||
137 | return $this->rule; |
||
138 | } |
||
139 | |||
140 | public function setRule(?Rule $rule): self |
||
141 | { |
||
142 | $this->rule = $rule; |
||
143 | |||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function getAnthem(): ?string |
||
148 | { |
||
149 | return $this->anthem; |
||
150 | } |
||
151 | |||
152 | public function setAnthem(?string $anthem): self |
||
153 | { |
||
154 | $this->anthem = $anthem; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | public function getFluff(): ?string |
||
160 | { |
||
161 | return $this->fluff; |
||
162 | } |
||
163 | |||
164 | public function setFluff(?string $fluff): self |
||
165 | { |
||
166 | $this->fluff = $fluff; |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | public function getRoster(): ?string |
||
172 | { |
||
173 | return $this->roster; |
||
174 | } |
||
175 | |||
176 | public function setRoster(string $roster): self |
||
177 | { |
||
178 | $this->roster = $roster; |
||
179 | |||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @return Collection|Player[] |
||
185 | */ |
||
186 | public function getPlayers(): Collection |
||
187 | { |
||
188 | return $this->players; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return Collection|Player[] |
||
193 | */ |
||
194 | public function getAvailablePlayers(): Collection |
||
195 | { |
||
196 | $criteria = Criteria::create() |
||
197 | ->andWhere(Criteria::expr()->eq('dead', false)) |
||
198 | ->andWhere(Criteria::expr()->eq('fire', false)) |
||
199 | ->orderBy(['number' => 'ASC']) |
||
200 | ; |
||
201 | return $this->players->matching($criteria); |
||
202 | } |
||
203 | |||
204 | public function getAvailablePlayersSheet(): Collection |
||
205 | { |
||
206 | // In want to have 16 players in the list, no less, no more |
||
207 | $usedNumbers = []; |
||
208 | $newPlayerList = $this->getAvailablePlayers(); |
||
209 | foreach ($newPlayerList as $player) { |
||
210 | $usedNumbers[$player->getNumber()] = $player; |
||
211 | } |
||
212 | for ($i=1; $i<=16; $i++) { |
||
213 | if (!isset($usedNumbers[$i])) { |
||
214 | $newPlayerList->add((new Player())->setNumber($i)); |
||
215 | } |
||
216 | } |
||
217 | $criteria = Criteria::create(); |
||
218 | $criteria->orderBy(['number' => 'ASC']); |
||
219 | return $newPlayerList->matching($criteria); |
||
220 | } |
||
221 | |||
222 | public function addPlayer(Player $player): self |
||
223 | { |
||
224 | if (!$this->players->contains($player)) { |
||
225 | $this->players[] = $player; |
||
226 | $player->setTeam($this); |
||
227 | } |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | public function removePlayer(Player $player): self |
||
233 | { |
||
234 | if ($this->players->contains($player)) { |
||
235 | $this->players->removeElement($player); |
||
236 | // set the owning side to null (unless already changed) |
||
237 | if ($player->getTeam() === $this) { |
||
238 | $player->setTeam(null); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | public function getLogoFilename(): ?string |
||
246 | { |
||
247 | return $this->logoFilename; |
||
248 | } |
||
249 | |||
250 | public function setLogoFilename(?string $logoFilename): self |
||
251 | { |
||
252 | $this->logoFilename = $logoFilename; |
||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | public function getLogoMimeType(): ?string |
||
257 | { |
||
258 | return $this->logoMimeType; |
||
259 | } |
||
260 | |||
261 | public function setLogoMimeType(?string $logoMimeType): self |
||
262 | { |
||
263 | $this->logoMimeType = $logoMimeType; |
||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | public function getCoverFilename(): ?string |
||
268 | { |
||
269 | return $this->coverFilename; |
||
270 | } |
||
271 | |||
272 | public function setCoverFilename(?string $coverFilename): self |
||
273 | { |
||
274 | $this->coverFilename = $coverFilename; |
||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | public function getCoverMimeType(): ?string |
||
279 | { |
||
280 | return $this->coverMimeType; |
||
281 | } |
||
282 | |||
283 | public function setCoverMimeType(?string $coverMimeType): self |
||
287 | } |
||
288 | |||
289 | public function getReady(): ?bool |
||
290 | { |
||
291 | return $this->ready; |
||
292 | } |
||
293 | |||
294 | public function isReady(): ?bool |
||
295 | { |
||
296 | return $this->getReady(); |
||
297 | } |
||
298 | |||
299 | public function setReady(bool $ready): self |
||
300 | { |
||
301 | $this->ready = $ready; |
||
302 | |||
303 | return $this; |
||
304 | } |
||
305 | |||
306 | public function getLockedByManagment(): ?bool |
||
307 | { |
||
308 | return $this->lockedByManagment; |
||
309 | } |
||
310 | |||
311 | public function isLockedByManagment(): ?bool |
||
312 | { |
||
313 | return $this->getLockedByManagment(); |
||
314 | } |
||
315 | |||
316 | public function setLockedByManagment(bool $lockedByManagment): self |
||
317 | { |
||
318 | $this->lockedByManagment = $lockedByManagment; |
||
319 | |||
320 | return $this; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @return Collection|TeamVersion[] |
||
325 | */ |
||
326 | public function getVersions(): Collection |
||
327 | { |
||
328 | return $this->versions; |
||
329 | } |
||
330 | |||
331 | public function addVersion(TeamVersion $version): self |
||
339 | } |
||
340 | |||
341 | public function removeVersion(TeamVersion $version): self |
||
342 | { |
||
343 | if ($this->versions->contains($version)) { |
||
344 | $this->versions->removeElement($version); |
||
345 | // set the owning side to null (unless already changed) |
||
346 | if ($version->getTeam() === $this) { |
||
347 | $version->setTeam(null); |
||
348 | } |
||
349 | } |
||
350 | |||
351 | return $this; |
||
352 | } |
||
353 | } |
||
354 |