Completed
Push — develop ( 688bc1...4964b4 )
by Mohamed
07:24
created

CountTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 32.35 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 7
c 4
b 1
f 0
lcom 0
cbo 2
dl 22
loc 68
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A countUsers() 0 4 1
A assignedIssuesCount() 11 11 2
A createdIssuesCount() 11 11 2
A projectsWithCountOpenIssues() 0 10 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
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\User;
13
14
use Illuminate\Database\Eloquent\Relations;
15
use Tinyissue\Model\Project;
16
use Tinyissue\Model\User;
17
18
/**
19
 * CountTrait is trait class containing the methods for counting database records for the User model.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @method bool              permission($key)
24
 * @method Relations\HasMany projects($status = Project::STATUS_OPEN)
25
 * @method Relations\HasMany issues()
26
 */
27
trait CountTrait
28
{
29
    /**
30
     * @param int $deleted
31
     *
32
     * @return int
33
     */
34 2
    public static function countUsers($deleted = User::NOT_DELETED_USERS)
35
    {
36 2
        return User::where('deleted', '=', $deleted)->count();
37
    }
38
39
    /**
40
     * Count number of assigned issues in a project.
41
     *
42
     * @param int $projectId
43
     *
44
     * @return int
45
     */
46 19 View Code Duplication
    public function assignedIssuesCount($projectId = 0)
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...
47
    {
48 19
        $issues = $this->issues();
49
50 19
        if (0 < $projectId) {
51 19
            $issues = $issues->where('project_id', '=', $projectId);
52
        }
53 19
        $issues->where('status', '=', Project\Issue::STATUS_OPEN);
54
55 19
        return $issues->count();
56
    }
57
58
    /**
59
     * Count number of created issues in a project.
60
     *
61
     * @param int $projectId
62
     *
63
     * @return int
64
     */
65 2 View Code Duplication
    public function createdIssuesCount($projectId = 0)
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...
66
    {
67 2
        $issues = $this->issuesCreatedBy();
0 ignored issues
show
Bug introduced by
The method issuesCreatedBy() does not exist on Tinyissue\Model\Traits\User\CountTrait. Did you maybe mean issues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
69 2
        if (0 < $projectId) {
70 2
            $issues = $issues->where('project_id', '=', $projectId);
71
        }
72 2
        $issues->where('status', '=', Project\Issue::STATUS_OPEN);
73
74 2
        return $issues->count();
75
    }
76
77
    /**
78
     * Returns all projects with open issue count.
79
     *
80
     * @param int $status
81
     *
82
     * @return $this
83
     */
84 3
    public function projectsWithCountOpenIssues($status = Project::STATUS_OPEN)
85
    {
86 3
        if ($this->permission('project-all')) {
87 3
            $project = new Project();
88
89 3
            return $project->projectsWithOpenIssuesCount($status, Project::PRIVATE_ALL);
90
        }
91
92
        return $this->projects($status)->with('openIssuesCount');
93
    }
94
}
95