RecordsActivity   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
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 65
rs 10
c 0
b 0
f 0
ccs 0
cts 31
cp 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bootRecordsActivity() 0 13 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
 * Trait RecordsActivity
7
 * @package Scool\EnrollmentMobile\Traits
8
 */
9
trait RecordsActivity
10
{
11
    /**
12
     * Boot the trait.
13
     */
14
    protected static function bootRecordsActivity()
15
    {
16
        // Comprovar si no hi ha usuari logat.
17
        if (auth()->guest()) {
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...
18
            return;
19
        }
20
21
        foreach (static::getActivitiesToRecord() as $event) {
22
            static::$event(function ($model) use ($event) {
23
                $model->recordActivity($event);
24
            });
25
        }
26
    }
27
28
    /**
29
     * Fetch all model events that require activity recording.
30
     *
31
     * @return array
32
     */
33
    protected static function getActivitiesToRecord()
34
    {
35
        return ['created', 'updated', 'deleted'];
36
    }
37
38
    /**
39
     * Record new activity for the model.
40
     *
41
     * @param string $event
42
     */
43
    protected function recordActivity($event)
44
    {
45
        $this->activity()->create([
46
            '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...
47
            'type' => $this->getActivityType($event)
48
        ]);
49
    }
50
51
    /**
52
     * Fetch the activity relationship.
53
     *
54
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
55
     */
56
    public function activity()
57
    {
58
        return $this->morphMany('Scool\EnrollmentMobile\Models\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...
59
    }
60
61
    /**
62
     * Determine the activity type.
63
     *
64
     * @param  string $event
65
     * @return string
66
     */
67
    protected function getActivityType($event)
68
    {
69
        $type = strtolower((new \ReflectionClass($this))->getShortName());
70
71
        return "{$event}_{$type}";
72
    }
73
}
74