Completed
Push — master ( 06b4a8...88e670 )
by Manel
05:44
created

RecordsActivity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 63
rs 10
c 0
b 0
f 0
ccs 0
cts 29
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bootRecordsActivity() 0 11 3
A getActivitiesToRecord() 0 4 1
A recordActivity() 0 7 1
A activity() 0 4 1
A getActivityType() 0 6 1
1
<?php
2
3
namespace Scool\EnrollmentMobile\Traits;
4
5
6
/**
7
 * Trait RecordsActivity
8
 * @package Scool\EnrollmentMobile\Traits
9
 */
10
trait RecordsActivity
11
{
12
    /**
13
     * Boot the trait.
14
     */
15
    protected static function bootRecordsActivity()
16
    {
17
        // Comprovar si no hi ha usuari logat.
18
        if (auth()->guest()) return;
0 ignored issues
show
Bug introduced by
The method guest does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
19
20
        foreach (static::getActivitiesToRecord() as $event) {
21
            static::$event(function ($model) use ($event) {
22
                $model->recordActivity($event);
23
            });
24
        }
25
    }
26
27
    /**
28
     * Fetch all model events that require activity recording.
29
     *
30
     * @return array
31
     */
32
    protected static function getActivitiesToRecord()
33
    {
34
        return ['created', 'updated', 'deleted'];
35
    }
36
37
    /**
38
     * Record new activity for the model.
39
     *
40
     * @param string $event
41
     */
42
    protected function recordActivity($event)
43
    {
44
        $this->activity()->create([
45
            'user_id' => auth()->id(),
0 ignored issues
show
Bug introduced by
The method id does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
            'type' => $this->getActivityType($event)
47
        ]);
48
    }
49
50
    /**
51
     * Fetch the activity relationship.
52
     *
53
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
54
     */
55
    public function activity()
56
    {
57
        return $this->morphMany('App\Activity', 'subject');
0 ignored issues
show
Bug introduced by
It seems like morphMany() 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...
58
    }
59
60
    /**
61
     * Determine the activity type.
62
     *
63
     * @param  string $event
64
     * @return string
65
     */
66
    protected function getActivityType($event)
67
    {
68
        $type = strtolower((new \ReflectionClass($this))->getShortName());
69
70
        return "{$event}_{$type}";
71
    }
72
}
73