Completed
Push — master ( fe49e9...e809dd )
by Julien
23:40
created

Category   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 131
Duplicated Lines 9.16 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.41%

Importance

Changes 0
Metric Value
dl 12
loc 131
ccs 38
cts 59
cp 0.6441
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasGradeMinAndMax() 0 9 2
A hasAgeMinAndMax() 0 9 2

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 App;
4
5
use Illuminate\Support\Facades\App;
6
use Illuminate\Support\Facades\Auth;
7
8
class Category extends \Xoco70\LaravelTournaments\Models\Category
9
{
10
    const AGE_CUSTOM = 5;
11
12
    /**
13
     * Get All Presets for Categories
14
     * @param null $ruleId
15
     * @return array
16
     */
17 3
    public function getCategorieslabelByRule($ruleId = null)
18
    {
19 3
        $result = [];
20
21 3
        if ($ruleId == null) {
22 3
            $rules = ['ikf', 'ekf', 'lakc'];
23 3
            foreach ($rules as $rule) {
24 3
                $result[] = static::whereIn('id', array_keys(config('options.' . $rule . '_settings')))
25 3
                    ->select('name')->get()
26 3
                    ->map->name->toArray();
27
            }
28
        }
29 3
        return $result;
30
    }
31
32
    public function scopeIsTeam($query)
33
    {
34
        return $query->where('isTeam', 1);
35
    }
36
37 2
    public function buildName()
38
    {
39
40 2
        if (Auth::check()) {
41 2
            App::setLocale(Auth::user()->locale);
0 ignored issues
show
Bug introduced by
Accessing locale on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
42
        }
43
        $genders = [
44 2
            'M' => trans('categories.male'),
45 2
            'F' => trans('categories.female'),
46 2
            'X' => trans('categories.mixt')
47
        ];
48
49 2
        $teamText = $this->isTeam() ? trans_choice('core.team', 1) : trans('categories.single');
50 2
        $ageCategoryText = $this->getAgeString();
51 2
        $gradeText = $this->getGradeString();
52
53 2
        return $teamText . ' ' . $genders[$this->gender] . ' ' . $ageCategoryText . ' ' . $gradeText;
54
    }
55
56
    /**
57
     * Build Age String
58
     * @return string
59
     */
60 2
    public function getAgeString()
61
    {
62
        $ageCategories = [
63 2
            0 => trans('categories.no_age_restriction'),
64 2
            1 => trans('categories.children'),
65 2
            2 => trans('categories.students'),
66 2
            3 => trans('categories.adults'),
67 2
            4 => trans('categories.masters'),
68 2
            5 => trans('categories.custom')
69
        ];
70
71 2
        if ($this->ageCategory == self::AGE_CUSTOM) {
72 2
            $ageCategoryText = ' - ' . trans('categories.age') . ' : ';
73 2
            if ($this->ageMin != 0 && $this->ageMax != 0) {
74 2
                return $this->hasAgeMinAndMax($ageCategoryText);
75
            }
76 View Code Duplication
            if ($this->ageMin == 0 && $this->ageMax != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
                return $ageCategoryText .= ' < ' . $this->ageMax . ' ' . trans('categories.years');
78
            }
79 View Code Duplication
            if ($this->ageMin != 0 && $this->ageMax == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
                return $ageCategoryText .= ' > ' . $this->ageMin . ' ' . trans('categories.years');
81
            }
82
            return '';
83
        }
84
        return $ageCategories[$this->ageCategory];
85
    }
86
87
    /**
88
     * @param $ageCategoryText
89
     * @return string
90
     */
91 2
    protected function hasAgeMinAndMax($ageCategoryText): string
92
    {
93 2
        if ($this->ageMin == $this->ageMax) {
94
            $ageCategoryText .= $this->ageMax . ' ' . trans('categories.years');
95
        } else {
96 2
            $ageCategoryText .= $this->ageMin . ' - ' . $this->ageMax . ' ' . trans('categories.years');
97
        }
98 2
        return $ageCategoryText;
99
    }
100
101
    /**
102
     * Build Grade String
103
     * @return string
104
     */
105 2
    public function getGradeString()
106
    {
107
108 2
        $grades = Grade::getAll();
109 2
        if ($this->gradeCategory == 3) {  // Custom
110
            $gradeText = ' - ' . trans('core.grade') . ' : ';
111
            if ($this->gradeMin != 0 && $this->gradeMax != 0) {
112
                return $this->hasGradeMinAndMax($grades, $gradeText);
113
            }
114 View Code Duplication
            if ($this->gradeMin == 0 && $this->gradeMax != 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
115
                return $gradeText . ' < ' . $grades[$this->gradeMax - 1]->name;
116
            }
117 View Code Duplication
            if ($this->gradeMin != 0 && $this->gradeMax == 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
118
                return $gradeText . ' > ' . $grades[$this->gradeMin - 1]->name;
119
            }
120
        }
121 2
        return '';
122
    }
123
124
    /**
125
     * @param $grades
126
     * @param $gradeText
127
     * @return string
128
     */
129
    protected function hasGradeMinAndMax($grades, $gradeText): string
130
    {
131
        if ($this->gradeMin == $this->gradeMax) {
132
            $gradeText .= $grades[$this->gradeMin - 1]->name;
133
        } else {
134
            $gradeText .= $grades[$this->gradeMin - 1]->name . ' - ' . $grades[$this->gradeMax - 1]->name;
135
        }
136
        return $gradeText;
137
    }
138
}
139