TouchableComponent::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
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 8
dl 0
loc 11
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\Response\Directives\APL\Document\APLComponentType;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Gesture\AbstractGesture;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
10
11
abstract class TouchableComponent extends ActionableComponent implements \JsonSerializable
12
{
13
    /**
14
     * @param APLComponentType $type The type of the component
15
     * @param AbstractGesture|null $gesture Single gesture handler
16
     * @param AbstractGesture[]|null $gestures Array of gesture handlers
17
     * @param AbstractStandardCommand[]|null $onCancel Commands to run when a gesture takes over the pointer
18
     * @param AbstractStandardCommand[]|null $onDown Commands to run when a pointer down event occurs
19
     * @param AbstractStandardCommand[]|null $onMove Commands to run as the pointer moves
20
     * @param AbstractStandardCommand[]|null $onPress Commands to run for a pointer down followed by a pointer up
21
     * @param AbstractStandardCommand[]|null $onUp Commands to run when releasing the pointer
22
     */
23 20
    public function __construct(
24
        APLComponentType $type,
25
        public ?AbstractGesture $gesture = null,
26
        public ?array $gestures = null,
27
        public ?array $onCancel = null,
28
        public ?array $onDown = null,
29
        public ?array $onMove = null,
30
        public ?array $onPress = null,
31
        public ?array $onUp = null,
32
    ) {
33 20
        parent::__construct($type);
34
    }
35
36 12
    public function jsonSerialize(): array
37
    {
38 12
        $data = parent::jsonSerialize();
39
40 12
        if ($this->gesture !== null) {
41
            $data['gesture'] = $this->gesture;
42
        }
43
44 12
        if (!empty($this->gestures)) {
45 1
            $data['gestures'] = $this->gestures;
46
        }
47
48 12
        if (!empty($this->onCancel)) {
49 1
            $data['onCancel'] = $this->onCancel;
50
        }
51
52 12
        if (!empty($this->onDown)) {
53 1
            $data['onDown'] = $this->onDown;
54
        }
55
56 12
        if (!empty($this->onMove)) {
57 1
            $data['onMove'] = $this->onMove;
58
        }
59
60 12
        if (!empty($this->onPress)) {
61 1
            $data['onPress'] = $this->onPress;
62
        }
63
64 12
        if (!empty($this->onUp)) {
65 1
            $data['onUp'] = $this->onUp;
66
        }
67
68 12
        return $data;
69
    }
70
}
71