SwipeAway::jsonSerialize()   B
last analyzed

Complexity

Conditions 10
Paths 64

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 14
nc 64
nop 0
dl 0
loc 29
ccs 15
cts 15
cp 1
crap 10
rs 7.6666
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\Gesture;
6
7
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Component\APLBaseComponent;
8
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\GestureType;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\SwipeAction;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\SwipeDirection;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
12
13
class SwipeAway extends AbstractGesture
14
{
15
    /**
16
     * @param SwipeDirection|null $direction Direction the user swipes to activate the gesture
17
     * @param SwipeAction|null $action How to display the original and child components during the swipe gesture
18
     * @param APLBaseComponent|null $item Single item to display during and after the swipe gesture
19
     * @param APLBaseComponent[]|null $items Array of items to display during and after the swipe gesture
20
     * @param AbstractStandardCommand[]|null $onSwipeMove Commands to run as the swipe position changes
21
     * @param AbstractStandardCommand[]|null $onSwipeDone Commands to run when the swipe is complete
22
     * @param AbstractStandardCommand[]|null $onCancel Commands to run when gesture is cancelled
23
     * @param bool|null $when APL boolean expression controlling whether this gesture is active
24
     */
25 5
    public function __construct(
26
        public ?SwipeDirection $direction = null,
27
        public ?SwipeAction $action = null,
28
        public ?APLBaseComponent $item = null,
29
        public ?array $items = null,
30
        public ?array $onSwipeMove = null,
31
        public ?array $onSwipeDone = null,
32
        ?array $onCancel = null,
33
        ?bool $when = null,
34
    ) {
35 5
        parent::__construct(GestureType::SWIPE_AWAY, $onCancel, $when);
36
    }
37
38 3
    public function jsonSerialize(): array
39
    {
40 3
        $data = parent::jsonSerialize();
41
42 3
        if ($this->direction !== null) {
43 1
            $data['direction'] = $this->direction->value;
44
        }
45
46 3
        if ($this->action !== null) {
47 1
            $data['action'] = $this->action->value;
48
        }
49
50 3
        if ($this->item !== null) {
51 1
            $data['item'] = $this->item;
52
        }
53
54 3
        if ($this->items !== null && !empty($this->items)) {
55 1
            $data['items'] = $this->items;
56
        }
57
58 3
        if ($this->onSwipeMove !== null && !empty($this->onSwipeMove)) {
59 1
            $data['onSwipeMove'] = $this->onSwipeMove;
60
        }
61
62 3
        if ($this->onSwipeDone !== null && !empty($this->onSwipeDone)) {
63 1
            $data['onSwipeDone'] = $this->onSwipeDone;
64
        }
65
66 3
        return $data;
67
    }
68
}
69