Skip to content

Commit 6aab2a7

Browse files
authored
[TASK] Remove thecodingmachine/safe dependency (part 4) (#1617)
1 parent 09baae0 commit 6aab2a7

8 files changed

Lines changed: 75 additions & 13 deletions

File tree

Build/phpstan/phpstan-baseline.neon

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ parameters:
4242
count: 1
4343
path: ../../src/Parsing/ParserState.php
4444

45+
-
46+
message: '#^Function preg_match is unsafe to use\. It can return FALSE instead of throwing an exception\. Please add ''use function Safe\\preg_match;'' at the beginning of the file to use the variant provided by the ''thecodingmachine/safe'' library\.$#'
47+
identifier: theCodingMachineSafe.function
48+
count: 1
49+
path: ../../src/Property/Declaration.php
50+
51+
-
52+
message: '#^Function preg_match is unsafe to use\. It can return FALSE instead of throwing an exception\. Please add ''use function Safe\\preg_match;'' at the beginning of the file to use the variant provided by the ''thecodingmachine/safe'' library\.$#'
53+
identifier: theCodingMachineSafe.function
54+
count: 1
55+
path: ../../src/Property/Selector.php
56+
57+
-
58+
message: '#^Function preg_match is unsafe to use\. It can return FALSE instead of throwing an exception\. Please add ''use function Safe\\preg_match;'' at the beginning of the file to use the variant provided by the ''thecodingmachine/safe'' library\.$#'
59+
identifier: theCodingMachineSafe.function
60+
count: 1
61+
path: ../../src/Property/Selector/CompoundSelector.php
62+
63+
-
64+
message: '#^Function preg_match_all is unsafe to use\. It can return FALSE instead of throwing an exception\. Please add ''use function Safe\\preg_match_all;'' at the beginning of the file to use the variant provided by the ''thecodingmachine/safe'' library\.$#'
65+
identifier: theCodingMachineSafe.function
66+
count: 2
67+
path: ../../src/Property/Selector/SpecificityCalculator.php
68+
4569
-
4670
message: '#^Parameter \#2 \$arguments of class Sabberworm\\CSS\\Value\\CSSFunction constructor expects array\<Sabberworm\\CSS\\Value\\Value\|string\>\|Sabberworm\\CSS\\Value\\RuleValueList, Sabberworm\\CSS\\Value\\Value\|string given\.$#'
4771
identifier: argument.type

src/Property/Declaration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
use Sabberworm\CSS\Value\RuleValueList;
1919
use Sabberworm\CSS\Value\Value;
2020

21-
use function Safe\preg_match;
22-
2321
/**
2422
* `Declaration`s just have a string key (the property name) and a 'Value'.
2523
*
@@ -105,7 +103,9 @@ public static function parse(ParserState $parserState, array $commentsBefore = [
105103
*/
106104
private static function getDelimitersForPropertyValue(string $propertyName): array
107105
{
108-
if (preg_match('/^font($|-)/', $propertyName) === 1) {
106+
$matchResult = \preg_match('/^font($|-)/', $propertyName);
107+
\assert(\is_int($matchResult));
108+
if ($matchResult === 1) {
109109
return [',', '/', ' '];
110110
}
111111

src/Property/Selector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Sabberworm\CSS\Settings;
1616
use Sabberworm\CSS\ShortClassNameProvider;
1717

18-
use function Safe\preg_match;
19-
2018
/**
2119
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this
2220
* class.
@@ -68,7 +66,10 @@ class Selector implements Renderable
6866
public static function isValid(string $selector): bool
6967
{
7068
// Note: We need to use `static::` here as the constant is overridden in the `KeyframeSelector` class.
71-
$numberOfMatches = preg_match(static::SELECTOR_VALIDATION_RX, $selector);
69+
$numberOfMatches = \preg_match(static::SELECTOR_VALIDATION_RX, $selector);
70+
if (!\is_int($numberOfMatches)) {
71+
throw new \RuntimeException('The selector is not valid UTF-8.', 1787284398);
72+
}
7273

7374
return $numberOfMatches === 1;
7475
}

src/Property/Selector/CompoundSelector.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
1111
use Sabberworm\CSS\ShortClassNameProvider;
1212

13-
use function Safe\preg_match;
14-
1513
/**
1614
* Class representing a CSS compound selector.
1715
* Selectors have to be split at combinators (space, `>`, `+`, `~`) before being passed to this class.
@@ -277,7 +275,10 @@ public function getArrayRepresentation(): array
277275

278276
private static function isValid(string $value): bool
279277
{
280-
$numberOfMatches = preg_match(self::SELECTOR_VALIDATION_RX, $value);
278+
$numberOfMatches = \preg_match(self::SELECTOR_VALIDATION_RX, $value);
279+
if (!\is_int($numberOfMatches)) {
280+
throw new \RuntimeException('The selector is not valid UTF-8.', 1787283476);
281+
}
281282

282283
return $numberOfMatches === 1;
283284
}

src/Property/Selector/SpecificityCalculator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Sabberworm\CSS\Property\Selector;
66

7-
use function Safe\preg_match_all;
8-
97
/**
108
* Utility class to calculate the specificity of a CSS selector.
119
*
@@ -65,8 +63,10 @@ public static function calculate(string $selector): int
6563
/// @todo should exclude \# as well as "#"
6664
$matches = null;
6765
$b = \substr_count($selector, '#');
68-
$c = preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $selector, $matches);
69-
$d = preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $selector, $matches);
66+
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $selector, $matches);
67+
\assert(\is_int($c));
68+
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $selector, $matches);
69+
\assert(\is_int($d));
7070
self::$cache[$selector] = ($a * 1000) + ($b * 100) + ($c * 10) + $d;
7171
}
7272

tests/Unit/Property/KeyframeSelectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public function getArrayRepresentationIncludesComponent(): void
3737

3838
self::assertSame('50%', $result['components'][0]['value']);
3939
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function isValidThrowsForSelectorThatIsNotValidUtf8(): void
45+
{
46+
$this->expectException(\RuntimeException::class);
47+
$this->expectExceptionMessage('The selector is not valid UTF-8.');
48+
$this->expectExceptionCode(1787284398);
49+
50+
KeyframeSelector::isValid("a\xFF");
51+
}
4052
}

tests/Unit/Property/Selector/CompoundSelectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,18 @@ public function constructorThrowsExceptionWithInvalidValue(string $value): void
371371
new CompoundSelector($value);
372372
}
373373

374+
/**
375+
* @test
376+
*/
377+
public function constructorThrowsForValueThatIsNotValidUtf8(): void
378+
{
379+
$this->expectException(\RuntimeException::class);
380+
$this->expectExceptionMessage('The selector is not valid UTF-8.');
381+
$this->expectExceptionCode(1787283476);
382+
383+
new CompoundSelector("a\xFF");
384+
}
385+
374386
/**
375387
* @test
376388
*

tests/Unit/Property/SelectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,18 @@ public function isValidForInvalidSelectorReturnsFalse(string $selector): void
523523
self::assertFalse(Selector::isValid($selector));
524524
}
525525

526+
/**
527+
* @test
528+
*/
529+
public function isValidThrowsForSelectorThatIsNotValidUtf8(): void
530+
{
531+
$this->expectException(\RuntimeException::class);
532+
$this->expectExceptionMessage('The selector is not valid UTF-8.');
533+
$this->expectExceptionCode(1787284398);
534+
535+
Selector::isValid("a\xFF");
536+
}
537+
526538
/**
527539
* @test
528540
*/

0 commit comments

Comments
 (0)