Context::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 8
dl 0
loc 10
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0

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\Request;
6
7
class Context
8
{
9
    /**
10
     * @param System|null $system System context
11
     * @param AudioPlayer|null $audioPlayer Audio player context
12
     * @param Geolocation|null $geolocation Geolocation context
13
     * @param Advertising|null $advertising Advertising context
14
     * @param Viewport|null $viewport Single viewport context
15
     * @param Viewport[]|null $viewports Array of viewport contexts
16
     * @param AlexaPresentationAPL|null $apl APL presentation context
17
     * @param array|null $extensions Extensions context
18
     */
19 45
    public function __construct(
20
        public ?System $system = null,
21
        public ?AudioPlayer $audioPlayer = null,
22
        public ?Geolocation $geolocation = null,
23
        public ?Advertising $advertising = null,
24
        public ?Viewport $viewport = null,
25
        public ?array $viewports = null,
26
        public ?AlexaPresentationAPL $apl = null,
27
        public ?array $extensions = null,
28
    ) {
29 45
    }
30
31
    /**
32
     * @param array $amazonRequest
33
     *
34
     * @return Context
35
     */
36 37
    public static function fromAmazonRequest(array $amazonRequest): self
37
    {
38 37
        $viewports = null;
39 37
        if (isset($amazonRequest['Viewports']) && is_array($amazonRequest['Viewports'])) {
40 1
            $viewports = [];
41 1
            foreach ($amazonRequest['Viewports'] as $viewportData) {
42 1
                $viewports[] = Viewport::fromAmazonRequest($viewportData);
43
            }
44
        }
45
46 37
        return new self(
47 37
            system: isset($amazonRequest['System']) ? System::fromAmazonRequest($amazonRequest['System']) : null,
48 37
            audioPlayer: isset($amazonRequest['AudioPlayer']) ? AudioPlayer::fromAmazonRequest($amazonRequest['AudioPlayer']) : null,
49 37
            geolocation: isset($amazonRequest['Geolocation']) ? Geolocation::fromAmazonRequest($amazonRequest['Geolocation']) : null,
50 37
            advertising: isset($amazonRequest['Advertising']) ? Advertising::fromAmazonRequest($amazonRequest['Advertising']) : null,
51 37
            viewport: isset($amazonRequest['Viewport']) ? Viewport::fromAmazonRequest($amazonRequest['Viewport']) : null,
52 37
            viewports: $viewports,
53 37
            apl: isset($amazonRequest['Alexa.Presentation.APL']) ? AlexaPresentationAPL::fromAmazonRequest($amazonRequest['Alexa.Presentation.APL']) : null,
54 37
            extensions: $amazonRequest['Extensions'] ?? null,
55 37
        );
56
    }
57
}
58