Merge pull request #3129 from rectorphp/fix-route

fix missing host at @Route annotation
This commit is contained in:
Tomas Votruba 2020-04-03 01:08:32 +02:00 committed by GitHub
commit 4af174e1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 1 deletions

View File

@ -9,6 +9,9 @@ use Rector\BetterPhpDocParser\Contract\PhpDocNode\ShortNameAwareTagInterface;
use Rector\BetterPhpDocParser\PhpDocNode\AbstractTagValueNode;
use Symfony\Component\Routing\Annotation\Route;
/**
* @see \Rector\BetterPhpDocParser\Tests\PhpDocParser\SymfonyRouteTagParser\SymfonyRouteClassMethodTest
*/
final class SymfonyRouteTagValueNode extends AbstractTagValueNode implements ShortNameAwareTagInterface
{
/**
@ -26,6 +29,11 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode implements Sho
*/
private $path;
/**
* @var string|null
*/
private $host;
/**
* @var bool
*/
@ -75,6 +83,7 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode implements Sho
array $methods = [],
array $options = [],
array $defaults = [],
?string $host = null,
array $requirements = [],
?string $originalContent = null
) {
@ -103,6 +112,7 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode implements Sho
$matches = Strings::match($originalContent, '#requirements={(.*?)(?<separator>(=|:))(.*)}#');
$this->requirementsKeyValueSeparator = $matches['separator'] ?? '=';
}
$this->host = $host;
}
public function __toString(): string
@ -127,6 +137,10 @@ final class SymfonyRouteTagValueNode extends AbstractTagValueNode implements Sho
$contentItems['defaults'] = $this->printArrayItem($this->defaults, 'defaults');
}
if ($this->host !== null) {
$contentItems['host'] = sprintf('host="%s"', $this->host);
}
if ($this->requirements !== []) {
$contentItems['requirements'] = $this->printArrayItemWithSeparator(
$this->requirements,

View File

@ -43,6 +43,7 @@ final class SymfonyRoutePhpDocNodeFactory extends AbstractPhpDocNodeFactory
$route->getMethods(),
$route->getOptions(),
$route->getDefaults(),
$route->getHost(),
$route->getRequirements(),
$annotationContent
);

View File

@ -0,0 +1,3 @@
/**
* @Route("/user", name="user_index", host="%test%", methods={"GET"})
*/

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\SymfonyRouteTagParser\Source;
use Symfony\Component\Routing\Annotation\Route;
final class RouteWithHost
{
/**
* @Route("/user", name="user_index", host="%test%", methods={"GET"})
*/
public function run()
{
}
}

View File

@ -11,7 +11,7 @@ use Rector\BetterPhpDocParser\Tests\PhpDocParser\OrmTagParser\AbstractPhpDocInfo
/**
* @see \Rector\BetterPhpDocParser\PhpDocNode\Symfony\SymfonyRouteTagValueNode
*/
final class ClassMethodTest extends AbstractPhpDocInfoTest
final class SymfonyRouteClassMethodTest extends AbstractPhpDocInfoTest
{
/**
* @dataProvider provideData()
@ -29,5 +29,7 @@ final class ClassMethodTest extends AbstractPhpDocInfoTest
public function provideData(): Iterator
{
yield [__DIR__ . '/Source/SomeClassMethod.php', __DIR__ . '/Fixture/expected_some_class_method.txt'];
yield [__DIR__ . '/Source/RouteWithHost.php', __DIR__ . '/Fixture/expected_route_with_host.txt'];
}
}