HasAchievements::achievementPoints()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Laravel\Achievements\Traits;
4
5
/**
6
 * Trait HasAchievements
7
 *
8
 * @property \Illuminate\Database\Eloquent\Collection|\Laravel\Achievements\AchievementModel[] $achievements
9
 * @property \Illuminate\Database\Eloquent\Collection|\Laravel\Achievements\AchievementCriteriaModel[] $achievementCriterias
10
 */
11
trait HasAchievements
12
{
13
    /**
14
     * Completed achievements list.
15
     *
16
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
17
     */
18
    public function achievements()
19
    {
20
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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...
21
            config('achievements.models.achievement'),
22
            'achievementable',
23
            'achievementables',
24
            null,
25
            'achievement_model_id'
26
        )->withPivot(['completed_at']);
27
    }
28
29
    /**
30
     * Total achievement points.
31
     *
32
     * @return int
33
     */
34
    public function achievementPoints()
35
    {
36
        return intval($this->achievements()->sum('points'));
37
    }
38
39
    /**
40
     * Achievement criterias progress.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
43
     */
44
    public function achievementCriterias()
45
    {
46
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() 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...
47
            config('achievements.models.criteria'),
48
            'achievement_criteriable',
49
            'achievement_criteriables',
50
            null,
51
            'achievement_criteria_model_id'
52
        )->withPivot(['value', 'completed', 'progress_data', 'updated_at']);
53
    }
54
}
55