From 5c61a7a60913df7d3add9ffbf86eeb903d5f33aa Mon Sep 17 00:00:00 2001 From: Wouter de Jong Date: Sun, 6 Apr 2025 13:14:12 +0200 Subject: [PATCH] Make behavior of PHP and GitHub references consistent --- src/Reference/MethodReference.php | 11 +++++------ src/Reference/PhpClassReference.php | 10 ++++++---- src/Reference/PhpMethodReference.php | 15 +++++++++++---- .../expected/blocks/references/php-class.html | 2 +- .../expected/blocks/references/php-method.html | 3 ++- tests/fixtures/expected/main/datetime.html | 2 +- .../source/blocks/references/php-class.rst | 2 +- .../source/blocks/references/php-method.rst | 2 ++ 8 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Reference/MethodReference.php b/src/Reference/MethodReference.php index 632aaa26..b068f334 100644 --- a/src/Reference/MethodReference.php +++ b/src/Reference/MethodReference.php @@ -30,20 +30,19 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - $className = explode('::', $data)[0]; - $className = str_replace('\\\\', '\\', $className); - - if (!u($data)->containsAny('::')) { + $data = u($data); + if (!$data->containsAny('::')) { throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName())); } - $methodName = explode('::', $data)[1]; + [$className, $methodName] = $data->split('::', 2); + $className = $className->replace('\\\\', '\\'); $scrollTextFragment = sprintf('#:~:text=%s', rawurlencode('function '.$methodName)); return new ResolvedReference( $environment->getCurrentFileName(), $methodName.'()', - sprintf('%s/%s.php%s', $this->symfonyRepositoryUrl, str_replace('\\', '/', $className), $scrollTextFragment), + sprintf('%s/%s.php%s', $this->symfonyRepositoryUrl, $className->replace('\\', '/'), $scrollTextFragment), [], [ 'title' => sprintf('%s::%s()', $className, $methodName), diff --git a/src/Reference/PhpClassReference.php b/src/Reference/PhpClassReference.php index 0bb0ba86..fbaabe44 100644 --- a/src/Reference/PhpClassReference.php +++ b/src/Reference/PhpClassReference.php @@ -12,6 +12,7 @@ use Doctrine\RST\Environment; use Doctrine\RST\References\Reference; use Doctrine\RST\References\ResolvedReference; +use function Symfony\Component\String\u; class PhpClassReference extends Reference { @@ -29,14 +30,15 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - $classnamePath = str_replace('\\', '-', strtolower($data)); + $className = u($data)->replace('\\\\', '\\'); + return new ResolvedReference( $environment->getCurrentFileName(), - $data, - sprintf('%s/class.%s.php', $this->phpDocUrl, $classnamePath), + $className->afterLast('\\'), + sprintf('%s/class.%s.php', $this->phpDocUrl, $className->replace('\\', '-')->lower()), [], [ - 'title' => $data, + 'title' => $className, ] ); } diff --git a/src/Reference/PhpMethodReference.php b/src/Reference/PhpMethodReference.php index ac537440..9a85a918 100644 --- a/src/Reference/PhpMethodReference.php +++ b/src/Reference/PhpMethodReference.php @@ -12,6 +12,7 @@ use Doctrine\RST\Environment; use Doctrine\RST\References\Reference; use Doctrine\RST\References\ResolvedReference; +use function Symfony\Component\String\u; class PhpMethodReference extends Reference { @@ -29,15 +30,21 @@ public function getName(): string public function resolve(Environment $environment, string $data): ResolvedReference { - [$class, $method] = explode('::', $data, 2); + $data = u($data); + if (!$data->containsAny('::')) { + throw new \RuntimeException(sprintf('Malformed method reference "%s" in file "%s"', $data, $environment->getCurrentFileName())); + } + + [$className, $methodName] = $data->split('::', 2); + $className = $className->replace('\\\\', '\\'); return new ResolvedReference( $environment->getCurrentFileName(), - $data.'()', - sprintf('%s/%s.%s.php', $this->phpDocUrl, strtolower($class), strtolower($method)), + $methodName.'()', + sprintf('%s/%s.%s.php', $this->phpDocUrl, $className->replace('\\', '-')->lower(), $methodName->lower()), [], [ - 'title' => $class, + 'title' => sprintf('%s::%s()', $className, $methodName), ] ); } diff --git a/tests/fixtures/expected/blocks/references/php-class.html b/tests/fixtures/expected/blocks/references/php-class.html index b1e018e1..a91b1fd2 100644 --- a/tests/fixtures/expected/blocks/references/php-class.html +++ b/tests/fixtures/expected/blocks/references/php-class.html @@ -1,2 +1,2 @@

ArrayAccess

-

BcMath\Number

+

Number

diff --git a/tests/fixtures/expected/blocks/references/php-method.html b/tests/fixtures/expected/blocks/references/php-method.html index e86c5717..59323ada 100644 --- a/tests/fixtures/expected/blocks/references/php-method.html +++ b/tests/fixtures/expected/blocks/references/php-method.html @@ -1 +1,2 @@ -

Locale::getDefault()

+

getDefault()

+

add()

diff --git a/tests/fixtures/expected/main/datetime.html b/tests/fixtures/expected/main/datetime.html index f36ddc33..d6892fa9 100644 --- a/tests/fixtures/expected/main/datetime.html +++ b/tests/fixtures/expected/main/datetime.html @@ -79,7 +79,7 @@

methods: doRequest(). Or a namespace: Constraints. Or a PHP function: parse_ini_file. -Or a PHP method! Locale::getDefault().

+Or a PHP method! getDefault().

diff --git a/tests/fixtures/source/blocks/references/php-class.rst b/tests/fixtures/source/blocks/references/php-class.rst index f0782b48..284a9018 100644 --- a/tests/fixtures/source/blocks/references/php-class.rst +++ b/tests/fixtures/source/blocks/references/php-class.rst @@ -1,4 +1,4 @@ :phpclass:`ArrayAccess` -:phpclass:`BcMath\Number` +:phpclass:`BcMath\\Number` diff --git a/tests/fixtures/source/blocks/references/php-method.rst b/tests/fixtures/source/blocks/references/php-method.rst index 11705cd5..26d782d5 100644 --- a/tests/fixtures/source/blocks/references/php-method.rst +++ b/tests/fixtures/source/blocks/references/php-method.rst @@ -1,2 +1,4 @@ :phpmethod:`Locale::getDefault` + +:phpmethod:`BcMath\\Number::add`