HasErrors   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 74
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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

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