• 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

50.94
/src/Responsive/Descriptor/XDescriptor.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\PixelDensity;
10
use SixtyEightPublishers\ImageStorage\Responsive\SrcSet;
11
use function array_map;
12
use function array_unshift;
13
use function implode;
14
use function in_array;
15
use function number_format;
16
use function sprintf;
17

18
final class XDescriptor implements DescriptorInterface
19
{
20
    /** @var array<float> */
21
    private array $pixelDensities;
22

23
    /**
24
     * @param int|float|string ...$pixelDensities
25
     */
26
    public function __construct(...$pixelDensities)
27
    {
28
        $pixelDensities = array_map('floatval', $pixelDensities);
1✔
29

30
        if (!in_array(1.0, $pixelDensities, true)) {
1✔
31
            array_unshift($pixelDensities, 1.0);
1✔
32
        }
33

34
        $this->pixelDensities = $pixelDensities;
1✔
35
    }
1✔
36

37
    public static function default(): self
38
    {
39
        return new self(1, 2, 3);
1✔
40
    }
41

42
    public function validateModifierValue(
43
        mixed $value,
44
        mixed $default,
45
    ): float {
UNCOV
46
        if (true === $value) {
×
UNCOV
47
            if (!is_numeric($default) && [] !== $this->pixelDensities) {
×
UNCOV
48
                return $this->pixelDensities[0];
×
49
            }
50

UNCOV
51
            $value = $default;
×
52
        }
53

UNCOV
54
        if (is_numeric($value) && in_array((float) $value, $this->pixelDensities, true)) {
×
UNCOV
55
            return (float) $value;
×
56
        }
57

UNCOV
58
        throw new InvalidArgumentException(
×
UNCOV
59
            message: sprintf(
×
UNCOV
60
                'Invalid preset value "%s" passed for descriptor %s',
×
UNCOV
61
                var_export($value, true),
×
62
                $this,
63
            ),
64
        );
65
    }
66

67
    public function expandModifier(
68
        ModifierCollectionInterface $modifierCollection,
69
        mixed $value,
70
    ): array {
UNCOV
71
        $pdAlias = $modifierCollection
×
UNCOV
72
            ->getByName(PixelDensity::class)
×
UNCOV
73
            ->getAlias();
×
74

UNCOV
75
        if (is_numeric($value) && in_array((float) $value, $this->pixelDensities, true)) {
×
76
            return [
UNCOV
77
                $pdAlias => (float) $value,
×
78
            ];
79
        }
80

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

UNCOV
90
    public function iterateModifiers(ModifierCollectionInterface $modifierCollection): iterable
×
91
    {
UNCOV
92
        $pdAlias = $modifierCollection
×
UNCOV
93
            ->getByName(PixelDensity::class)
×
UNCOV
94
            ->getAlias();
×
95

UNCOV
96
        foreach ($this->pixelDensities as $pixelDensity) {
×
97
            yield [
UNCOV
98
                $pdAlias => $pixelDensity,
×
99
            ];
100
        }
UNCOV
101
    }
×
102

103
    public function __toString(): string
104
    {
105
        return sprintf('X(%s)', implode(',', $this->pixelDensities));
1✔
106
    }
107

108
    public function createSrcSet(ArgsFacade $args): SrcSet
109
    {
110
        $pdAlias = $args->getModifierAlias(PixelDensity::class);
1✔
111
        $modifiers = $args->getDefaultModifiers() ?? [];
1✔
112

113
        if (null === $pdAlias) {
1✔
114
            $link = empty($modifiers) ? '' : $args->createLink($modifiers);
1✔
115

116
            return new SrcSet(
1✔
117
                descriptor: 'x',
1✔
118
                links: '' !== $link ? [ '1.0' => $link ] : [],
1✔
119
                value: $link,
120
            );
121
        }
122

123
        $links = [];
1✔
124
        $parts = array_map(static function (float $pd) use ($args, $pdAlias, $modifiers, &$links) {
1✔
125
            $modifiers[$pdAlias] = $pd;
1✔
126
            $link = $args->createLink($modifiers);
1✔
127
            $formattedPd = number_format($pd, 1, '.', '');
1✔
128
            $links[$formattedPd] = $link;
1✔
129

130
            return sprintf(
1✔
131
                '%s%s',
1✔
132
                $link,
133
                1.0 === $pd ? '' : (' ' . $formattedPd . 'x'),
1✔
134
            );
135
        }, $this->pixelDensities);
1✔
136

137
        return new SrcSet(
1✔
138
            descriptor: 'x',
1✔
139
            links: $links,
140
            value: implode(', ', $parts),
1✔
141
        );
142
    }
143
}
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