From 762ca838446583745adadb11e9c3cbfff3454842 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 22 Dec 2023 09:53:19 +0900 Subject: [PATCH 1/5] test: fix @param types --- tests/system/Test/DOMParserTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index f6e33cc32250..07f3961bc640 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 { From 41c1e6318929b0f8f8275b9dabaafdc93451c3a3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 22 Dec 2023 09:53:52 +0900 Subject: [PATCH 2/5] test: add test for `id="0"` --- tests/system/Test/DOMParserTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/system/Test/DOMParserTest.php b/tests/system/Test/DOMParserTest.php index 07f3961bc640..6df1d95c7663 100644 --- a/tests/system/Test/DOMParserTest.php +++ b/tests/system/Test/DOMParserTest.php @@ -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(); From b611d38267033a771c7b48d5eaef494ed49572b2 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 22 Dec 2023 10:04:00 +0900 Subject: [PATCH 3/5] docs: make @return more strict --- system/Test/DOMParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index a14183eeea92..6f9156ead89c 100644 --- a/system/Test/DOMParser.php +++ b/system/Test/DOMParser.php @@ -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) { From f4c17bbac17c4a365b6c77a85bd7cd162f055f97 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 22 Dec 2023 10:07:10 +0900 Subject: [PATCH 4/5] fix: DOMParser cannot see element with `id="0"` --- system/Test/DOMParser.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/system/Test/DOMParser.php b/system/Test/DOMParser.php index 6f9156ead89c..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}\"]"; } From c03d34a14737ed5a085fdd93dd339184313a122b Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 22 Dec 2023 10:09:40 +0900 Subject: [PATCH 5/5] chore: remove ignoreErrors --- phpstan-baseline.php | 5 ----- 1 file changed, 5 deletions(-) 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,