Test Failed
Push — master ( 04b9d0...addd19 )
by Zhengchao
07:38
created

CanFollow   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 70.59%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 86
ccs 12
cts 17
cp 0.7059
rs 10
c 5
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toggleFollow() 0 6 1
A following() 0 4 1
A follow() 0 6 1
A unfollow() 0 6 1
A isFollowing() 0 4 1
A parseFollowable() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of emanci/laravel-rally package.
5
 *
6
 * (c) emanci <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Emanci\Rally\Traits;
13
14
use Illuminate\Database\Eloquent\Model;
15
16
trait CanFollow
17
{
18
    /**
19
     * Follow a followable entity.
20
     *
21
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
22
     * @param string                                        $className
23
     *
24
     * @return array
25
     */
26 7
    public function follow($followable, $className = __CLASS__)
27
    {
28 7
        $followable = $this->parseFollowable($followable);
29
30 7
        return $this->following($className)->sync($followable, false);
0 ignored issues
show
Bug introduced by
It seems like $followable defined by $this->parseFollowable($followable) on line 28 can also be of type integer; however, Illuminate\Database\Eloq...sWithPivotTable::sync() does only seem to accept object<Illuminate\Support\Collection>|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
    }
32
33
    /**
34
     * Unfollow a followable entity.
35
     *
36
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
37
     * @param string                                        $className
38
     *
39
     * @return bool|null
40
     */
41
    public function unfollow($followable, $className = __CLASS__)
42
    {
43
        $followable = $this->parseFollowable($followable);
44
45
        return $this->following($className)->detach($followable);
46
    }
47
48
    /**
49
     * Check if entity is following given entity.
50
     *
51
     * @param int|\Illuminate\Database\Eloquent\Model $followable
52
     * @param string                                  $className
53
     *
54
     * @return bool
55
     */
56
    public function isFollowing($followable, $className = __CLASS__)
57
    {
58
        return $this->following($className)->get()->contains($followable);
59
    }
60
61
    /**
62
     * Toggle follow a followable or followables.
63
     *
64
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
65
     * @param string                                        $className
66
     *
67
     * @return array
68
     */
69 1
    public function toggleFollow($followable, $className = __CLASS__)
70
    {
71 1
        $followable = $this->parseFollowable($followable);
72
73 1
        return $this->following($className)->toggle($followable);
74
    }
75
76
    /**
77
     * Return entity following.
78
     *
79
     * @param string $className
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
82
     */
83 8
    public function following($className = __CLASS__)
84
    {
85 8
        return $this->morphedByMany($className, config('rally.followable_prefix'), config('rally.followers_table'), config('rally.follower_prefix').'_id', config('rally.followable_prefix').'_id')->withTimestamps();
0 ignored issues
show
Bug introduced by
It seems like morphedByMany() 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...
86
    }
87
88
    /**
89
     * @param int|array|\Illuminate\Database\Eloquent\Model $followable
90
     *
91
     * @return int|array
92
     */
93 8
    protected function parseFollowable($followable)
94
    {
95 8
        if ($followable instanceof Model) {
96 1
            return $followable->getKey();
97
        }
98
99 7
        return $followable;
100
    }
101
}
102