Path::addArrayIfNotEmpty()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 2
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\AVGItem;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\AVGItemType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\StrokeLineCap;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\StrokeLineJoin;
10
11
class Path extends AVGItem implements \JsonSerializable
12
{
13
    public const TYPE = AVGItemType::PATH;
14
15
    /**
16
     * @param string|null $fill Fill color
17
     * @param int|null $fillOpacity Fill opacity
18
     * @param string|null $fillTransform Fill transform
19
     * @param string|null $pathData Path data string
20
     * @param int|null $pathLength Path length
21
     * @param string|null $stroke Stroke color
22
     * @param int[]|null $strokeDashArray Array of dash lengths
23
     * @param int|null $strokeDashOffset Dash offset
24
     * @param StrokeLineCap|null $strokeLineCap Line cap style
25
     * @param StrokeLineJoin|null $strokeLineJoin Line join style
26
     * @param int|null $strokeMiterLimit Miter limit
27
     * @param int|null $strokeOpacity Stroke opacity
28
     * @param string|null $strokeTransform Stroke transform
29
     * @param int|null $strokeWidth Stroke width
30
     */
31 10
    public function __construct(
32
        public ?string $fill = null,
33
        public ?int $fillOpacity = null,
34
        public ?string $fillTransform = null,
35
        public ?string $pathData = null,
36
        public ?int $pathLength = null,
37
        public ?string $stroke = null,
38
        public ?array $strokeDashArray = null,
39
        public ?int $strokeDashOffset = null,
40
        public ?StrokeLineCap $strokeLineCap = null,
41
        public ?StrokeLineJoin $strokeLineJoin = null,
42
        public ?int $strokeMiterLimit = null,
43
        public ?int $strokeOpacity = null,
44
        public ?string $strokeTransform = null,
45
        public ?int $strokeWidth = null,
46
    ) {
47 10
        parent::__construct(self::TYPE);
48
    }
49
50 6
    public function jsonSerialize(): array
51
    {
52 6
        $data = parent::jsonSerialize();
53
54 6
        $this->addScalar($data, 'fill', $this->fill);
55 6
        $this->addScalar($data, 'fillOpacity', $this->fillOpacity);
56 6
        $this->addScalar($data, 'fillTransform', $this->fillTransform);
57 6
        $this->addScalar($data, 'pathData', $this->pathData);
58 6
        $this->addScalar($data, 'pathLength', $this->pathLength);
59 6
        $this->addScalar($data, 'stroke', $this->stroke);
60 6
        $this->addArrayIfNotEmpty($data, 'strokeDashArray', $this->strokeDashArray);
61 6
        $this->addScalar($data, 'strokeDashOffset', $this->strokeDashOffset);
62 6
        $this->addEnum($data, 'strokeLineCap', $this->strokeLineCap);
63 6
        $this->addEnum($data, 'strokeLineJoin', $this->strokeLineJoin);
64 6
        $this->addScalar($data, 'strokeMiterLimit', $this->strokeMiterLimit);
65 6
        $this->addScalar($data, 'strokeOpacity', $this->strokeOpacity);
66 6
        $this->addScalar($data, 'strokeTransform', $this->strokeTransform);
67 6
        $this->addScalar($data, 'strokeWidth', $this->strokeWidth);
68
69 6
        return $data;
70
    }
71
72
    /**
73
     * @param array<string,mixed> $data
74
     */
75 6
    private function addScalar(array &$data, string $key, mixed $value): void
76
    {
77 6
        if ($value !== null) {
78 2
            $data[$key] = $value;
79
        }
80
    }
81
82
    /**
83
     * @param array<string,mixed> $data
84
     * @param array<mixed>|null $value
85
     */
86 6
    private function addArrayIfNotEmpty(array &$data, string $key, ?array $value): void
87
    {
88 6
        if ($value !== null && $value !== []) {
89 1
            $data[$key] = $value;
90
        }
91
    }
92
93
    /**
94
     * @param array<string,mixed> $data
95
     */
96 6
    private function addEnum(array &$data, string $key, ?\UnitEnum $enum): void
0 ignored issues
show
Bug introduced by
The type UnitEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
97
    {
98 6
        if ($enum !== null) {
99 3
            $data[$key] = $enum->value;
100
        }
101
    }
102
}
103