Passed
Push — master ( e59319...214e16 )
by Caen
03:19 queued 12s
created

FeaturedImage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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

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 Hyde\Framework\Features\Blogging\Models;
6
7
use Stringable;
8
9
abstract class FeaturedImage implements Stringable
10
{
11
    protected readonly string $source;
12
13
    protected readonly ?string $altText;
14
    protected readonly ?string $titleText;
15
16
    protected readonly ?string $authorName;
17
    protected readonly ?string $authorUrl;
18
19
    protected readonly ?string $copyrightText;
20
    protected readonly ?string $licenseName;
21
    protected readonly ?string $licenseUrl;
22
23
    public function __construct(string $source, ?string $altText, ?string $titleText, ?string $authorName, ?string $authorUrl, ?string $copyrightText, ?string $licenseName, ?string $licenseUrl)
24
    {
25
        $this->source = $this->setSource($source);
0 ignored issues
show
Bug introduced by
The property source is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
26
27
        $this->altText = $altText;
0 ignored issues
show
Bug introduced by
The property altText is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
28
        $this->titleText = $titleText;
0 ignored issues
show
Bug introduced by
The property titleText is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
29
        $this->authorName = $authorName;
0 ignored issues
show
Bug introduced by
The property authorName is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
30
        $this->authorUrl = $authorUrl;
0 ignored issues
show
Bug introduced by
The property authorUrl is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
31
        $this->copyrightText = $copyrightText;
0 ignored issues
show
Bug introduced by
The property copyrightText is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
32
        $this->licenseName = $licenseName;
0 ignored issues
show
Bug introduced by
The property licenseName is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
33
        $this->licenseUrl = $licenseUrl;
0 ignored issues
show
Bug introduced by
The property licenseUrl is declared read-only in Hyde\Framework\Features\...ng\Models\FeaturedImage.
Loading history...
34
    }
35
36
    public function __toString(): string
37
    {
38
        return $this->getSource();
39
    }
40
41
    /**
42
     * Get the source of the image, must be usable within the src attribute of an image tag,
43
     * and is thus not necessarily the path to the source image on disk.
44
     *
45
     * @return string The image's url or path
46
     */
47
    abstract public function getSource(): string;
48
49
    protected function setSource(string $source): string
50
    {
51
        return $source;
52
    }
53
54
    abstract public function getContentLength(): int;
55
56
    public function getAltText(): ?string
57
    {
58
        return $this->altText;
59
    }
60
61
    public function getTitleText(): ?string
62
    {
63
        return $this->titleText;
64
    }
65
66
    public function getAuthorName(): ?string
67
    {
68
        return $this->authorName;
69
    }
70
71
    public function getAuthorUrl(): ?string
72
    {
73
        return $this->authorUrl;
74
    }
75
76
    public function getCopyrightText(): ?string
77
    {
78
        return $this->copyrightText;
79
    }
80
81
    public function getLicenseName(): ?string
82
    {
83
        return $this->licenseName;
84
    }
85
86
    public function getLicenseUrl(): ?string
87
    {
88
        return $this->licenseUrl;
89
    }
90
91
    public function hasAltText(): bool
92
    {
93
        return $this->altText !== null;
94
    }
95
96
    public function hasTitleText(): bool
97
    {
98
        return $this->titleText !== null;
99
    }
100
101
    public function hasAuthorName(): bool
102
    {
103
        return $this->authorName !== null;
104
    }
105
106
    public function hasAuthorUrl(): bool
107
    {
108
        return $this->authorUrl !== null;
109
    }
110
111
    public function hasCopyrightText(): bool
112
    {
113
        return $this->copyrightText !== null;
114
    }
115
116
    public function hasLicenseName(): bool
117
    {
118
        return $this->licenseName !== null;
119
    }
120
121
    public function hasLicenseUrl(): bool
122
    {
123
        return $this->licenseUrl !== null;
124
    }
125
126
    /**
127
     * Used in resources/views/components/post/image.blade.php to add meta tags with itemprop attributes.
128
     *
129
     * @return array{text?: string|null, name?: string|null, url: string, contentUrl: string}
130
     */
131
    public function getMetadataArray(): array
132
    {
133
        $metadata = [];
134
135
        if ($this->hasAltText()) {
136
            $metadata['text'] = $this->getAltText();
137
        }
138
139
        if ($this->hasTitleText()) {
140
            $metadata['name'] = $this->getTitleText();
141
        }
142
143
        $metadata['url'] = $this->getSource();
144
        $metadata['contentUrl'] = $this->getSource();
145
146
        return $metadata;
147
    }
148
}
149