Game   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 410
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 175
dl 0
loc 410
c 0
b 0
f 0
ccs 184
cts 184
cp 1
rs 7.92
wmc 51

17 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 11 1
A getASort() 0 10 2
A getAnswerForm() 0 19 4
A getASortType() 0 10 2
A getCommentForm() 0 20 4
B vote() 0 51 9
A activeUser() 0 9 3
A getDatabases() 0 22 1
A validUser() 0 6 2
B updateRank() 0 26 6
A getFormSettings() 0 7 1
A getUserForm() 0 14 5
A getNav() 0 12 2
A canUserVote() 0 3 1
A getQuestionForm() 0 16 4
A getQSort() 0 10 2
A getQSortType() 0 10 2

How to fix   Complexity   

Complex Class

Complex classes like Game often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Game, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Pamo\Game;
4
5
use Pamo\Question\HTMLForm\CreateQuestionForm;
6
use Pamo\Question\HTMLForm\DeleteQuestionForm;
7
use Pamo\Question\HTMLForm\UpdateQuestionForm;
8
use Pamo\Answer\HTMLForm\CreateAnswerForm;
9
use Pamo\Answer\HTMLForm\DeleteAnswerForm;
10
use Pamo\Answer\HTMLForm\UpdateAnswerForm;
11
use Pamo\Comment\HTMLForm\CreateCommentForm;
12
use Pamo\Comment\HTMLForm\DeleteCommentForm;
13
use Pamo\Comment\HTMLForm\UpdateCommentForm;
14
use Pamo\User\HTMLForm\CreateUserForm;
15
use Pamo\User\HTMLForm\UpdateUserForm;
16
use Pamo\User\HTMLForm\DeleteUserForm;
17
18
/**
19
 * Game
20
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
21
 */
