Completed
Branch develop-3.0 (4fe777)
by Mohamed
11:06
created

TagRelations   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A parent() 0 4 1
A tags() 0 4 1
A userAccessibleTags() 0 4 1
A scopeAccessibleToUser() 0 7 1
A scopeAccessibleToLoggedUser() 0 4 1
A issues() 0 4 1
A projects() 0 4 1
belongsTo() 0 1 ?
hasMany() 0 1 ?
belongsToMany() 0 1 ?
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;
13
14
use Illuminate\Database\Eloquent\Builder;
15
use Illuminate\Database\Eloquent\Relations;
16
use Tinyissue\Contracts\Model\UserInterface;
17
18
/**
19
 * RelationTrait is trait class containing the relationship methods for the Tag model.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 *
23
 * @property static $this
24
 */
25
trait TagRelations
26
{
27
    /**
28
     * Returns the parent/group for the tag.
29
     *
30
     * @return Relations\BelongsTo
31
     */
32
    public function parent()
33
    {
34
        return $this->belongsTo('Tinyissue\Model\Tag', 'parent_id');
35
    }
36
37
    /**
38
     * Parent tag/group have many tags.
39
     *
40
     * @return Relations\HasMany
41
     */
42
    public function tags()
43
    {
44
        return $this->hasMany('Tinyissue\Model\Tag', 'parent_id');
45
    }
46
47
    public function userAccessibleTags()
48
    {
49
        return $this->tags()->accessibleToLoggedUser();
50
    }
51
52
    public function scopeAccessibleToUser(Builder $query, UserInterface $user)
53
    {
54
        return $query->where(function (Builder $query) use ($user) {
55
            $query->where('role_limit', '<=', $user->role_id);
0 ignored issues
show
Bug introduced by
Accessing role_id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
56
            $query->orWhere('role_limit', '=', null);
57
        });
58
    }
59
60
    public function scopeAccessibleToLoggedUser(Builder $query)
61
    {
62
        return $this->scopeAccessibleToUser($query, $this->getLoggedUser());
0 ignored issues
show
Bug introduced by
It seems like getLoggedUser() 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...
63
    }
64
65
    /**
66
     * Returns issues for the Tag. Tag can belong to many issues & issue can have many tags.
67
     *
68
     * @return Relations\BelongsToMany
69
     */
70
    public function issues()
71
    {
72
        return $this->belongsToMany('Tinyissue\Model\Project\Issue', 'projects_issues_tags', 'issue_id', 'tag_id');
73
    }
74
75
    /**
76
     * Returns projects for the Tag. Tag can belong to many projects & project can have many tags.
77
     *
78
     * @return Relations\BelongsToMany
79
     */
80
    public function projects()
81
    {
82
        return $this->belongsToMany('Tinyissue\Model\Project', 'projects_kanban_tags', 'project_id', 'tag_id');
83
    }
84
85
    abstract public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null);
86
87
    abstract public function hasMany($related, $foreignKey = null, $localKey = null);
88
89
    abstract public function belongsToMany($related, $table = null, $foreignKey = null, $otherKey = null, $relation = null);
90
}
91