Conditions | 9 |
Paths | 28 |
Total Lines | 87 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | public function run() |
||
35 | { |
||
36 | // If previous trees already exist, delete all |
||
37 | $this->championship->tree()->delete(); |
||
38 | $tree = new Collection(); |
||
39 | |||
40 | // Get Areas |
||
41 | $areas = $this->settings->fightingAreas; |
||
42 | |||
43 | |||
44 | $this->championship->category->isTeam() |
||
45 | ? $fighters = $this->championship->teams |
||
46 | : $fighters = $this->championship->competitors; |
||
47 | |||
48 | |||
49 | if ($fighters->count() / $areas < config('kendo-tournaments.MIN_COMPETITORS_X_AREA')) { |
||
50 | throw new TreeGenerationException(trans('msg.min_competitor_required', ['number' => Config::get('kendo-tournaments.MIN_COMPETITORS_X_AREA')])); |
||
51 | |||
52 | } |
||
53 | // Get Competitor's / Team list ordered by entities ( Federation, Assoc, Club, etc...) |
||
54 | $fightersByEntity = $this->getFightersByEntity($fighters); |
||
55 | |||
56 | // Chunk user by areas |
||
57 | |||
58 | $usersByArea = $fightersByEntity->chunk(sizeof($fightersByEntity) / $areas); |
||
59 | |||
60 | $area = 1; |
||
61 | |||
62 | // loop on areas |
||
63 | foreach ($usersByArea as $fightersByEntity) { |
||
64 | |||
65 | // Chunking to make small round robin groups |
||
66 | if ($this->championship->hasPreliminary()) { |
||
67 | $fightersGroup = $fightersByEntity->chunk($this->settings->preliminaryGroupSize)->shuffle(); |
||
68 | |||
69 | } else if ($this->championship->isDirectEliminationType()) { |
||
70 | $fightersGroup = $fightersByEntity->chunk(2)->shuffle(); |
||
71 | } else { |
||
72 | $fightersGroup = $fightersByEntity; |
||
73 | |||
74 | // // Not so good, Round Robin has no trees |
||
75 | // $round = new Round; |
||
76 | // $round->area = $area; |
||
77 | // $round->order = 1; |
||
78 | // $round->championship_id = $this->championship->id; |
||
79 | // if ($this->championship->category->isTeam()) { |
||
80 | // $round->isTeam = 1; |
||
81 | // } |
||
82 | // $round->save(); |
||
83 | // $tree->push($round); |
||
84 | } |
||
85 | |||
86 | $order = 1; |
||
87 | |||
88 | // Before doing anything, check last group if numUser = 1 |
||
89 | foreach ($fightersGroup as $fighters) { |
||
90 | |||
91 | $fighters = $fighters->pluck('id')->shuffle(); |
||
92 | |||
93 | $round = new Round; |
||
94 | $round->area = $area; |
||
95 | $round->order = $order; |
||
96 | // if ($this->championship->category->isTeam()) { |
||
97 | $round->championship_id = $this->championship->id; |
||
98 | |||
99 | $round->save(); |
||
100 | $tree->push($round); |
||
101 | $order++; |
||
102 | |||
103 | // Add all competitors to Pivot Table |
||
104 | if ($this->championship->category->isTeam()) { |
||
105 | $round->teams()->sync($fighters); |
||
106 | } else { |
||
107 | $round->competitors()->detach(); |
||
108 | foreach ($fighters as $fighter){ |
||
109 | $round->competitors()->attach($fighter); |
||
110 | } |
||
111 | // $round->competitors()->sync($fighters); |
||
112 | } |
||
113 | |||
114 | |||
115 | } |
||
116 | $area++; |
||
117 | } |
||
118 | |||
119 | return $tree; |
||
120 | } |
||
121 | |||
295 | } |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.