UserUtility   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 100
dl 0
loc 135
rs 10
c 1
b 0
f 0
wmc 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B getLevel() 0 62 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Utility;
6
7
class UserUtility
8
{
9
    /**
10
     * The array containing the XP needed for each level.
11
     *
12
     * PostWasSolvedEvent::class => 120,
13
     * ConversationWasCreatedEvent::class => 90,
14
     * PostWasCreatedEvent::class => 75,
15
     * CommentWasCreatedEvent::class => 75
16
     *
17
     * @var array
18
     */
19
    protected static array $levels = [
20
        0, //0
21
        400, //1
22
        800, //2
23
        1200, //3
24
        1600, //4
25
        2000, //5
26
        2400, //6
27
        2800, //7
28
        3200, //8
29
        3600, //9
30
        4000, //10
31
        4400, //11
32
        4800, //12
33
        5200, //13
34
        5600, //14
35
        6000, //15
36
        6400, //16
37
        6800, //17
38
        7200, //18
39
        7600, //19
40
        8000, //20
41
        8400, //21
42
        8800, //22
43
        9200, //23
44
        9600, //24
45
        10000, //25
46
        10400, //26
47
        10800, //27
48
        11200, //28
49
        11600, //29
50
        12000, //30
51
        12400, //31
52
        12800, //32
53
        13200, //33
54
        13600, //34
55
        14000, //35
56
        14400, //36
57
        14800, //37
58
        15200, //38
59
        15600, //39
60
        16000, //40
61
        16400, //41
62
        16800, //42
63
        17200, //43
64
        17600, //44
65
        18000, //45
66
        18400, //46
67
        18800, //47
68
        19200, //48
69
        19600, //49
70
        20000 //50
71
    ];
72
73
    /**
74
     * Get the level of a user with his experiences.
75
     *
76
     * @param int $userXP The XP of the user to get the level.
77
     *
78
     * @return array
79
     */
80
    public static function getLevel(int $userXP): array
81
    {
82
        $infos = [
83
            'previousLevelExperience' => 0,
84
            'previousLevel' => 0,
85
            'currentLevel' => 0,
86
            'currentLevelExperience' => 0,
87
            'currentUserExperience' => 0,
88
            'nextLevel' => 1,
89
            'experienceNeededNextLevel' => 400,
90
            'nextLevelExperience' => 400,
91
            'matchExactXPLevel' => false,
92
            'maxLevel' => false
93
        ];
94
95
        if ($userXP === 0) {
96
            return $infos;
97
        }
98
99
        for ($i = 0; $i < count(static::$levels); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
100
            // The XP of the user match the exact XP of the rank and there's another rank after this one.
101
            if ($userXP === static::$levels[$i] && isset(static::$levels[$i + 1])) {
102
                return array_merge($infos, [
103
                    'previousLevelExperience' => static::$levels[$i - 1],
104
                    'previousLevel' => $i - 1,
105
                    'currentLevel' => $i,
106
                    'currentLevelExperience' => static::$levels[$i],
107
                    'currentUserExperience' => $userXP,
108
                    'nextLevel' => $i + 1,
109
                    'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP,
110
                    'nextLevelExperience' => static::$levels[$i + 1],
111
                    'matchExactXPLevel' => true
112
                ]);
113
            }
114
            // If there's another rank after this one and the user XP is higher than the current rank.
115
            if (isset(static::$levels[$i + 1]) && $userXP > static::$levels[$i]) {
116
                // If the user XP is higher than the current rank but lower than the next rank.
117
                if ($userXP > static::$levels[$i] && $userXP < static::$levels[$i + 1]) {
118
                    return array_merge($infos, [
119
                        'previousLevelExperience' => static::$levels[$i],
120
                        'previousLevel' => $i === 0 ? 0 : $i - 1,
121
                        'currentLevel' => $i,
122
                        'currentLevelExperience' => static::$levels[$i],
123
                        'currentUserExperience' => $userXP,
124
                        'nextLevel' => $i + 1,
125
                        'experienceNeededNextLevel' => static::$levels[$i + 1] - $userXP,
126
                        'nextLevelExperience' => static::$levels[$i + 1]
127
                    ]);
128
                }
129
            } else {
130
                // The user has reached the max lvl
131
                return array_merge($infos, [
132
                    'previousLevelExperience' => static::$levels[$i],
133
                    'previousLevel' => $i === 0 ? 0 : $i - 1,
134
                    'currentLevel' => $i,
135
                    'currentLevelExperience' => static::$levels[$i],
136
                    'currentUserExperience' => $userXP,
137
                    'nextLevel' => 0,
138
                    'experienceNeededNextLevel' => 0,
139
                    'nextLevelExperience' => 0,
140
                    'matchExactXPLevel' => (static::$levels[$i] - $userXP) === 0,
141
                    'maxLevel' => true
142
                ]);
143
            }
144
        }
145
    }
146
}
147