Completed
Branch develop (a31570)
by Mohamed
08:09 queued 04:45
created

QueryTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 10 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 81.82%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 2
cbo 0
dl 7
loc 70
ccs 18
cts 22
cp 0.8182
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A activeUsers() 0 6 1
A projectsWidthActivities() 0 10 1
A projectsWidthIssues() 7 16 2
A loadPermissions() 0 8 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;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Model\Project;
17
18
/**
19
 * QueryTrait is trait class containing the database queries methods for the User model
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property int                 $id
24
 * @property Eloquent\Collection $permission
25
 *
26
 * @method   Relations\HasMany projects($status = Project::STATUS_OPEN)
27
 * @method   Relations\HasMany permissions()
28
 */
29
trait QueryTrait
30
{
31
    /**
32
     * Returns public users
33
     *
34
     * @return Eloquent\Collection
35
     */
36
    public function activeUsers()
37
    {
38
        return $this->with('role')
0 ignored issues
show
Bug introduced by
It seems like with() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
            ->where('private', '=', false)
40
            ->orderBy('firstname', 'ASC')->get();
41
    }
42
43
    /**
44
     * Returns user projects with activities details eager loaded
45
     *
46
     * @param int $status
47
     *
48
     * @return Relations\HasMany
49
     */
50 10
    public function projectsWidthActivities($status = Project::STATUS_OPEN)
51
    {
52 10
        return $this->projects($status)
53 10
            ->with([
54
                'activities' => function (Relations\Relation $query) {
55 2
                    $query->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note');
56 2
                    $query->orderBy('created_at', 'DESC');
57 10
                },
58
            ]);
59
    }
60
61
    /**
62
     * Returns projects with issues details eager loaded
63
     *
64
     * @param int $status
65
     *
66
     * @return Relations\HasMany
67
     */
68 1
    public function projectsWidthIssues($status = Project::STATUS_OPEN)
69
    {
70
        return $this
71 1
            ->projects($status)
72 1
            ->with([
73 View Code Duplication
                'issues' => function (Relations\Relation $query) use ($status) {
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...
74 1
                    $query->with('updatedBy');
75 1
                    $query->where('assigned_to', '=', $this->id);
76 1
                    if ($status === Project::STATUS_OPEN) {
77 1
                        $query->where('status', '=', Project\Issue::STATUS_OPEN);
78
                    }
79 1
                },
80
                'issues.user'          => function () {},
81
                'issues.countComments' => function () {},
82
            ]);
83
    }
84
85
    /**
86
     * Load user permissions
87
     *
88
     * @return Eloquent\Collection
89
     */
90 50
    protected function loadPermissions()
91
    {
92 50
        if (null === $this->permission) {
93 50
            $this->permission = $this->permissions()->with('permission')->get();
94
        }
95
96 50
        return $this->permission;
97
    }
98
}
99