Skip to content

Commit 2320afc

Browse files
committed
check preg_replace for null results
1 parent 2332f66 commit 2320afc

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/Property/Selector.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,16 @@ public function setSelector(string $selector): void
184184
$hasAttribute = \strpos($selector, '[') !== false;
185185

186186
// Whitespace can't be adjusted within an attribute selector, as it would change its meaning
187-
/** @phpstan-ignore theCodingMachineSafe.function */
188-
$this->selector = !$hasAttribute ? \preg_replace('/\\s++/', ' ', $selector) : $selector;
187+
if ($hasAttribute) {
188+
$this->selector = $selector;
189+
} else {
190+
/** @phpstan-ignore theCodingMachineSafe.function */
191+
$normalized = \preg_replace('/\\s++/', ' ', $selector);
192+
if ($normalized === null) {
193+
throw new \RuntimeException('Unexpected error');
194+
}
195+
$this->selector = $normalized;
196+
}
189197
}
190198

191199
/**

src/Value/Size.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,22 @@ public function render(OutputFormat $outputFormat): string
202202
if ($matchResult === false) {
203203
throw new \RuntimeException('Unexpected error');
204204
}
205-
$size = $matchResult === 1
205+
if ($matchResult === 1) {
206206
/** @phpstan-ignore theCodingMachineSafe.function */
207-
? \preg_replace("/$decimalPoint?0+$/", '', \sprintf('%f', $this->size))
208-
: (string) $this->size;
207+
$size = \preg_replace("/$decimalPoint?0+$/", '', \sprintf('%f', $this->size));
208+
if ($size === null) {
209+
throw new \RuntimeException('Unexpected error');
210+
}
211+
} else {
212+
$size = (string) $this->size;
213+
}
209214

210215
/** @phpstan-ignore theCodingMachineSafe.function */
211-
return \preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size) . ($this->unit ?? '');
216+
$result = \preg_replace(["/$decimalPoint/", '/^(-?)0\\./'], ['.', '$1.'], $size);
217+
if ($result === null) {
218+
throw new \RuntimeException('Unexpected error');
219+
}
220+
return $result . ($this->unit ?? '');
212221
}
213222

214223
/**

0 commit comments

Comments
 (0)