Completed
Push — master ( 2c4859...5ed06f )
by Adolfo
14s queued 11s
created

HasFeatures::addFeatures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sagitarius29\LaravelSubscriptions\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Sagitarius29\LaravelSubscriptions\PlanFeature;
8
9
trait HasFeatures
10
{
11
    /**
12
     * @param  PlanFeature|Model  $feature
13
     */
14
    public function addFeature(PlanFeature $feature)
15
    {
16
        $this->features()->save($feature);
17
    }
18
19
    public function features(): HasMany
20
    {
21
        return $this->hasMany(config('subscriptions.entities.plan_feature'));
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
22
    }
23
24
    public function addFeatures(array $features)
25
    {
26
        $this->features()->saveMany($features);
0 ignored issues
show
Documentation introduced by
$features is of type array, but the function expects a object<Illuminate\Databa...ent\Relations\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
    }
28
29
    public function getFeatureByCode($featureCode)
30
    {
31
        return $this->features()->whereCode($featureCode)->first();
32
    }
33
34
    public function hasFeatures(): bool
35
    {
36
        return $this->features()->exists();
37
    }
38
}
39