HasAttributes   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
dl 0
loc 78
ccs 16
cts 16
cp 1
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributes() 0 3 1
A addAttribute() 0 5 1
A addAttributes() 0 7 2
A fakeAttributes() 0 7 1
A setAttributes() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
use VGirol\JsonApiConstant\Members;
8
9
/**
10
 * Add "attributes" member to a factory.
11
 */
12
trait HasAttributes
13
{
14
    /**
15
     * The "attributes" object.
16
     *
17
     * @var array
18
     */
19
    protected $attributes;
20
21
    /**
22
     * Set all the attributes.
23
     *
24
     * @param array $attributes
25
     *
26
     * @return static
27
     */
28 30
    public function setAttributes(array $attributes)
29
    {
30 30
        $this->attributes = $attributes;
31
32 30
        return $this;
33
    }
34
35
    /**
36
     * Get all the attributes.
37
     *
38
     * @return array|null
39
     */
40 15
    public function getAttributes(): ?array
41
    {
42 15
        return $this->attributes;
43
    }
44
45
    /**
46
     * Add many attributes.
47
     *
48
     * @param array $attributes
49
     *
50
     * @return static
51
     */
52 3
    public function addAttributes(array $attributes)
53
    {
54 3
        foreach ($attributes as $name => $value) {
55 3
            $this->addAttribute($name, $value);
56
        }
57
58 3
        return $this;
59
    }
60
61
    /**
62
     * Add a single attribute.
63
     *
64
     * @param string $name
65
     * @param mixed $value
66
     *
67
     * @return static
68
     */
69 6
    public function addAttribute(string $name, $value)
70
    {
71 6
        $this->addToObject(Members::ATTRIBUTES, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like addToObject() 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

71
        $this->/** @scrutinizer ignore-call */ 
72
               addToObject(Members::ATTRIBUTES, $name, $value);
Loading history...
72
73 6
        return $this;
74
    }
75
76
    /**
77
     * Fill the "attributes" object with fake members and values.
78
     *
79
     * @param integer $count The number of attributes to generate.
80
     *
81
     * @return static
82
     */
83 18
    public function fakeAttributes(int $count = 5)
84
    {
85 18
        $this->setAttributes(
86 18
            $this->fakeMembers($count)
0 ignored issues
show
Bug introduced by
It seems like fakeMembers() 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

86
            $this->/** @scrutinizer ignore-call */ 
87
                   fakeMembers($count)
Loading history...
87
        );
88
89 18
        return $this;
90
    }
91
}
92