Skip to content

Commit fe43bff

Browse files
authored
[TASK] Remove thecodingmachine/safe dependency (part 6) (#1620)
1 parent 1c8d9df commit fe43bff

4 files changed

Lines changed: 31 additions & 12 deletions

File tree

Build/phpstan/phpstan-baseline.neon

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ parameters:
8484
count: 1
8585
path: ../../src/Value/CSSFunction.php
8686

87+
-
88+
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\.$#'
89+
identifier: theCodingMachineSafe.function
90+
count: 2
91+
path: ../../src/Value/CalcFunction.php
92+
8793
-
8894
message: '#^Parameter \#2 \$offset of method Sabberworm\\CSS\\Parsing\\ParserState\:\:peek\(\) expects int\<0, max\>, \-1 given\.$#'
8995
identifier: argument.type
@@ -102,6 +108,12 @@ parameters:
102108
count: 1
103109
path: ../../src/Value/Size.php
104110

111+
-
112+
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\.$#'
113+
identifier: theCodingMachineSafe.function
114+
count: 1
115+
path: ../../src/Value/Value.php
116+
105117
-
106118
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\.$#'
107119
identifier: argument.type

src/Value/CalcFunction.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
99
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
1010

11-
use function Safe\preg_match;
12-
1311
class CalcFunction extends CSSFunction
1412
{
1513
private const T_OPERAND = 1;
@@ -61,10 +59,11 @@ public static function parse(ParserState $parserState, bool $ignoreCase = false)
6159
} else {
6260
if (\in_array($parserState->peek(), $operators, true)) {
6361
if (($parserState->comes('-') || $parserState->comes('+'))) {
64-
if (
65-
preg_match('/\\s/', $parserState->peek(1, -1)) !== 1
66-
|| preg_match('/\\s/', $parserState->peek(1, 1)) !== 1
67-
) {
62+
$matchResultBefore = \preg_match('/\\s/', $parserState->peek(1, -1));
63+
\assert(\is_int($matchResultBefore));
64+
$matchResultAfter = \preg_match('/\\s/', $parserState->peek(1, 1));
65+
\assert(\is_int($matchResultAfter));
66+
if ($matchResultBefore !== 1 || $matchResultAfter !== 1) {
6867
throw new UnexpectedTokenException(
6968
" {$parserState->peek()} ",
7069
$parserState->peek(1, -1) . $parserState->peek(2),

src/Value/Value.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
use Sabberworm\CSS\Position\Positionable;
1414
use Sabberworm\CSS\ShortClassNameProvider;
1515

16-
use function Safe\preg_match;
17-
1816
/**
1917
* Abstract base class for specific classes of CSS values: `Size`, `Color`, `CSSString` and `URL`, and another
2018
* abstract subclass `ValueList`.
@@ -214,14 +212,20 @@ private static function parseUnicodeRangeValue(ParserState $parserState): string
214212
$codepointMaxLength = 6; // Code points outside BMP can use up to six digits
215213
$range = '';
216214
$parserState->consume('U+');
217-
do {
215+
while (true) {
218216
if ($parserState->comes('-')) {
219217
$codepointMaxLength = 13; // Max length is 2 six-digit code points + the dash(-) between them
220218
}
221219
$range .= $parserState->consume(1);
222-
} while (
223-
(\strlen($range) < $codepointMaxLength) && (preg_match('/[A-Fa-f0-9\\?-]/', $parserState->peek()) === 1)
224-
);
220+
if (\strlen($range) >= $codepointMaxLength) {
221+
break;
222+
}
223+
$matchResult = \preg_match('/[A-Fa-f0-9\\?-]/', $parserState->peek());
224+
\assert(\is_int($matchResult));
225+
if ($matchResult !== 1) {
226+
break;
227+
}
228+
}
225229

226230
return "U+{$range}";
227231
}

tests/Unit/Value/CalcFunctionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ public function provideInvalidSyntax(): array
159159
return [
160160
'missing space around -' => ['calc(100%-20px)'],
161161
'missing space around +' => ['calc(100%+20px)'],
162+
'missing space before -' => ['calc(100%- 20px)'],
163+
'missing space before +' => ['calc(100%+ 20px)'],
164+
'missing space after -' => ['calc(100% -20px)'],
165+
'missing space after +' => ['calc(100% +20px)'],
162166
'invalid operator' => ['calc(100% ^ 20px)'],
163167
];
164168
}

0 commit comments

Comments
 (0)