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

GridSequenceComponent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 9
dl 0
loc 12
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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