mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-29 14:13:30 +00:00
replace annotation type
This commit is contained in:
parent
6200b970e3
commit
5fe6684891
|
@ -16,6 +16,10 @@ use PhpParser\Node\Scalar\String_;
|
|||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
||||
use PHPStan\Type\ArrayType;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocNode\Sensio\SensioTemplateTagValueNode;
|
||||
use Rector\Core\PhpParser\Node\Manipulator\FuncCallManipulator;
|
||||
|
@ -177,10 +181,13 @@ PHP
|
|||
|
||||
private function updateReturnType(ClassMethod $classMethod): void
|
||||
{
|
||||
$this->updateReturnPhpDoc($classMethod);
|
||||
|
||||
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// change return type
|
||||
if ($classMethod->returnType !== null) {
|
||||
$returnTypeName = $this->getName($classMethod->returnType);
|
||||
if ($returnTypeName !== null && is_a($returnTypeName, Response::class, true)) {
|
||||
|
@ -310,4 +317,27 @@ PHP
|
|||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
private function updateReturnPhpDoc(ClassMethod $classMethod): void
|
||||
{
|
||||
/** @var PhpDocInfo|null $phpDocInfo */
|
||||
$phpDocInfo = $classMethod->getAttribute(AttributeKey::PHP_DOC_INFO);
|
||||
if ($phpDocInfo === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$returnTagValueNode = $phpDocInfo->getByType(ReturnTagValueNode::class);
|
||||
if ($returnTagValueNode === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$returnStaticType = $this->staticTypeMapper->mapPHPStanPhpDocTypeNodeToPHPStanType(
|
||||
$returnTagValueNode->type,
|
||||
$classMethod
|
||||
);
|
||||
|
||||
if ($returnStaticType instanceof ArrayType || $returnStaticType instanceof UnionType) {
|
||||
$returnTagValueNode->type = new IdentifierTypeNode('\Symfony\Component\HttpFoundation\Response');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\FixturePhp56;
|
||||
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
class ClassReturnTypeController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Template("AppBundle:Module:index.html.twig")
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\FixturePhp56;
|
||||
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
class ClassReturnTypeController extends AbstractController
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function indexAction()
|
||||
{
|
||||
return $this->render('AppBundle:Module:index.html.twig');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\Fixture;
|
||||
namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector\FixtureVersion3;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
|
|
|
@ -56,7 +56,8 @@ use Symfony\Component\HttpFoundation\Response;
|
|||
class ClassWithReturnTypes extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @return Response
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function indexAction(): \Symfony\Component\HttpFoundation\Response
|
||||
{
|
||||
|
@ -64,7 +65,8 @@ class ClassWithReturnTypes extends AbstractController
|
|||
}
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
public function index2Action(): \Symfony\Component\HttpFoundation\Response
|
||||
{
|
||||
|
@ -76,6 +78,7 @@ class ClassWithReturnTypes extends AbstractController
|
|||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function index3Action(): Response
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Sensio\Tests\Rector\FrameworkExtraBundle\TemplateAnnotationRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Sensio\Rector\FrameworkExtraBundle\TemplateAnnotationRector;
|
||||
|
||||
final class TemplateAnnotationRectorPhp56Test extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp56');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
protected function getRectorsWithConfiguration(): array
|
||||
{
|
||||
return [
|
||||
TemplateAnnotationRector::class => [
|
||||
'$version' => 5,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function getPhpVersion(): string
|
||||
{
|
||||
return '5.6';
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user