Skip to content

Commit 2332f66

Browse files
committed
Remove thecodingmachine/safe dependency
1 parent 9641004 commit 2332f66

15 files changed

Lines changed: 119 additions & 65 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Please also have a look at our
1616

1717
### Changed
1818

19+
- Remove `thecodingmachine/safe` dependency (#1484)
1920
- Clean up extra whitespace in CSS selector (#1398)
2021
- The array keys passed to `DeclarationBlock::setSelectors()` are no longer
2122
preserved (#1407)

bin/quickdump.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
use Sabberworm\CSS\Parser;
77

8-
use function Safe\file_get_contents;
9-
108
/**
119
* This script is used for generating the examples in the README.
1210
*/
1311

1412
require_once(__DIR__ . '/../vendor/autoload.php');
1513

16-
$source = file_get_contents('php://stdin');
14+
/** @phpstan-ignore theCodingMachineSafe.function */
15+
$source = \file_get_contents('php://stdin');
16+
if ($source === false) {
17+
throw new \RuntimeException('Unexpected error');
18+
}
1719
$parser = new Parser($source);
1820

1921
$document = $parser->parse();

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"homepage": "https://www.sabberworm.com/blog/2010/6/10/php-css-parser",
2525
"require": {
2626
"php": "^7.2.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
27-
"ext-iconv": "*",
28-
"thecodingmachine/safe": "^1.3 || ^2.5 || ^3.3"
27+
"ext-iconv": "*"
2928
},
3029
"require-dev": {
3130
"php-parallel-lint/php-parallel-lint": "1.4.0",

config/phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ parameters:
2626
-
2727
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) .* will always evaluate to#'
2828
path: '../tests/'
29+
-
30+
identifier: theCodingMachineSafe.function
31+
path: '../tests/'

src/CSSList/CSSList.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
use Sabberworm\CSS\Value\URL;
2626
use Sabberworm\CSS\Value\Value;
2727

28-
use function Safe\preg_match;
29-
3028
/**
3129
* This is the most generic container available. It can contain `DeclarationBlock`s (rule sets with a selector),
3230
* `RuleSet`s as well as other `CSSList` objects.
@@ -252,7 +250,12 @@ private static function identifierIs(string $identifier, string $match): bool
252250
return true;
253251
}
254252

255-
return preg_match("/^(-\\w+-)?$match$/i", $identifier) === 1;
253+
/** @phpstan-ignore theCodingMachineSafe.function */
254+
$matchResult = \preg_match("/^(-\\w+-)?$match$/i", $identifier);
255+
if ($matchResult === false) {
256+
throw new \RuntimeException('Unexpected error');
257+
}
258+
return $matchResult === 1;
256259
}
257260

258261
/**

src/Parsing/ParserState.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
use Sabberworm\CSS\Comment\Comment;
88
use Sabberworm\CSS\Settings;
99

10-
use function Safe\iconv;
11-
use function Safe\preg_match;
12-
use function Safe\preg_split;
13-
1410
/**
1511
* @internal since 8.7.0
1612
*/
@@ -121,7 +117,12 @@ public function parseIdentifier(bool $ignoreCase = true): string
121117
}
122118
$character = null;
123119
while (!$this->isEnd() && ($character = $this->parseCharacter(true)) !== null) {
124-
if (preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character) !== 0) {
120+
/** @phpstan-ignore theCodingMachineSafe.function */
121+
$matchResult = \preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character);
122+
if ($matchResult === false) {
123+
throw new \RuntimeException('Unexpected error');
124+
}
125+
if ($matchResult !== 0) {
125126
$result .= $character;
126127
} else {
127128
$result .= '\\' . $character;
@@ -145,13 +146,23 @@ public function parseCharacter(bool $isForIdentifier): ?string
145146
if ($this->comes('\\n') || $this->comes('\\r')) {
146147
return '';
147148
}
148-
if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
149+
/** @phpstan-ignore theCodingMachineSafe.function */
150+
$hexMatch = \preg_match('/[0-9a-fA-F]/Su', $this->peek());
151+
if ($hexMatch === false) {
152+
throw new \RuntimeException('Unexpected error');
153+
}
154+
if ($hexMatch === 0) {
149155
return $this->consume(1);
150156
}
151157
$hexCodePoint = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
152158
if ($this->strlen($hexCodePoint) < 6) {
153159
// Consume whitespace after incomplete unicode escape
154-
if (preg_match('/\\s/isSu', $this->peek()) !== 0) {
160+
/** @phpstan-ignore theCodingMachineSafe.function */
161+
$whitespaceMatch = \preg_match('/\\s/isSu', $this->peek());
162+
if ($whitespaceMatch === false) {
163+
throw new \RuntimeException('Unexpected error');
164+
}
165+
if ($whitespaceMatch !== 0) {
155166
if ($this->comes('\\r\\n')) {
156167
$this->consume(2);
157168
} else {
@@ -165,7 +176,8 @@ public function parseCharacter(bool $isForIdentifier): ?string
165176
$utf32EncodedCharacter .= \chr($codePoint & 0xff);
166177
$codePoint = $codePoint >> 8;
167178
}
168-
return iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
179+
/** @phpstan-ignore theCodingMachineSafe.function */
180+
return \iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
169181
}
170182
if ($isForIdentifier) {
171183
$peek = \ord($this->peek());
@@ -205,7 +217,15 @@ public function consumeWhiteSpace(array &$comments = []): string
205217
{
206218
$consumed = '';
207219
do {
208-
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
220+
while (true) {
221+
/** @phpstan-ignore theCodingMachineSafe.function */
222+
$whitespaceCheck = \preg_match('/\\s/isSu', $this->peek());
223+
if ($whitespaceCheck === false) {
224+
throw new \RuntimeException('Unexpected error');
225+
}
226+
if ($whitespaceCheck !== 1) {
227+
break;
228+
}
209229
$consumed .= $this->consume(1);
210230
}
211231
if ($this->parserSettings->usesLenientParsing()) {
@@ -319,7 +339,8 @@ public function consumeExpression(string $expression, ?int $maximumLength = null
319339
{
320340
$matches = null;
321341
$input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
322-
if (preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
342+
/** @phpstan-ignore theCodingMachineSafe.function */
343+
if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
323344
throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
324345
}
325346

@@ -468,7 +489,11 @@ private function strsplit(string $string): array
468489
{
469490
if ($this->parserSettings->hasMultibyteSupport()) {
470491
if ($this->streql($this->charset, 'utf-8')) {
471-
$result = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
492+
/** @phpstan-ignore theCodingMachineSafe.function */
493+
$result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
494+
if ($result === false) {
495+
throw new \RuntimeException('Unexpected error');
496+
}
472497
} else {
473498
$length = \mb_strlen($string, $this->charset);
474499
$result = [];

src/Property/Selector.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
use Sabberworm\CSS\Property\Selector\SpecificityCalculator;
1212
use Sabberworm\CSS\Renderable;
1313

14-
use function Safe\preg_match;
15-
use function Safe\preg_replace;
16-
1714
/**
1815
* Class representing a single CSS selector. Selectors have to be split by the comma prior to being passed into this
1916
* class.
@@ -61,7 +58,11 @@ class Selector implements Renderable
6158
public static function isValid(string $selector): bool
6259
{
6360
// Note: We need to use `static::` here as the constant is overridden in the `KeyframeSelector` class.
64-
$numberOfMatches = preg_match(static::SELECTOR_VALIDATION_RX, $selector);
61+
/** @phpstan-ignore theCodingMachineSafe.function */
62+
$numberOfMatches = \preg_match(static::SELECTOR_VALIDATION_RX, $selector);
63+
if ($numberOfMatches === false) {
64+
throw new \RuntimeException('Unexpected error');
65+
}
6566

6667
return $numberOfMatches === 1;
6768
}
@@ -183,7 +184,8 @@ public function setSelector(string $selector): void
183184
$hasAttribute = \strpos($selector, '[') !== false;
184185

185186
// Whitespace can't be adjusted within an attribute selector, as it would change its meaning
186-
$this->selector = !$hasAttribute ? preg_replace('/\\s++/', ' ', $selector) : $selector;
187+
/** @phpstan-ignore theCodingMachineSafe.function */
188+
$this->selector = !$hasAttribute ? \preg_replace('/\\s++/', ' ', $selector) : $selector;
187189
}
188190

189191
/**

src/Property/Selector/SpecificityCalculator.php

Lines changed: 10 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,16 @@ 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+
/** @phpstan-ignore theCodingMachineSafe.function */
67+
$c = \preg_match_all(self::NON_ID_ATTRIBUTES_AND_PSEUDO_CLASSES_RX, $selector, $matches);
68+
if ($c === false) {
69+
throw new \RuntimeException('Unexpected error');
70+
}
71+
/** @phpstan-ignore theCodingMachineSafe.function */
72+
$d = \preg_match_all(self::ELEMENTS_AND_PSEUDO_ELEMENTS_RX, $selector, $matches);
73+
if ($d === false) {
74+
throw new \RuntimeException('Unexpected error');
75+
}
7076
self::$cache[$selector] = ($a * 1000) + ($b * 100) + ($c * 10) + $d;
7177
}
7278

src/Rule/Rule.php

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

20-
use function Safe\preg_match;
21-
2220
/**
2321
* `Rule`s just have a string key (the rule) and a 'Value'.
2422
*
@@ -103,7 +101,12 @@ public static function parse(ParserState $parserState, array $commentsBeforeRule
103101
*/
104102
private static function listDelimiterForRule(string $rule): array
105103
{
106-
if (preg_match('/^font($|-)/', $rule) === 1) {
104+
/** @phpstan-ignore theCodingMachineSafe.function */
105+
$matchResult = \preg_match('/^font($|-)/', $rule);
106+
if ($matchResult === false) {
107+
throw new \RuntimeException('Unexpected error');
108+
}
109+
if ($matchResult === 1) {
107110
return [',', '/', ' '];
108111
}
109112

src/Value/CSSString.php

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

14-
use function Safe\preg_match;
15-
1614
/**
1715
* This class is a wrapper for quoted strings to distinguish them from keywords.
1816
*
@@ -59,7 +57,15 @@ public static function parse(ParserState $parserState): CSSString
5957
$content = null;
6058
if ($quote === null) {
6159
// Unquoted strings end in whitespace or with braces, brackets, parentheses
62-
while (preg_match('/[\\s{}()<>\\[\\]]/isu', $parserState->peek()) === 0) {
60+
while (true) {
61+
/** @phpstan-ignore theCodingMachineSafe.function */
62+
$matchResult = \preg_match('/[\\s{}()<>\\[\\]]/isu', $parserState->peek());
63+
if ($matchResult === false) {
64+
throw new \RuntimeException('Unexpected error');
65+
}
66+
if ($matchResult !== 0) {
67+
break;
68+
}
6369
$result .= $parserState->parseCharacter(false);
6470
}
6571
} else {

0 commit comments

Comments
 (0)