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

68publishers / image-storage / 22677854876

04 Mar 2026 04:07PM UTC coverage: 87.038% (-4.3%) from 91.356%
22677854876

push

github

tg666
Added port to Referer header if exists

0 of 3 new or added lines in 1 file covered. (0.0%)

113 existing lines in 8 files now uncovered.

1484 of 1705 relevant lines covered (87.04%)

0.87 hits per line

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

56.67
/src/Responsive/Descriptor/WDescriptor.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\ImageStorage\Responsive\Descriptor;
6

7
use SixtyEightPublishers\ImageStorage\Exception\InvalidArgumentException;
8
use SixtyEightPublishers\ImageStorage\Modifier\Collection\ModifierCollectionInterface;
9
use SixtyEightPublishers\ImageStorage\Modifier\Width;
10
use SixtyEightPublishers\ImageStorage\Responsive\SrcSet;
11
use function array_map;
12
use function array_unique;
13
use function array_values;
14
use function implode;
15
use function is_numeric;
16
use function range;
17
use function sprintf;
18
use function var_export;
19

20
final class WDescriptor implements DescriptorInterface
21
{
22
    /** @var array<int> */
23
    private array $widths;
24

25
    public function __construct(int ...$widths)
26
    {
27
        $this->widths = $widths;
1✔
28
    }
1✔
29

30
    public static function fromRange(int $min, int $max, int $step = 100): self
31
    {
32
        if (0 >= $min || 0 >= $max || 0 >= $step) {
1✔
33
            throw new InvalidArgumentException(sprintf(
1✔
34
                'Can not create WDescriptor from the range %d..%d with step %d.',
1✔
35
                $min,
36
                $max,
37
                $step,
38
            ));
39
        }
40

41
        if ($max < $min) {
1✔
42
            $tmp = $min;
1✔
43
            $min = $max;
1✔
44
            $max = $tmp;
1✔
45
        }
46

47
        if (($max - $min) < $step) {
1✔
48
            throw new InvalidArgumentException(sprintf(
1✔
49
                'Can not create WDescriptor from the range %d..%d with step %d. The step must not exceed the specified range.',
1✔
50
                $min,
51
                $max,
52
                $step,
53
            ));
54
        }
55

56
        $range = range($min, $max, $step);
1✔
57
        $range[] = $max;
1✔
58

59
        return new self(...array_values(array_unique($range)));
1✔
60
    }
61

62
    public function validateModifierValue(
63
        mixed $value,
64
        mixed $default,
65
    ): int {
UNCOV
66
        if (true === $value) {
×
UNCOV
67
            if (!is_numeric($default) && [] !== $this->widths) {
×
UNCOV
68
                return $this->widths[0];
×
69
            }
70

UNCOV
71
            $value = $default;
×
72
        }
73

UNCOV
74
        if (is_numeric($value) && in_array((int) $value, $this->widths, true)) {
×
UNCOV
75
            return (int) $value;
×
76
        }
77

UNCOV
78
        throw new InvalidArgumentException(
×
UNCOV
79
            message: sprintf(
×
UNCOV
80
                'Invalid preset value "%s" passed for descriptor %s',
×
UNCOV
81
                var_export($value, true),
×
82
                $this,
83
            ),
84
        );
85
    }
86

87
    public function expandModifier(
88
        ModifierCollectionInterface $modifierCollection,
89
        mixed $value,
90
    ): array {
UNCOV
91
        $wAlias = $modifierCollection
×
UNCOV
92
            ->getByName(Width::class)
×
UNCOV
93
            ->getAlias();
×
94

UNCOV
95
        if (is_numeric($value) && in_array((int) $value, $this->widths, true)) {
×
96
            return [
UNCOV
97
                $wAlias => (int) $value,
×
98
            ];
99
        }
100

UNCOV
101
        throw new InvalidArgumentException(
×
UNCOV
102
            message: sprintf(
×
UNCOV
103
                'Invalid preset value "%s" passed for descriptor %s',
×
UNCOV
104
                var_export($value, true),
×
105
                $this,
106
            ),
107
        );
108
    }
109

UNCOV
110
    public function iterateModifiers(ModifierCollectionInterface $modifierCollection): iterable
×
111
    {
UNCOV
112
        $wAlias = $modifierCollection
×
UNCOV
113
            ->getByName(Width::class)
×
UNCOV
114
            ->getAlias();
×
115

UNCOV
116
        foreach ($this->widths as $width) {
×
117
            yield [
UNCOV
118
                $wAlias => $width,
×
119
            ];
120
        }
UNCOV
121
    }
×
122

123
    public function __toString(): string
124
    {
125
        return sprintf('W(%s)', implode(',', $this->widths));
1✔
126
    }
127

128
    public function createSrcSet(ArgsFacade $args): SrcSet
129
    {
130
        $wAlias = $args->getModifierAlias(Width::class);
1✔
131
        $modifiers = $args->getDefaultModifiers() ?? [];
1✔
132

133
        if (null === $wAlias) {
1✔
134
            $link = empty($modifiers) ? '' : $args->createLink($modifiers);
1✔
135

136
            return new SrcSet(
1✔
137
                descriptor: 'w',
1✔
138
                links: '' !== $link ? [ 0 => $link ] : [],
1✔
139
                value: $link,
140
            );
141
        }
142

143
        $links = [];
1✔
144
        $parts = array_map(static function (int $w) use ($args, $wAlias, $modifiers, &$links) {
1✔
145
            $modifiers[$wAlias] = $w;
1✔
146
            $link = $args->createLink($modifiers);
1✔
147
            $links[$w] = $link;
1✔
148

149
            return sprintf(
1✔
150
                '%s %dw',
1✔
151
                $link,
152
                $w,
153
            );
154
        }, $this->widths);
1✔
155

156
        return new SrcSet(
1✔
157
            descriptor: 'w',
1✔
158
            links: $links,
159
            value: implode(', ', $parts),
1✔
160
        );
161
    }
162
}
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