22
class Game
23
{
24
    /**
25
     * @var object $di.
26
     */
27
    private $di;
28
29
30
31
    /**
32
     * Game class
33
     *
34
     */
35 36
    public function init($di)
36
    {
37 36
        $this->di = $di;
38 36
        $this->game = [];
0 ignored issues
show
Bug Best Practice introduced by
The property game does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39 36
        $this->session = $this->di->get("session");
0 ignored issues
show
Bug Best Practice introduced by
The property session does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40 36
        $this->request = $this->di->get("request");
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41 36
        $this->gravatar = $this->di->get("gravatar");
0 ignored issues
show
Bug Best Practice introduced by
The property gravatar does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42 36
        $this->sortQBy = $this->session->get("sortQBy", "created");
0 ignored issues
show
Bug Best Practice introduced by
The property sortQBy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43 36
        $this->sortQType = $this->session->get("sortQType", "DESC");
0 ignored issues
show
Bug Best Practice introduced by
The property sortQType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44 36
        $this->sortABy = $this->session->get("sortABy", "created");
0 ignored issues
show
Bug Best Practice introduced by
The property sortABy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45 36
        $this->sortAType = $this->session->get("sortAType", "ASC");
0 ignored issues
show
Bug Best Practice introduced by
The property sortAType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46 36
    }
47
48
49
50
    /**
51
     * Get and setup all databases.
52
     *
53
     */
54 36
    public function getDatabases()
55
    {
56 36
        $this->question = $this->di->get("question");
0 ignored issues
show
Bug Best Practice introduced by
The property question does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
57 36
        $this->question->setDb($this->di->get("dbqb"));
58
59 36
        $this->answer = $this->di->get("answer");
0 ignored issues
show
Bug Best Practice introduced by
The property answer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60 36
        $this->answer->setDb($this->di->get("dbqb"));
61
62 36
        $this->user = $this->di->get("user");
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
63 36
        $this->user->setDb($this->di->get("dbqb"));
64
65 36
        $this->commentQuestion = $this->di->get("comment-question");
0 ignored issues
show
Bug Best Practice introduced by
The property commentQuestion does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
66 36
        $this->commentQuestion->setDb($this->di->get("dbqb"));
67
68 36
        $this->commentAnswer = $this->di->get("comment-answer");
0 ignored issues
show
Bug Best Practice introduced by
The property commentAnswer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
69 36
        $this->commentAnswer->setDb($this->di->get("dbqb"));
70
71 36
        $this->tag = $this->di->get("tag");
0 ignored issues
show
Bug Best Practice introduced by
The property tag does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72 36
        $this->tag->setDb($this->di->get("dbqb"));
73
74 36
        $this->tagQuestion = $this->di->get("tag-question");
0 ignored issues
show
Bug Best Practice introduced by
The property tagQuestion does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75 36
        $this->tagQuestion->setDb($this->di->get("dbqb"));
76 36
    }
77
78
79
80
    /**
81
     * Vote
82
     *
83
     * @return object
84
     */
85 1
    public function vote($type, $id, $vote)
86
    {
87 1
        $currentUser = $this->session->get("user", null);
88
89 1
        switch ($type) {
90 1
            case "question":
91 1
                $table = $this->question;
92 1
                break;
93 1
            case "answer":
94 1
                $table = $this->answer;
95 1
                break;
96 1
            case "comment-question":
97 1
                $table = $this->commentQuestion;
98 1
                break;
99 1
            case "comment-answer":
100 1
                $table = $this->commentAnswer;
101 1
                break;
102
        }
103
104 1
        $table->find("id", $id);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table does not seem to be defined for all execution paths leading up to this point.
Loading history...
105
106 1
        $eligible = $this->canUserVote($table->user);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $eligible is correct as $this->canUserVote($table->user) targeting Pamo\Game\Game::canUserVote() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107
108 1
        if (!$eligible) {
0 ignored issues
show
introduced by
$eligible is of type null, thus it always evaluated to false.
Loading history...
109 1
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type object.
Loading history...
110
        }
111
112 1
        if ($vote === "up") {
113 1
            $table->vote += 1;
114 1
            $this->updateRank($table->user, $table->vote);
115 1
        } else if ($vote === "down") {
116 1
            $table->vote -= 1;
117
        } else {
118 1
            $table->vote = $table->vote;
119
        }
120
121 1
        $table->save();
122
123 1
        if ($type === "question") {
124 1
            $questionId = $table->id;
125
        } else {
126 1
            $questionId = $table->questionid;
127
        }
128
129 1
        $user = $this->user;
130 1
        $user->find("username", $currentUser["username"]);
131 1
        $user->voted += 1;
132 1
        $user->save();
133 1
        $this->updateRank($currentUser["username"], 1);
134
135 1
        return $questionId;
136
    }
137
138
    /**
139
     * Rank
140
     *
141
     * @return null
142
     */
143 5
    public function updateRank($username, $rank)
144
    {
145
        switch (true) {
146 5
            case $rank <= 1:
147 1
                $value = 1;
148 1
                break;
149 5
            case $rank <= 5:
150 5
                $value = 5;
151 5
                break;
152 1
            case $rank <= 10:
153 1
                $value = 10;
154 1
                break;
155 1
            case $rank <= 20:
156 1
                $value = 20;
157 1
                break;
158 1
            case $rank <= 30:
159 1
                $value = 30;
160 1
                break;
161
            default:
162 1
                $value = 50;
163
        }
164
165 5
        $user = $this->user;
166 5
        $user->find("username", $username);
167 5
        $user->rank += $value;
168 5
        $user->save();
169 5
    }
170
171
172
173
    /**
174
     * Return question form
175
     *
176
     * @return object
177
     */
178 3
    public function getUserForm($adminType, $username = null)
179
    {
180 3
        switch ($adminType) {
181 3
            case "create":
182 1
                $userForm = new CreateUserForm($this->di);
183 1
                break;
184 2
            case "edit":
185 1
                $userForm = new UpdateUserForm($this->di, $username);
186 1
                break;
187 1
            case "delete":
188 1
                $userForm = new DeleteUserForm($this->di, $username);
189 1
                break;
190
        }
191 3
        return isset($userForm) ? $userForm : null;
192
    }
193
194
195
196
    /**
197
     * Return question form
198
     *
199
     * @return object
200
     */
201 4
    public function getQuestionForm($adminType, $questionId = null)
202
    {
203 4
        switch ($adminType) {
204 4
            case "create":
205 1
                $questionForm = new CreateQuestionForm($this->di);
206 1
                break;
207 3
            case "edit":
208 2
                $questionForm = new UpdateQuestionForm($this->di, $questionId);
209 2
                break;
210 2
            case "delete":
211 1
                $questionForm = new DeleteQuestionForm($this->di, $questionId);
212 1
                break;
213
            default:
214 1
                $questionForm = null;
215
        }
216 4
        return $questionForm;
217
    }
218
219
220
221
    /**
222
     * Return answer form
223
     *
224
     * @return object
225
     */
226 7
    public function getAnswerForm($adminType, $questionId)
227
    {
228 7
        $request = $this->di->get("request");
229 7
        $adminId = $request->getGet("adminId", null);
230
231 7
        switch ($adminType) {
232 7
            case "create":
233 7
                $answerForm = new CreateAnswerForm($this->di, $questionId, $this->activeUser("username"));
0 ignored issues
show
Bug introduced by
$this->activeUser('username') of type array is incompatible with the type string expected by parameter $user of Pamo\Answer\HTMLForm\Cre...swerForm::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

233
                $answerForm = new CreateAnswerForm($this->di, $questionId, /** @scrutinizer ignore-type */ $this->activeUser("username"));
Loading history...
234 7
                break;
235 2
            case "edit":
236 1
                $answerForm = new UpdateAnswerForm($this->di, $adminId);
237 1
                break;
238 2
            case "delete":
239 1
                $answerForm = new DeleteAnswerForm($this->di, $adminId);
240 1
                break;
241
            default:
242 1
                $answerForm = null;
243
        }
244 7
        return $answerForm;
245
    }
246
247
248
249
    /**
250
     * Return comment form
251
     *
252
     * @return object
253
     */
254 3
    public function getCommentForm($adminType, $questionId, $table)
255
    {
256 3
        $request = $this->di->get("request");
257 3
        $adminId = $request->getGet("adminId", null);
258 3
        $answerId = $request->getGet("answerId", null);
259
260 3
        switch ($adminType) {
261 3
            case "create":
262 2
                $commentForm = new CreateCommentForm($this->di, $table, $questionId, $answerId);
263 2
                break;
264 3
            case "edit":
265 2
                $commentForm = new UpdateCommentForm($this->di, $table, $adminId);
266 2
                break;
267 3
            case "delete":
268 2
                $commentForm = new DeleteCommentForm($this->di, $table, $adminId);
269 2
                break;
270
            default:
271 1
                $commentForm = null;
272
        }
273 3
        return $commentForm;
274
    }
275
276
277
278
    /**
279
     * Return nav
280
     *
281
     * @return array
282
     */
283 34
    public function getNav()
284
    {
285 34
        $navData = [];
286
287 34
        if ($this->activeUser()) {
288
            $navData = [
289 27
                "activeUser" => $this->activeUser(),
290 27
                "gravatar" => $this->gravatar->get($this->activeUser("email"))
291
            ];
292
        }
293
294 34
        return $navData;
295
    }
296
297
298
299
    /**
300
     * Return form settings
301
     *
302
     * @return array
303
     */
304 7
    public function getFormSettings()
305
    {
306
        $formSettings = [
307 7
            "use_fieldset" => false
308
        ];
309
310 7
        return $formSettings;
311
    }
312
313
314
315
    /**
316
     * Return current user
317
     *
318
     * @return array
319
     */
320 34
    public function activeUser($item = null)
321
    {
322 34
        $user = $this->session->get("user", null);
323
324 34
        if ($item && $user) {
325 30
            return $user[$item];
326
        }
327
328 34
        return $user;
329
    }
330
331
332
333
    /**
334
     * Is the logged in user the content owner.
335
     *
336
     * @return bool
337
     */
338 3
    public function validUser($username)
339
    {
340 3
        if ($this->activeUser("username") != $username) {
341 2
            return false;
342
        }
343 1
        return true;
344
    }
345
346
347
348
    /**
349
     * Is the logged in user the content owner.
350
     *
351
     * @return null
352
     */
353 1
    public function canUserVote($username)
354
    {
355 1
        return $this->activeUser("username") != $username;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->activeUser('username') != $username returns the type boolean which is incompatible with the documented return type null.
Loading history...
356
    }
357
358
359
360
    /**
361
     * Return question sort
362
     *
363
     * @return string
364
     */
365 7
    public function getQSort()
366
    {
367 7
        $sort = $this->request->getGet("sortQBy", null);
368
369 7
        if ($sort) {
370 7
            $this->session->set("sortQBy", $sort);
371 7
            $this->sortQBy = $sort;
0 ignored issues
show
Bug Best Practice introduced by
The property sortQBy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
372
        }
373
374 7
        return $this->sortQBy;
375
    }
376
377
378
379
    /**
380
     * Return question sort type
381
     *
382
     * @return string
383
     */
384 7
    public function getQSortType()
385
    {
386 7
        $sortType = $this->request->getGet("sortQType", null);
387
388 7
        if ($sortType) {
389 7
            $this->session->set("sortQType", $sortType);
390 7
            $this->sortQType = $sortType;
0 ignored issues
show
Bug Best Practice introduced by
The property sortQType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
391
        }
392
393 7
        return $this->sortQType;
394
    }
395
396
397
398
    /**
399
     * Return answer sort
400
     *
401
     * @return string
402
     */
403 10
    public function getASort()
404
    {
405 10
        $sort = $this->request->getGet("sortABy", null);
406
407 10
        if ($sort) {
408 10
            $this->session->set("sortABy", $sort);
409 10
            $this->sortABy = $sort;
0 ignored issues
show
Bug Best Practice introduced by
The property sortABy does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
410
        }
411
412 10
        return $this->sortABy;
413
    }
414
415
416
417
    /**
418
     * Return answer sort type
419
     *
420
     * @return string
421
     */
422 10
    public function getASortType()
423
    {
424 10
        $sortType = $this->request->getGet("sortAType", null);
425
426 10
        if ($sortType) {
427 10
            $this->session->set("sortAType", $sortType);
428 10
            $this->sortAType = $sortType;
0 ignored issues
show
Bug Best Practice introduced by
The property sortAType does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
429
        }
430
431 10
        return $this->sortAType;
432
    }
433
}
434