• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

harmim / images / #1415

pending completion
#1415

push

harmim
Use PHPStan and coding standard

377 of 377 new or added lines in 7 files covered. (100.0%)

447 of 525 relevant lines covered (85.14%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.63
/src/Config/Config.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Harmim\Images\Config;
6

7
use Harmim;
8
use Nette;
9

10

11
/**
12
 * @property-read array<string,Items> $types
13
 * @property-read ?string $type
14
 * @property-read ?string $destDir
15
 * @property-read ?bool $orig
16
 * @property-read ?bool $compressed
17
 */
18
readonly class Config extends Items
19
{
20
        public const Defaults = [
21
                'types' => [],
22
                'type' => null,
23
                'destDir' => null,
24
                'orig' => null,
25
                'compressed' => null,
26
        ] + parent::Defaults;
27

28

29
        /**
30
         * @param  array<string, Items>  $types
31
         */
32
        public function __construct(
1✔
33
                string $wwwDir = self::Defaults['wwwDir'],
34
                string $imagesDir = self::Defaults['imagesDir'],
35
                string $origDir = self::Defaults['origDir'],
36
                string $compressionDir = self::Defaults['compressionDir'],
37
                string $placeholder = self::Defaults['placeholder'],
38
                int $width = self::Defaults['width'],
39
                int $height = self::Defaults['height'],
40
                int $compression = self::Defaults['compression'],
41
                Harmim\Images\Resize|array $transform = self::Defaults['transform'],
42
                array $allowedImgTagAttrs = self::Defaults['allowedImgTagAttrs'],
43
                array $imgTagAttrs = self::Defaults['imgTagAttrs'],
44
                bool $lazy = self::Defaults['lazy'],
45
                private array $types = self::Defaults['types'],
46
                private ?string $type = self::Defaults['type'],
47
                private ?string $destDir = self::Defaults['destDir'],
48
                private ?bool $orig = self::Defaults['orig'],
49
                private ?bool $compressed = self::Defaults['compressed'],
50
        ) {
51
                parent::__construct(
1✔
52
                        wwwDir: $wwwDir,
1✔
53
                        imagesDir: $imagesDir,
54
                        origDir: $origDir,
55
                        compressionDir: $compressionDir,
56
                        placeholder: $placeholder,
57
                        width: $width,
58
                        height: $height,
59
                        compression: $compression,
60
                        transform: $transform,
61
                        allowedImgTagAttrs: $allowedImgTagAttrs,
62
                        imgTagAttrs: $imgTagAttrs,
63
                        lazy: $lazy,
64
                );
65
        }
1✔
66

67

68
        /**
69
         * @return array<string, Items>
70
         */
71
        final public function getTypes(): array
72
        {
73
                return $this->types;
1✔
74
        }
75

76

77
        final public function getType(): ?string
78
        {
79
                return $this->type;
1✔
80
        }
81

82

83
        final public function getDestDir(): ?string
84
        {
85
                return $this->destDir;
1✔
86
        }
87

88

89
        final public function isOrig(): ?bool
90
        {
91
                return $this->orig;
1✔
92
        }
93

94

95
        final public function isCompressed(): ?bool
96
        {
97
                return $this->compressed;
1✔
98
        }
99

100

101
        public static function fromArray(array $array): self
1✔
102
        {
103
                $items = parent::fromArray($array);
1✔
104
                assert(isset($array['types']) && is_array($array['types']));
1✔
105
                $types = Nette\Utils\Arrays::map(
1✔
106
                        $array['types'],
1✔
107
                        static function (
1✔
108
                                mixed $typeItems,
109
                                mixed $type,
110
                        ) use ($array): parent {
1✔
111
                                assert(is_string($type) && is_array($typeItems));
1✔
112
                                return parent::fromArray($typeItems + $array);
1✔
113
                        },
1✔
114
                );
115
                $type = isset($array['type']) && is_string($array['type'])
1✔
116
                        ? $array['type']
1✔
117
                        : null;
1✔
118
                $destDir = isset($array['destDir']) && is_string($array['destDir'])
1✔
119
                        ? $array['destDir']
×
120
                        : null;
1✔
121
                $orig = isset($array['orig']) && is_bool($array['orig'])
1✔
122
                        ? $array['orig']
1✔
123
                        : null;
1✔
124
                $compressed =
1✔
125
                        isset($array['compressed']) && is_bool($array['compressed'])
1✔
126
                                ? $array['compressed']
1✔
127
                                : null;
1✔
128

129
                return new self(
1✔
130
                        wwwDir: $items->wwwDir,
1✔
131
                        imagesDir: $items->imagesDir,
1✔
132
                        origDir: $items->origDir,
1✔
133
                        compressionDir: $items->compressionDir,
1✔
134
                        placeholder: $items->placeholder,
1✔
135
                        width: $items->width,
1✔
136
                        height: $items->height,
1✔
137
                        compression: $items->compression,
1✔
138
                        transform: $items->transform,
1✔
139
                        allowedImgTagAttrs: $items->allowedImgTagAttrs,
1✔
140
                        imgTagAttrs: $items->imgTagAttrs,
1✔
141
                        lazy: $items->lazy,
1✔
142
                        types: $types,
143
                        type: $type,
144
                        destDir: $destDir,
145
                        orig: $orig,
146
                        compressed: $compressed,
147
                );
148
        }
149

150

151
        public function toArray(): array
152
        {
153
                return [
154
                        'types' => Nette\Utils\Arrays::map(
1✔
155
                                $this->types,
1✔
156
                                static fn(Items $typeItems): array => $typeItems->toArray(),
1✔
157
                        ),
158
                ] + get_object_vars($this) + parent::toArray();
1✔
159
        }
160

161

162
        /**
163
         * @param  array<string, mixed>  $options
164
         */
165
        public function mergeWithOptions(array $options): self
1✔
166
        {
167
                $typeConfig = [];
1✔
168
                if (isset($options['type'])) {
1✔
169
                        assert(
1✔
170
                                is_string($options['type'])
1✔
171
                                && array_key_exists($options['type'], $this->types),
1✔
172
                        );
173
                        $typeConfig = $this->types[$options['type']]->toArray();
1✔
174
                }
175

176
                $merged = $options + $typeConfig + $this->toArray();
1✔
177

178
                if (isset($merged['imgTagAttrs'])) {
1✔
179
                        $merged['imgTagAttrs'] = array_filter(
1✔
180
                                $options,
1✔
181
                                static function (mixed $v, string $k) use ($merged): bool {
1✔
182
                                        if (
183
                                                isset($merged['allowedImgTagAttrs'])
1✔
184
                                                && is_array($merged['allowedImgTagAttrs'])
1✔
185
                                        ) {
186
                                                foreach ($merged['allowedImgTagAttrs'] as $attr) {
1✔
187
                                                        assert(is_string($attr));
1✔
188
                                                        if (str_starts_with($k, $attr)) {
1✔
189
                                                                assert(is_scalar($v));
1✔
190
                                                                return true;
1✔
191
                                                        }
192
                                                }
193
                                        }
194

195
                                        return false;
1✔
196
                                },
1✔
197
                                ARRAY_FILTER_USE_BOTH,
1✔
198
                        ) + $merged['imgTagAttrs'];
1✔
199
                }
200

201
                return self::fromArray($merged);
1✔
202
        }
203
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc