Skip to content

Commit e27a5a7

Browse files
committed
bug symfony#49983 [DomCrawler] Avoid passing null to substr/strrpos methods (VincentLanglet)
This PR was merged into the 5.4 branch. Discussion ---------- [DomCrawler] Avoid passing null to substr/strrpos methods | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix symfony#49960 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> When calling `resolve('foo', 'http://localhost?bar=1')` two php deprecations were triggered. This is because `parse_url` returns null and then we use the result as a string. I made the code working the same for `resolve('foo', 'http://localhost?bar=1')` than for `resolve('/foo', 'http://localhost?bar=1')` (which was already tested and was not triggering the deprecation). Commits ------- a025ee1 Avoid call on null
2 parents a504ba4 + a025ee1 commit e27a5a7

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/Symfony/Component/DomCrawler/Tests/UriResolverTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ public static function provideResolverTests()
8181
['/foo', 'file:///bar/baz', 'file:///foo'],
8282
['foo', 'file:///', 'file:///foo'],
8383
['foo', 'file:///bar/baz', 'file:///bar/foo'],
84+
85+
['foo', 'http://localhost?bar=1', 'http://localhost/foo'],
86+
['foo', 'http://localhost#bar', 'http://localhost/foo'],
8487
];
8588
}
8689
}

src/Symfony/Component/DomCrawler/UriResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function resolve(string $uri, ?string $baseUri): string
7070
}
7171

7272
// relative path
73-
$path = parse_url(substr($baseUri, \strlen($baseUriCleaned)), \PHP_URL_PATH);
73+
$path = parse_url(substr($baseUri, \strlen($baseUriCleaned)), \PHP_URL_PATH) ?? '';
7474
$path = self::canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
7575

7676
return $baseUriCleaned.('' === $path || '/' !== $path[0] ? '/' : '').$path;

0 commit comments

Comments
 (0)