HasProjects::isOnProject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Chriscreates\Projects\Traits;
4
5
use Chriscreates\Projects\Models\Project;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
8
trait HasProjects
9
{
10
    use HasProjectRoles;
11
12
    public function projects() : BelongsToMany
13
    {
14
        return $this->belongsToMany(get_class(config('projects.user_class')), 'project_users', 'user_id', 'project_id');
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() 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...
15
    }
16
17
    /**
18
     * Get count of the model's Project relations
19
     *
20
     * @return bool
21
     */
22
    public function hasProjects() : bool
23
    {
24
        return $this->projects->count() > 0;
0 ignored issues
show
Bug introduced by
The property projects does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
    }
26
27
    /**
28
     * Determine if the given Project is owned by the model.
29
     *
30
     * @param  \Chriscreates\Projects\Models\Project  $project
31
     * @return bool
32
     */
33
    public function ownsProject(Project $project) : bool
34
    {
35
        return $this->roleOnProject(config('projects.owner_role'), $project);
36
    }
37
38
    /**
39
     * Determine if the given model is on a Project.
40
     *
41
     * @param  \Chriscreates\Projects\Models\Project  $project
42
     * @return bool
43
     */
44
    public function isOnProject(Project $project) : bool
45
    {
46
        if ($this->projects->isEmpty()) {
47
            return false;
48
        }
49
50
        return $this->projects->contains($project->getKeyName(), $project->id);
51
    }
52
}
53