HasSpecificationsTrait::specifications()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Pbmedia\Specifications\Laravel\Models;
4
5
use Illuminate\Database\Eloquent\Relations\MorphMany;
6
use Pbmedia\Specifications\AttributeScore;
7
use Pbmedia\Specifications\Interfaces\CanBeSpecified;
8
use Pbmedia\Specifications\Specifications;
9
10
trait HasSpecificationsTrait
11
{
12
    /**
13
     * ScoreModel class name.
14
     *
15
     * @var string
16
     */
17
    protected static $scoreModelClass = ScoreModel::class;
18
19
    /**
20
     * Instance of Specifications.
21
     *
22
     * @var \Pbmedia\Specifications\Specifications
23
     */
24
    protected $specifications;
25
26
    /**
27
     * Returns a Specifications object with the Scores and Attributes
28
     * loaded from the database.
29
     *
30
     * @return \Pbmedia\Specifications\Specifications
31
     */
32
    public function specifications(): Specifications
33
    {
34
        if (!$this->specifications) {
35
            $this->specifications = new Specifications;
36
37
            $this->getRelationValue('Scores')->load('Attribute')->each(function ($scoreModel) {
0 ignored issues
show
Bug introduced by
It seems like getRelationValue() 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...
38
                $this->specifications->add($scoreModel->getAttributeScore());
39
            });
40
        }
41
42
        return $this->specifications;
43
    }
44
45
    /**
46
     * Defines the relationship with the Scores attached to this model.
47
     *
48
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
49
     */
50
    public function Scores(): MorphMany
51
    {
52
        return $this->morphMany(static::$scoreModelClass, 'specifiable');
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...
53
    }
54
55
    /**
56
     * Binds a callback to the 'saved' event of the model which syncs the
57
     * AttributeScore objects in the Specifications instance with
58
     * ones in the database.
59
     */
60
    public static function bootHasSpecificationsTrait()
61
    {
62
        static::saved(function (CanBeSpecified $canBeSpecified) {
63
            $scoreIds = $canBeSpecified->getRelationValue('Scores')->pluck('id');
0 ignored issues
show
Bug introduced by
The method getRelationValue() does not seem to exist on object<Pbmedia\Specifica...erfaces\CanBeSpecified>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
65
            if ($scoreIds->count() > 0) {
66
                app(static::$scoreModelClass)->whereIn('id', $scoreIds)->delete();
67
            }
68
69
            $attributScoreCollection = $canBeSpecified->specifications()->all();
70
71
            if ($attributScoreCollection->isEmpty()) {
72
                return;
73
            }
74
75
            $attributScoreCollection->map(function (AttributeScore $attributeScore) {
76
                return [
77
                    'attribute_id' => $attributeScore->getAttribute()->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Pbmedia\Specifications\Interfaces\Attribute suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
78
                    'value'        => $attributeScore->getScoreValue(),
79
                ];
80
            })->each(function (array $scoreModelAttributes) use ($canBeSpecified) {
81
                $canBeSpecified->Scores()->create($scoreModelAttributes);
0 ignored issues
show
Bug introduced by
The method Scores() does not seem to exist on object<Pbmedia\Specifica...erfaces\CanBeSpecified>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
            });
83
        });
84
    }
85
}
86