Completed
Push — master ( 0a3a64...75d382 )
by Julien
02:47
created

PreliminaryFight   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
lcom 1
cbo 1
dl 10
loc 70
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 2
B saveFights() 0 30 5
A saveFight() 0 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Illuminate\Support\Collection;
6
7
class PreliminaryFight extends Fight
8
{
9
10 View Code Duplication
    public function __construct(Fight $fight = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
    {
12
        if ($fight!=null){
13
            $this->id= $fight->id;
14
            $this->short_id= $fight->short_id;
15
            $this->fighters_group_id= $fight->fighters_group_id;
16
            $this->c1= $fight->c1;
17
            $this->c2= $fight->c2;
18
        }
19
    }
20
21
    /**
22
     * Save a Fight.
23
     *
24
     * @param Collection $groups
25
     * @param int $numGroup
26
     */
27
    public static function saveFights($groups, $numGroup = 1)
28
    {
29
        $competitor1 = $competitor2 = null;
30
        $order = 1;
31
32
        foreach ($groups as $group) {
33
34
            $fighters = $group->getFighters();
35
36
            $fighter1 = $fighters->get(0);
37
            $fighter2 = $fighters->get(1);
38
            $fighter3 = $fighters->get(2);
39
40
            switch ($numGroup) {
41
                case 1:
42
                    $competitor1 = $fighter1;
43
                    $competitor2 = $fighter2;
44
                    break;
45
                case 2:
46
                    $competitor1 = $fighter2;
47
                    $competitor2 = $fighter3;
48
                    break;
49
                case 3:
50
                    $competitor1 = $fighter3;
51
                    $competitor2 = $fighter1;
52
                    break;
53
            }
54
            $order = self::saveFight($group, $competitor1, $competitor2, $order);
55
        }
56
    }
57
58
    /**
59
     * @param $group
60
     * @param $competitor1
61
     * @param $competitor2
62
     * @param $order
63
     * @return mixed
64
     */
65
    private static function saveFight($group, $competitor1, $competitor2, $order)
66
    {
67
        $fight = new Fight();
68
        $fight->fighters_group_id = $group->id;
69
        $fight->c1 = $competitor1 != null ? $competitor1->id : null;
70
        $fight->c2 = $competitor2 != null ? $competitor2->id : null;
71
        $fight->short_id = $order++;
72
        $fight->area = $group->area;
73
        $fight->save();
74
        return $order;
75
    }
76
}