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

HasErrors::getErrors()   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
use VGirol\JsonApiFaker\Contract\ErrorContract;
8
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
9
use VGirol\JsonApiFaker\Messages;
10
11
/**
12
 * Add "errors" member
13
 */
14
trait HasErrors
15
{
16
    /**
17
     * The "errors" member
18
     *
19
     * @var array An array of objects implementing ErrorContract
20
     */
21
    protected $errors;
22
23
    /**
24
     * Set the "errors" member
25
     *
26
     * @param array $errors An array of objects implementing ErrorContract
27
     *
28
     * @return static
29
     * @throws JsonApiFakerException
30
     */
31 12
    public function setErrors(array $errors)
32
    {
33 12
        foreach ($errors as $error) {
34 12
            if (is_a($error, ErrorContract::class) === false) {
35 3
                throw new JsonApiFakerException(Messages::SET_ERRORS_BAD_TYPE);
36
            }
37
        }
38
39 9
        $this->errors = $errors;
40
41 9
        return $this;
42
    }
43
44
    /**
45
     * Get the "errors" member
46
     *
47
     * An array of ErrorContract objects
48
     *
49
     * @return array|null
50
     */
51 15
    public function getErrors(): ?array
52
    {
53 15
        return $this->errors;
54
    }
55
56
    /**
57
     * Add a single error to the "errors" member
58
     *
59
     * @param ErrorContract $error
60
     *
61
     * @return static
62
     */
63 6
    public function addError(ErrorContract $error)
64
    {
65 6
        $this->addToArray('errors', $error);
0 ignored issues
show
Bug introduced by
It seems like addToArray() 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

65
        $this->/** @scrutinizer ignore-call */ 
66
               addToArray('errors', $error);
Loading history...
66
67 6
        return $this;
68
    }
69
70
    /**
71
     * Fill the "errors" member with fake values
72
     *
73
     * @param integer $count
74
     *
75
     * @return static
76
     * @throws JsonApiFakerException
77
     */
78 3
    public function fakeErrors(int $count = 2)
79
    {
80 3
        $collection = [];
81 3
        for ($i = 0; $i < $count; $i++) {
82 3
            $collection[] = $this->generator
83 3
                ->error()
84 3
                ->fake();
85
        }
86
87 3
        return $this->setErrors($collection);
88
    }
89
}
90