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

TextComponent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
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 13
dl 0
loc 16
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\Document\FontStyle;
9
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\FontWeight;
10
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\TextAlign;
11
use MaxBeckers\AmazonAlexa\Response\Directives\APL\Document\TextAlignVertical;
12
use MaxBeckers\AmazonAlexa\Response\Directives\APL\StandardCommand\AbstractStandardCommand;
13
14
class TextComponent extends APLBaseComponent implements \JsonSerializable
15
{
16
    public const TYPE = APLComponentType::TEXT;
17
18
    /**
19
     * @param string|null $color The color of the text
20
     * @param string $fontFamily Font family (such as "Amazon Ember Display")
21
     * @param string $fontSize The size of the text
22
     * @param FontStyle|null $fontStyle The font style to display
23
     * @param FontWeight|null $fontWeight The font weight to display
24
     * @param string $lang The language of the text
25
     * @param string $letterSpacing Additional space to add between letters
26
     * @param string $lineHeight Line-height multiplier
27
     * @param int $maxLines The maximum number of lines of text to display
28
     * @param AbstractStandardCommand[]|null $onTextLayout Commands to run when the text layout changes
29
     * @param string $text The markup to display in this text box
30
     * @param TextAlign|null $textAlign Alignment of text within a paragraph
31
     * @param TextAlignVertical|null $textAlignVertical Vertical alignment of text
32
     */
33 15
    public function __construct(
34
        public ?string $color = null,
35
        public string $fontFamily = 'sans-serif',
36
        public string $fontSize = '40dp',
37
        public ?FontStyle $fontStyle = null,
38
        public ?FontWeight $fontWeight = null,
39
        public string $lang = '',
40
        public string $letterSpacing = '0',
41
        public string $lineHeight = '125%',
42
        public int $maxLines = 0,
43
        public ?array $onTextLayout = null,
44
        public string $text = '',
45
        public ?TextAlign $textAlign = null,
46
        public ?TextAlignVertical $textAlignVertical = null,
47
    ) {
48 15
        parent::__construct(self::TYPE);
49
    }
50
51 9
    public function jsonSerialize(): array
52
    {
53 9
        $data = parent::jsonSerialize();
54
55 9
        if ($this->color !== null) {
56 1
            $data['color'] = $this->color;
57
        }
58
59 9
        if ($this->fontFamily !== 'sans-serif') {
60 2
            $data['fontFamily'] = $this->fontFamily;
61
        }
62
63 9
        if ($this->fontSize !== '40dp') {
64 2
            $data['fontSize'] = $this->fontSize;
65
        }
66
67 9
        if ($this->fontStyle !== null) {
68 2
            $data['fontStyle'] = $this->fontStyle->value;
69
        }
70
71 9
        if ($this->fontWeight !== null) {
72 2
            $data['fontWeight'] = $this->fontWeight->value;
73
        }
74
75 9
        if ($this->lang !== '') {
76 2
            $data['lang'] = $this->lang;
77
        }
78
79 9
        if ($this->letterSpacing !== '0') {
80 2
            $data['letterSpacing'] = $this->letterSpacing;
81
        }
82
83 9
        if ($this->lineHeight !== '125%') {
84 2
            $data['lineHeight'] = $this->lineHeight;
85
        }
86
87 9
        if ($this->maxLines !== 0) {
88 2
            $data['maxLines'] = $this->maxLines;
89
        }
90
91 9
        if ($this->onTextLayout !== null && !empty($this->onTextLayout)) {
92 1
            $data['onTextLayout'] = $this->onTextLayout;
93
        }
94
95 9
        if ($this->text !== '') {
96 2
            $data['text'] = $this->text;
97
        }
98
99 9
        if ($this->textAlign !== null) {
100 2
            $data['textAlign'] = $this->textAlign->value;
101
        }
102
103 9
        if ($this->textAlignVertical !== null) {
104 2
            $data['textAlignVertical'] = $this->textAlignVertical->value;
105
        }
106
107 9
        return $data;
108
    }
109
}
110