Attributable   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 8
dl 0
loc 35
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isRequiredAttribute() 0 3 1
A attributeMissing() 0 11 3
A isOptionalAttribute() 0 3 1
1
<?php
2
3
namespace Attribute;
4
5
use Attribute\Exception\MissingRequiredAttributeException;
6
7
trait Attributable
8
{
9
    /**
10
     * @param $attribute
11
     * @return bool
12
     */
13
    public function isRequiredAttribute($attribute)
14
    {
15
        return in_array($attribute, $this->getRequiredAttributes(), true);
0 ignored issues
show
Bug introduced by
It seems like getRequiredAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        return in_array($attribute, $this->/** @scrutinizer ignore-call */ getRequiredAttributes(), true);
Loading history...
16
    }
17
18
    /**
19
     * @param $attribute
20
     * @return bool
21
     */
22
    public function isOptionalAttribute($attribute)
23
    {
24
        return in_array($attribute, $this->getOptionalAttributes(), true);
0 ignored issues
show
Bug introduced by
It seems like getOptionalAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        return in_array($attribute, $this->/** @scrutinizer ignore-call */ getOptionalAttributes(), true);
Loading history...
25
    }
26
27
    /**
28
     * @return bool
29
     * @throws \Attribute\Exception\MissingRequiredAttributeException
30
     */
31
    public function attributeMissing()
32
    {
33
        $requiredAttributes = $this->getRequiredAttributes();
34
35
        foreach ($requiredAttributes as $requiredAttribute) {
36
            if ($this->$requiredAttribute === null) {
37
                throw new MissingRequiredAttributeException("The {$requiredAttribute} is missing");
38
            }
39
        }
40
41
        return true;
42
    }
43
}
44