Skip to content

Commit 9981bb5

Browse files
committed
Bump dependency + improve attribute
- Use new squirrelphp/debug v2.x - Make arguments for StringFilter attribute clearer - Add PREG_UNMATCHED_AS_NULL to Regex::getMatches - Add additional string filter attribute tests
1 parent 846a7af commit 9981bb5

File tree

8 files changed

+86
-47
lines changed

8 files changed

+86
-47
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require": {
2121
"php": ">=8.0",
2222
"ext-mbstring": "*",
23-
"squirrelphp/debug": "^0.5"
23+
"squirrelphp/debug": "^2.0"
2424
},
2525
"require-dev": {
2626
"ext-intl": "*",

psalm-baseline.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.13.1@5cf660f63b548ccd4a56f62d916ee4d6028e01a3">
2+
<files psalm-version="v4.15.0@a1b5e489e6fcebe40cb804793d964e99fc347820">
3+
<file src="src/Attribute/StringFilter.php">
4+
<DocblockTypeContradiction occurrences="1">
5+
<code>\is_string($name)</code>
6+
</DocblockTypeContradiction>
7+
</file>
38
<file src="src/Attribute/StringFilterExtension.php">
49
<ArgumentTypeCoercion occurrences="1">
510
<code>$options['data_class']</code>
611
</ArgumentTypeCoercion>
712
</file>
13+
<file src="tests/StringFilterAttributeProcessorTest.php">
14+
<InvalidArgument occurrences="1">
15+
<code>''</code>
16+
</InvalidArgument>
17+
<InvalidScalarArgument occurrences="1">
18+
<code>[0]</code>
19+
</InvalidScalarArgument>
20+
</file>
821
</files>

src/Attribute/StringFilter.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,29 @@ class StringFilter
1010
{
1111
use InvalidValueExceptionTrait;
1212

13-
/**
14-
* @var string[] Filter names which should be executed
15-
*/
16-
public array $names = [];
17-
18-
public function __construct(mixed $names = null)
19-
{
20-
$arguments = \func_get_args();
21-
22-
if (\count($arguments) === 0) {
23-
throw $this->generateInvalidValueException('No arguments provided - either string or one array is mandatory');
13+
/** @var string[] Filter names which should be executed */
14+
private array $names;
15+
16+
/** @param non-empty-string|non-empty-string[] $names */
17+
public function __construct(
18+
string|array $names,
19+
string ...$moreNames,
20+
) {
21+
if (\is_string($names)) {
22+
$names = [$names];
2423
}
2524

26-
// This is the format by Doctrine annotation reader
27-
if (
28-
\count($arguments) === 1
29-
&& \is_array($arguments[0])
30-
) {
31-
$this->names = $arguments[0];
32-
} else {
33-
$this->names = $arguments;
25+
$this->names = \array_values($names);
26+
27+
if (\count($moreNames) > 0) {
28+
$this->names = \array_merge($this->names, \array_values($moreNames));
3429
}
3530

3631
foreach ($this->names as $key => $name) {
3732
if (!\is_string($name)) {
38-
throw $this->generateInvalidValueException('Non-string filter provided with index ' . $key . ': ' . Debug::sanitizeData($arguments));
33+
throw $this->generateInvalidValueException('Non-string filter provided, names: ' . Debug::sanitizeData($names) . ', moreNames: ' . Debug::sanitizeData($moreNames));
34+
} elseif (\strlen($name) === 0) {
35+
throw $this->generateInvalidValueException('Zero-length filter provided, names: ' . Debug::sanitizeData($names) . ', moreNames: ' . Debug::sanitizeData($moreNames));
3936
}
4037
}
4138
}

src/Common/InvalidValueExceptionTrait.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ trait InvalidValueExceptionTrait
1616
{
1717
private function generateInvalidValueException(string $message): \Throwable
1818
{
19-
return Debug::createException(InvalidValueException::class, [
20-
StringFilterInterface::class,
21-
StringFilterSelectInterface::class,
22-
RandomStringGeneratorInterface::class,
23-
RandomStringGeneratorSelectInterface::class,
24-
CondenseNumberInterface::class,
25-
StringFilterProcessor::class,
26-
StringFilterExtension::class,
27-
], $message);
19+
return Debug::createException(
20+
InvalidValueException::class,
21+
$message,
22+
ignoreClasses: [
23+
StringFilterInterface::class,
24+
StringFilterSelectInterface::class,
25+
RandomStringGeneratorInterface::class,
26+
RandomStringGeneratorSelectInterface::class,
27+
CondenseNumberInterface::class,
28+
StringFilterProcessor::class,
29+
StringFilterExtension::class,
30+
],
31+
);
2832
}
2933
}

src/Common/RegexExceptionTrait.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ private function generateRegexException(): \Throwable
2525
}
2626
}
2727

28-
return Debug::createException(RegexException::class, [
29-
StringFilterInterface::class,
30-
StringFilterSelectInterface::class,
31-
RandomStringGeneratorInterface::class,
32-
RandomStringGeneratorSelectInterface::class,
33-
CondenseNumberInterface::class,
34-
StringFilterProcessor::class,
35-
StringFilterExtension::class,
36-
], 'Regex error in ' . __CLASS__ . ': ' . ( $error ?? 'Unrecognized error code' ));
28+
return Debug::createException(
29+
RegexException::class,
30+
'Regex error in ' . __CLASS__ . ': ' . ( $error ?? 'Unrecognized error code' ),
31+
ignoreClasses: [
32+
StringFilterInterface::class,
33+
StringFilterSelectInterface::class,
34+
RandomStringGeneratorInterface::class,
35+
RandomStringGeneratorSelectInterface::class,
36+
CondenseNumberInterface::class,
37+
StringFilterProcessor::class,
38+
StringFilterExtension::class,
39+
],
40+
);
3741
}
3842
}

src/Regex.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public static function getMatches(
4040
int $flags = 0,
4141
int $offset = 0,
4242
): ?array {
43+
// Include unmatched subpatterns in $matches as null instead of empty string
44+
$flags = $flags | PREG_UNMATCHED_AS_NULL;
45+
4346
$result = @\preg_match_all($pattern, $subject, $matches, $flags, $offset);
4447

4548
if (!\is_int($result)) {
@@ -142,8 +145,12 @@ private static function generateRegexException(): \Throwable
142145
}
143146
}
144147

145-
return Debug::createException(RegexException::class, [
146-
self::class,
147-
], 'Regex error in ' . __CLASS__ . ': ' . ( $error ?? 'Unrecognized error code' ));
148+
return Debug::createException(
149+
RegexException::class,
150+
'Regex error in ' . __CLASS__ . ': ' . ( $error ?? 'Unrecognized error code' ),
151+
ignoreClasses: [
152+
self::class,
153+
],
154+
);
148155
}
149156
}

