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

APLBaseComponent::jsonSerialize()   F

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 98
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 27.5331

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 21
eloc 53
nc 73728
nop 0
dl 0
loc 98
ccs 43
cts 57
cp 0.7544
crap 27.5331
rs 0
c 1
b 0
f 1

How to fix   Long Method    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\Response\Directives\APL\Document\APLComponentType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Bind;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Display;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\LayoutDirection;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\PointerEvents;
12
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\Role;
13
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\TickHandler;
14
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\VisibilityChangeHandler;
15
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
16
17
abstract class APLBaseComponent implements \JsonSerializable
18
{
19
    /**
20
     * @param APLComponentType $type The type of the component
21
     * @param string|null $accessibilityLabel Voice-over reads this string when the user selects this component
22
     * @param Action|null $action Single programmatic equivalent for complex touch interaction
23
     * @param Action[]|null $actions Array of programmatic equivalents for complex touch interactions
24
     * @param Bind[]|null $bind Expressions to add to the data binding context
25
     * @param string|null $description Optional description of this component
26
     * @param bool $checked When true, this component has the checked state set
27
     * @param bool $disabled When true, this component doesn't respond to touch or focus
28
     * @param Display|null $display Determines whether the component displays on the screen
29
     * @param Entity[]|null $entities Array of opaque data used to clarify references in Alexa
30
     * @param Entity|null $entity Single opaque data used to clarify references in Alexa
31
     * @param TickHandler[]|null $handleTick Tick handlers to invoke as time passes
32
     * @param VisibilityChangeHandler[]|null $handleVisibilityChange Visibility handlers to invoke when visibility changes
33
     * @param string|null $height The requested height of the component
34
     * @param string|null $id Reference name of the component, used for navigation and events
35
     * @param bool $inheritParentState When true, replace the component state with the state of the parent component
36
     * @param LayoutDirection|null $layoutDirection The direction in which the component renders
37
     * @param string|null $maxHeight The maximum allowed height of this component
38
     * @param string|null $maxWidth The maximum allowed width of this component
39
     * @param string|null $minHeight The minimum allowed height of this component
40
     * @param string|null $minWidth The minimum allowed width of this component
41
     * @param AbstractStandardCommand[]|null $onMount Commands to run when the component is first displayed
42
     * @param AbstractStandardCommand[]|null $onCursorEnter Commands to run when a cursor enters the active region
43
     * @param AbstractStandardCommand[]|null $onCursorExit Commands to run when a cursor exits the active region
44
     * @param AbstractStandardCommand[]|null $onCursorMove Commands to run when a cursor moves in the active region
45
     * @param AbstractStandardCommand[]|null $onSpeechMark Commands to run when encountering a speech mark
46
     * @param AbstractStandardCommand[]|null $onLayout Commands to run when the layout calculation changes
47
     * @param float $opacity Opacity of this component and children
48
     * @param string[]|null $padding Space to add on the sides of the component
49
     * @param string|null $paddingBottom Space to add to the bottom of this component
50
     * @param string|null $paddingEnd Space to add to the end edge of this component
51
     * @param string|null $paddingLeft Space to add to the left of this component
52
     * @param string|null $paddingRight Space to add to the right of this component
53
     * @param string|null $paddingStart Space to add to the start edge of this component
54
     * @param string|null $paddingTop Space to add to the top of this component
55
     * @param PointerEvents|null $pointerEvents Controls whether the component can be the target of touch events
56
     * @param string[]|null $preserve Properties to save when reinflating the document
57
     * @param Role|null $role Role or purpose of the component
58
     * @param string|null $shadowColor Shadow color
59
     * @param string|null $shadowHorizontalOffset Horizontal offset of the shadow
60
     * @param string|null $shadowRadius Shadow blur radius
61
     * @param string|null $shadowVerticalOffset Vertical offset of the shadow
62
     * @param mixed $speech Transformed speech information for audio playback
63
     * @param array|null $style Named style or styles to apply
64
     * @param string[]|null $trackChanges Properties to track and report changes in the visual context
65
     * @param array|null $transform Array of transformations
66
     * @param bool $when If it evaluates to false, this component doesn't inflate
67
     * @param string|null $width The requested width of this component
68
     */
69 138
    public function __construct(
70
        public APLComponentType $type,
71
        public ?string $accessibilityLabel = null,
72
        public ?Action $action = null,
73
        public ?array $actions = null,
74
        public ?array $bind = null,
75
        public ?string $description = null,
76
        public bool $checked = false,
77
        public bool $disabled = false,
78
        public ?Display $display = null,
79
        public ?array $entities = null,
80
        public ?Entity $entity = null,
81
        public ?array $handleTick = null,
82
        public ?array $handleVisibilityChange = null,
83
        public ?string $height = 'auto',
84
        public ?string $id = null,
85
        public bool $inheritParentState = false,
86
        public ?LayoutDirection $layoutDirection = null,
87
        public ?string $maxHeight = null,
88
        public ?string $maxWidth = null,
89
        public ?string $minHeight = '0',
90
        public ?string $minWidth = '0',
91
        public ?array $onMount = null,
92
        public ?array $onCursorEnter = null,
93
        public ?array $onCursorExit = null,
94
        public ?array $onCursorMove = null,
95
        public ?array $onSpeechMark = null,
96
        public ?array $onLayout = null,
97
        public float $opacity = 1.0,
98
        public ?array $padding = null,
99
        public ?string $paddingBottom = '0',
100
        public ?string $paddingEnd = null,
101
        public ?string $paddingLeft = '0',
102
        public ?string $paddingRight = '0',
103
        public ?string $paddingStart = null,
104
        public ?string $paddingTop = '0',
105
        public ?PointerEvents $pointerEvents = null,
106
        public ?array $preserve = null,
107
        public ?Role $role = null,
108
        public ?string $shadowColor = 'transparent',
109
        public ?string $shadowHorizontalOffset = '0',
110
        public ?string $shadowRadius = '0',
111
        public ?string $shadowVerticalOffset = '0',
112
        public mixed $speech = null,
113
        public ?array $style = null,
114
        public ?array $trackChanges = null,
115
        public ?array $transform = null,
116
        public bool $when = true,
117
        public ?string $width = 'auto',
118
    ) {
119 138
    }
120
121 77
    public function jsonSerialize(): array
122
    {
123 77
        $data = [
124 77
            'type' => $this->type->value,
125 77
        ];
126
127
        // Add all non-null properties
128 77
        $properties = [
129 77
            'accessibilityLabel', 'action', 'actions', 'bind', 'description', 'entities', 'entity',
130 77
            'handleTick', 'handleVisibilityChange', 'id', 'onMount', 'onCursorEnter', 'onCursorExit',
131 77
            'onCursorMove', 'onSpeechMark', 'onLayout', 'padding', 'preserve', 'style', 'trackChanges',
132 77
            'transform', 'speech',
133 77
        ];
134
135 77
        foreach ($properties as $property) {
136 77
            if ($this->$property !== null && (is_array($this->$property) ? !empty($this->$property) : true)) {
137 5
                $data[$property] = $this->$property;
138
            }
139
        }
140
141
        // Add enum properties
142 77
        if ($this->display !== null) {
143
            $data['display'] = $this->display->value;
144
        }
145
146 77
        if ($this->layoutDirection !== null) {
147
            $data['layoutDirection'] = $this->layoutDirection->value;
148
        }
149
150 77
        if ($this->pointerEvents !== null) {
151
            $data['pointerEvents'] = $this->pointerEvents->value;
152
        }
153
154 77
        if ($this->role !== null) {
155
            $data['role'] = $this->role->value;
156
        }
157
158
        // Add boolean properties (only if different from default)
159 77
        if ($this->checked) {
160
            $data['checked'] = $this->checked;
161
        }
162
163 77
        if ($this->disabled) {
164
            $data['disabled'] = $this->disabled;
165
        }
166
167 77
        if ($this->inheritParentState) {
168
            $data['inheritParentState'] = $this->inheritParentState;
169
        }
170
171 77
        if (!$this->when) {
172
            $data['when'] = $this->when;
173
        }
174
175
        // Add dimension and styling properties (only if different from default)
176 77
        $dimensionProperties = [
177 77
            'height' => 'auto',
178 77
            'width' => 'auto',
179 77
            'minHeight' => '0',
180 77
            'minWidth' => '0',
181 77
            'paddingBottom' => '0',
182 77
            'paddingLeft' => '0',
183 77
            'paddingRight' => '0',
184 77
            'paddingTop' => '0',
185 77
            'shadowColor' => 'transparent',
186 77
            'shadowHorizontalOffset' => '0',
187 77
            'shadowRadius' => '0',
188 77
            'shadowVerticalOffset' => '0',
189 77
        ];
190
191 77
        foreach ($dimensionProperties as $property => $default) {
192 77
            if ($this->$property !== null && $this->$property !== $default) {
193
                $data[$property] = $this->$property;
194
            }
195
        }
196
197 77
        if ($this->maxHeight !== null) {
198
            $data['maxHeight'] = $this->maxHeight;
199
        }
200
201 77
        if ($this->maxWidth !== null) {
202
            $data['maxWidth'] = $this->maxWidth;
203
        }
204
205 77
        if ($this->paddingEnd !== null) {
206
            $data['paddingEnd'] = $this->paddingEnd;
207
        }
208
209 77
        if ($this->paddingStart !== null) {
210
            $data['paddingStart'] = $this->paddingStart;
211
        }
212
213
        // Add opacity if different from default
214 77
        if ($this->opacity !== 1.0) {
0 ignored issues
show
introduced by
The condition $this->opacity !== 1.0 is always true.
Loading history...
215
            $data['opacity'] = $this->opacity;
216
        }
217
218 77
        return $data;
219
    }
220
}
221