Passed
Pull Request — master (#97)
by Maximilian
04:02
created

GridSequenceComponent::jsonSerialize()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 45
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 23
nc 512
nop 0
dl 0
loc 45
ccs 25
cts 25
cp 1
crap 10
rs 4.1777
c 1
b 0
f 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace MaxBeckers\AmazonAlexa\Response\Directives\APL\Component;
6
7
use MaxBeckers\AmazonAlexa\Request\ScrollDirection;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\Traits\ActionableComponentTrait;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\Traits\MultiChildComponentTrait;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\APLComponentType;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Snap;
12
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
13
14
class GridSequenceComponent extends APLBaseComponent implements \JsonSerializable
15
{
16
    use ActionableComponentTrait;
17
    use MultiChildComponentTrait;
18
19
    public const TYPE = APLComponentType::GRID_SEQUENCE;
20
21
    /**
22
     * @param string|null $childHeight The height of children
23
     * @param string[]|null $childHeights Array of height dimensions for children
24
     * @param string|null $childWidth The width of children
25
     * @param string[]|null $childWidths Array of width dimensions for children
26
     * @param bool $numbered When true, assign ordinal numbers to the GridSequence children
27
     * @param AbstractStandardCommand[]|null $onScroll Commands to run during scrolling
28
     * @param string[]|null $preserve Properties to save when reinflating the document
29
     * @param ScrollDirection|null $scrollDirection The direction to scroll this GridSequence
30
     * @param Snap|null $snap The alignment that the child components snap to when scrolling stops
31
     */
32 11
    public function __construct(
33
        public ?string $childHeight = null,
34
        public ?array $childHeights = null,
35
        public ?string $childWidth = null,
36
        public ?array $childWidths = null,
37
        public bool $numbered = false,
38
        public ?array $onScroll = null,
39
        ?array $preserve = null,
40
        public ?ScrollDirection $scrollDirection = null,
41
        public ?Snap $snap = null,
42
    ) {
43 11
        parent::__construct(type: self::TYPE, preserve: $preserve);
44
    }
45
46 5
    public function jsonSerialize(): array
47
    {
48 5
        $data = array_merge(
49 5
            parent::jsonSerialize(),
50 5
            $this->serializeActionableProperties(),
51 5
            $this->serializeMultiChildProperties()
52 5
        );
53
54 5
        if ($this->childHeight !== null) {
55 1
            $data['childHeight'] = $this->childHeight;
56
        }
57
58 5
        if (!empty($this->childHeights)) {
59 1
            $data['childHeights'] = $this->childHeights;
60
        }
61
62 5
        if ($this->childWidth !== null) {
63 1
            $data['childWidth'] = $this->childWidth;
64
        }
65
66 5
        if (!empty($this->childWidths)) {
67 1
            $data['childWidths'] = $this->childWidths;
68
        }
69
70 5
        if ($this->numbered) {
71 2
            $data['numbered'] = $this->numbered;
72
        }
73
74 5
        if (!empty($this->onScroll)) {
75 1
            $data['onScroll'] = $this->onScroll;
76
        }
77
78 5
        if (!empty($this->preserve)) {
79 1
            $data['preserve'] = $this->preserve;
80
        }
81
82 5
        if ($this->scrollDirection !== null) {
83 1
            $data['scrollDirection'] = $this->scrollDirection->value;
84
        }
85
86 5
        if ($this->snap !== null) {
87 1
            $data['snap'] = $this->snap->value;
88
        }
89
90 5
        return $data;
91
    }
92
}
93