Leaderboard   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 190
Duplicated Lines 27.89 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 53
loc 190
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A attributeNames() 0 4 1
A getUid() 0 4 1
A getInClub() 0 4 1
A getItems() 0 4 1
A getBoardType() 0 4 1
A getRange() 0 4 1
A getTitle() 0 16 1
A getPlayerRankDescription() 18 18 3
A getClubRankDescription() 18 18 3
A setBoardType() 0 4 1
B setRange() 7 32 4
A setUid() 0 4 1
A setInClub() 0 4 1
B fetchList() 10 29 4
A getRankDescription() 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
 * @property integer $uid
4
 * @property integer $inClub
5
 * @property array $items
6
 * @property string $boardType
7
 * @property string $range
8
 * @property string $title
9
 * @property string $playerRankDescription
10
 * @property string $clubRankDescription
11
 */
12
class Leaderboard extends CModel
13
{
14
    const BOARD_RANGE = 5;
15
    const TYPE_PLAYER = 'board_p';
16
    const TYPE_CLUB = 'board_c';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
17
18
    const RANGE_ACTUAL = 'actual';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
19
    const RANGE_PREVIOUS = 'prev';
20
    const RANGE_LAST_SIX = 'last';
21
22
    private $uid;
23
    private $inClub;
24
    private $items = [];
25
    private $key = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
26
    private $boardType;
27
    private $range;
28
29
    public function attributeNames()
30
    {
31
        return [];
32
    }
33
34
    public function getUid()
35
    {
36
        return $this->uid;
37
    }
38
39
    public function getInClub()
40
    {
41
        return $this->inClub;
42
    }
43
44
    public function getItems()
45
    {
46
        return $this->items;
47
    }
48
49
    public function getBoardType()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51
        return $this->boardType;
52
    }
53
54
    public function getRange()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55
    {
56
        return $this->range;
57
    }
58
59
    public function getTitle()
60
    {
61
        $titles = [
62
            self::TYPE_PLAYER => [
63
            self::RANGE_ACTUAL => 'Aktuális hónap legjobb játékosai',
64
            self::RANGE_PREVIOUS => 'Előző hónap legjobb játékosai',
65
            self::RANGE_LAST_SIX => 'Utolsó 6 hónap legjobb játékosai',
66
            ],
67
            self::TYPE_CLUB => [
68
            self::RANGE_ACTUAL => 'Aktuális hónap legjobb klubjai',
69
            self::RANGE_PREVIOUS => 'Előző hónap legjobb klubjai',
70
            self::RANGE_LAST_SIX => 'Utolsó 6 hónap legjobb klubjai',
71
            ]
72
            ];
73
        return $titles[$this->boardType][$this->range];
74
    }
75
76 View Code Duplication
    public function getPlayerRankDescription()
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...
77
    {
78
        $redis = Yii::app()->redis->getClient();
79
80
        $total = $redis->zCard($this->key);
81
        $rank  = $redis->zRevRank($this->key, $this->uid) + 1;
82
83
        if ($rank == 1) {
84
            return 'Te vagy a király!';
85
        } else {
86
            $percent = round((1 - ($rank / $total)) * 100, 1);
87
            if (!$percent) {
88
                return 'Több párbajgyőzelemre van szükséged.';
89
            }
90
91
            return 'Jobb vagy, mint a játékosok ' . $percent . '%-a!';
92
        }
93
    }
94
95 View Code Duplication
    public function getClubRankDescription()
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...
96
    {
97
        $redis = Yii::app()->redis->getClient();
98
99
        $total = $redis->zCard($this->key);
100
        $rank  = $redis->zRevRank($this->key, $this->inClub) + 1;
101
102
        if ($rank == 1) {
103
            return 'A te klubod a király!';
104
        } else {
105
            $percent = round((1 - ($rank / $total)) * 100, 1);
106
            if (!$percent) {
107
                return 'Több versenygyőzelemre van szükségetek.';
108
            }
109
110
            return 'Jobb a klubod, mint a többi klub ' . $percent . '%-a!';
111
        }
112
    }
113
114
    public function setBoardType ($type)
115
    {
116
        $this->boardType = $type;
117
    }
118
119
    public function setRange ($range)
120
    {
121
        $d = new DateTime();
122
123
        //create key from date/intersect
124
        switch ($range) {
125
            case self::RANGE_LAST_SIX:
126
                $this->key = $this->boardType . ':6month';
127
                //create intersect
128
129
                $history = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
130
                $history[] = $this->boardType . ':' . $d->format('Ym');
131
132
                for ($i=0; $i<6; $i++) {
133
                    $d->modify('first day of previous month');
134
                    $history[] = $this->boardType . ':' . $d->format('Ym');
135
                }
136
137
                $redis = Yii::app()->redis->getClient();
138
                $redis->zUnionStore($this->key, $history);
139
140
                break;
141 View Code Duplication
            case self::RANGE_PREVIOUS:
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...
142
                $d->modify('first day of previous month');
143
                $this->key = $this->boardType . ':' . $d->format('Ym');
144
                break;
145 View Code Duplication
            default:
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...
146
                $range = self::RANGE_ACTUAL;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
147
                $this->key = $this->boardType . ':' . $d->format('Ym');
148
        }
149
        $this->range = $range;
150
    }
151
152
    public function setUid($uid)
153
    {
154
        $this->uid = (int)$uid;
155
    }
156
157
    public function setInClub($id)
158
    {
159
        $this->inClub = (int)$id;
160
    }
161
162
    public function fetchList()
163
    {
164
        if ($this->boardType == self::TYPE_PLAYER) {
165
            $class = 'Player';
166
        } else {
167
            $class = 'Club';
168
        }
169
170
        $redis = Yii::app()->redis->getClient();
171
172
        $myRank = $redis->zRevRank($this->key, $this->inClub);
173
174
        $range = self::BOARD_RANGE;
175
        $min = $myRank - $range > 0 ? $myRank - $range : 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
176
        $max = $myRank + $range + ($range-$myRank+$min);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
177
178
        $item = new $class;
179
        $i = $min+1;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
180 View Code Duplication
        foreach ($redis->zRevRange($this->key, $min, $max, true) as $id => $score) {
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...
181
            $item->subjectId = $id;
182
183
            $this->items[$i] = [
184
                'id'=>$id,
185
                'name'=>$item->getSubjectName(),
186
                'score'=>$score,
187
                ];
188
            $i++;
189
        }
190
    }
191
192
    public function getRankDescription()
193
    {
194
        if ($this->boardType == self::TYPE_CLUB) {
195
            $ret = $this->getClubRankDescription();
196
        } else {
197
            $ret = $this->getPlayerRankDescription();
198
        }
199
        return $ret;
200
    }
201
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
202