Completed
Push — master ( da0ab4...8ffee5 )
by Vincent
03:47
created

HasAttributes::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiFaker\Factory;
6
7
/**
8
 * Add "attributes" member to a factory.
9
 */
10
trait HasAttributes
11
{
12
    /**
13
     * The "attributes" object.
14
     *
15
     * @var array
16
     */
17
    protected $attributes;
18
19
    /**
20
     * Set all the attributes.
21
     *
22
     * @param array $attributes
23
     *
24
     * @return static
25
     */
26 30
    public function setAttributes(array $attributes)
27
    {
28 30
        $this->attributes = $attributes;
29
30 30
        return $this;
31
    }
32
33
    /**
34
     * Get all the attributes.
35
     *
36
     * @return array|null
37
     */
38 15
    public function getAttributes(): ?array
39
    {
40 15
        return $this->attributes;
41
    }
42
43
    /**
44
     * Add many attributes.
45
     *
46
     * @param array $attributes
47
     *
48
     * @return static
49
     */
50 3
    public function addAttributes(array $attributes)
51
    {
52 3
        foreach ($attributes as $name => $value) {
53 3
            $this->addAttribute($name, $value);
54
        }
55
56 3
        return $this;
57
    }
58
59
    /**
60
     * Add a single attribute.
61
     *
62
     * @param string $name
63
     * @param mixed $value
64
     *
65
     * @return static
66
     */
67 6
    public function addAttribute(string $name, $value)
68
    {
69 6
        $this->addToObject('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

69
        $this->/** @scrutinizer ignore-call */ 
70
               addToObject('attributes', $name, $value);
Loading history...
70
71 6
        return $this;
72
    }
73
74
    /**
75
     * Fill the "attributes" object with fake members and values.
76
     *
77
     * @param integer $count The number of attributes to generate.
78
     *
79
     * @return static
80
     */
81 18
    public function fakeAttributes(int $count = 5)
82
    {
83 18
        $this->setAttributes(
84 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

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