Test Failed
Push — master ( b6687a...d5e75a )
by Julien
06:32
created

PreliminaryFight   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 15.49 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

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