Passed
Pull Request — main (#15)
by
unknown
07:23
created

Media::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 8
dl 0
loc 14
rs 9.9332

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 DH\Adf\Node\Child;
6
7
use DH\Adf\Node\BlockNode;
8
use DH\Adf\Node\Node;
9
use InvalidArgumentException;
10
11
/**
12
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/media
13
 */
14
class Media extends Node
15
{
16
    public const TYPE_FILE = 'file';
17
    public const TYPE_LINK = 'link';
18
19
    protected string $type = 'media';
20
    private string $id;
21
    private string $mediaType;
22
    private string $collection;
23
    private ?string $occurrenceKey;
24
    private ?string $alt;
25
    private ?int $width;
26
    private ?int $height;
27
28
    public function __construct(string $id, string $mediaType, string $collection, ?int $width = null, ?int $height = null, ?string $occurrenceKey = null, ?BlockNode $parent = null, ?string $alt = null)
29
    {
30
        if (!\in_array($mediaType, [self::TYPE_FILE, self::TYPE_LINK], true)) {
31
            throw new InvalidArgumentException('Invalid media type');
32
        }
33
34
        parent::__construct($parent);
35
        $this->id = $id;
36
        $this->mediaType = $mediaType;
37
        $this->collection = $collection;
38
        $this->occurrenceKey = $occurrenceKey;
39
        $this->width = $width;
40
        $this->height = $height;
41
        $this->alt = $alt;
42
    }
43
44
    public static function load(array $data, ?BlockNode $parent = null): self
45
    {
46
        self::checkNodeData(static::class, $data, ['attrs']);
47
        self::checkRequiredKeys(['id', 'type', 'collection'], $data['attrs']);
48
49
        return new self(
50
            $data['attrs']['id'],
51
            $data['attrs']['type'],
52
            $data['attrs']['collection'],
53
            $data['attrs']['width'] ?? null,
54
            $data['attrs']['height'] ?? null,
55
            $data['attrs']['occurrenceKey'] ?? null,
56
            $parent,
57
            $data['attrs']['alt'] ?? null
58
        );
59
    }
60
61
    public function getId(): string
62
    {
63
        return $this->id;
64
    }
65
66
    public function getMediaType(): string
67
    {
68
        return $this->mediaType;
69
    }
70
71
    public function getCollection(): string
72
    {
73
        return $this->collection;
74
    }
75
76
    public function getOccurrenceKey(): ?string
77
    {
78
        return $this->occurrenceKey;
79
    }
80
81
    public function getWidth(): ?int
82
    {
83
        return $this->width;
84
    }
85
86
    public function getHeight(): ?int
87
    {
88
        return $this->height;
89
    }
90
91
    public function getAlt(): ?int
92
    {
93
        return $this->alt;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->alt could return the type string which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
96
    protected function attrs(): array
97
    {
98
        $attrs = parent::attrs();
99
100
        $attrs['id'] = $this->id;
101
        $attrs['type'] = $this->mediaType;
102
        $attrs['collection'] = $this->collection;
103
104
        if (null !== $this->occurrenceKey) {
105
            $attrs['occurrenceKey'] = $this->occurrenceKey;
106
        }
107
108
        if (null !== $this->width) {
109
            $attrs['width'] = $this->width;
110
        }
111
112
        if (null !== $this->height) {
113
            $attrs['height'] = $this->height;
114
        }
115
116
        if (null !== $this->alt) {
117
            $attrs['alt'] = $this->alt;
118
        }
119
120
        return $attrs;
121
    }
122
}
123