src/Url.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(string $url)
2727

2828
// For very bad URLs parse_url can return false
2929
if ($urlParts === false) {
30-
throw Debug::createException(UrlException::class, self::class, 'Invalid URL provided');
30+
throw Debug::createException(UrlException::class, 'Invalid URL provided', ignoreClasses: self::class);
3131
}
3232

3333
$this->urlParts = $urlParts;
@@ -125,7 +125,7 @@ public function getQueryString(): string
125125
public function setScheme(string $scheme): void
126126
{
127127
if ($scheme !== 'http' && $scheme !== 'https') {
128-
throw Debug::createException(UrlException::class, self::class, 'Invalid URL scheme');
128+
throw Debug::createException(UrlException::class, 'Invalid URL scheme', ignoreClasses: self::class);
129129
}
130130

131131
$this->urlParts['scheme'] = $scheme;

tests/StringFilterAttributeProcessorTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,31 @@ public function testInvalidProperty(): void
103103
$this->processor->process($testClass);
104104
}
105105

106+
public function testStringFilterWithArray(): void
107+
{
108+
$stringFilter = new StringFilter(['Trim', 'Lowercase', 'Dada']);
109+
110+
$this->assertEquals(['Trim', 'Lowercase', 'Dada'], $stringFilter->getNames());
111+
}
112+
106113
public function testStringFilterWithStrings(): void
107114
{
108115
$stringFilter = new StringFilter('Trim', 'Lowercase', 'Dada');
109116

110117
$this->assertEquals(['Trim', 'Lowercase', 'Dada'], $stringFilter->getNames());
111118
}
112119

113-
public function testInvalidStringFilter(): void
120+
public function testEmptyStringFilter(): void
121+
{
122+
$this->expectException(InvalidValueException::class);
123+
124+
new StringFilter('');
125+
}
126+
127+
public function testNonStringStringFilter(): void
114128
{
115129
$this->expectException(InvalidValueException::class);
116130

117-
new StringFilter();
131+
new StringFilter([0]);
118132
}
119133
}

0 commit comments

Comments
 (0)