diff --git a/phpstan-baseline.php b/phpstan-baseline.php index d4877780cf88..65a22b8f584a 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -3171,11 +3171,6 @@ 'count' => 1, 'path' => __DIR__ . '/system/Test/DOMParser.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#', - 'count' => 6, - 'path' => __DIR__ . '/system/Test/DOMParser.php', -]; $ignoreErrors[] = [ 'message' => '#^Access to an undefined property object\\:\\:\\$createdField\\.$#', 'count' => 1, diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index a14183eeea92..1565452159f9 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -189,23 +189,23 @@ protected function doXPath(?string $search, string $element, array $paths = []) $path = ''; // By ID - if (! empty($selector['id'])) { - $path = empty($selector['tag']) + if (isset($selector['id'])) { + $path = ($selector['tag'] === '') ? "id(\"{$selector['id']}\")" : "//{$selector['tag']}[@id=\"{$selector['id']}\"]"; } // By Class - elseif (! empty($selector['class'])) { - $path = empty($selector['tag']) + elseif (isset($selector['class'])) { + $path = ($selector['tag'] === '') ? "//*[@class=\"{$selector['class']}\"]" : "//{$selector['tag']}[@class=\"{$selector['class']}\"]"; } // By tag only - elseif (! empty($selector['tag'])) { + elseif ($selector['tag'] !== '') { $path = "//{$selector['tag']}"; } - if (! empty($selector['attr'])) { + if (isset($selector['attr'])) { foreach ($selector['attr'] as $key => $value) { $path .= "[@{$key}=\"{$value}\"]"; } @@ -231,7 +231,7 @@ protected function doXPath(?string $search, string $element, array $paths = []) /** * Look for the a selector in the passed text. * - * @return array + * @return array{tag: string, id: string|null, class: string|null, attr: array|null} */ public function parseSelector(string $selector) { diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index f6e33cc32250..6df1d95c7663 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -91,7 +91,7 @@ public static function provideText(): iterable /** * @dataProvider provideText * - * @param mixed $text + * @param string $text */ public function testSeeText($text): void { @@ -139,7 +139,7 @@ public function testSeeFail(): void /** * @dataProvider provideText * - * @param mixed $text + * @param string $text */ public function testSeeElement($text): void { @@ -171,6 +171,16 @@ public function testSeeElementID(): void $this->assertTrue($dom->see('Hello World', '#heading')); } + public function testSeeElementIDZero(): void + { + $dom = new DOMParser(); + + $html = '

Hello World Wide Web

'; + $dom->withString($html); + + $this->assertTrue($dom->see('Hello World', '#0')); + } + public function testSeeElementIDFails(): void { $dom = new DOMParser();