Skip to content

Commit 9f83efb

Browse files
authored
Remove array shape return in TemplateAnnotationToThisRenderRector as no longer used (#755)
* add fixture * Remove array shape return in TemplateAnnotationToThisRenderRector as no longer used
1 parent fdcb6e2 commit 9f83efb

9 files changed

Lines changed: 83 additions & 12 deletions

File tree

config/sets/symfony/symfony6/symfony61/symfony61-console.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@
77
use Rector\Symfony\Symfony61\Rector\Class_\CommandPropertyToAttributeRector;
88

99
return static function (RectorConfig $rectorConfig): void {
10-
$rectorConfig->rules([
11-
CommandConfigureToAttributeRector::class,
12-
CommandPropertyToAttributeRector::class,
13-
]);
10+
$rectorConfig->rules([CommandConfigureToAttributeRector::class, CommandPropertyToAttributeRector::class]);
1411
};

config/sets/symfony/symfony6/symfony62/symfony62-security-http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\Symfony\Symfony62\Rector\Class_\SecurityAttributeToIsGrantedAttributeRector;
76
use Rector\Renaming\Rector\Name\RenameClassRector;
7+
use Rector\Symfony\Symfony62\Rector\Class_\SecurityAttributeToIsGrantedAttributeRector;
88

99
return static function (RectorConfig $rectorConfig): void {
1010
$rectorConfig->rule(SecurityAttributeToIsGrantedAttributeRector::class);

config/sets/symfony/symfony6/symfony63/symfony63-dependency-injection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\Symfony\Symfony63\Rector\Class_\ParamAndEnvAttributeRector;
76
use Rector\Renaming\Rector\Name\RenameClassRector;
7+
use Rector\Symfony\Symfony63\Rector\Class_\ParamAndEnvAttributeRector;
88

99
return static function (RectorConfig $rectorConfig): void {
1010
$rectorConfig->rules([

config/sets/symfony/symfony7/symfony70/symfony70-serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Rector\Renaming\Rector\Name\RenameClassRector;
77

88
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
9+
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
1010
// @see https://github.com/symfony/symfony/blob/7.0/UPGRADE-7.0.md#frameworkbundle
1111
'Symfony\Component\Serializer\Normalizer\ObjectNormalizer' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface',
1212
'Symfony\Component\Serializer\Normalizer\PropertyNormalizer' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface',

config/sets/symfony/symfony7/symfony71/symfony71-dependency-injection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
'Symfony\Component\DependencyInjection\Attribute\AutowireLocator'
1919
),
2020
]);
21-
};
21+
};

config/sets/symfony/symfony7/symfony73/symfony73-console.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@
77
use Rector\Symfony\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector;
88

99
return static function (RectorConfig $rectorConfig): void {
10-
$rectorConfig->rules([
11-
CommandHelpToAttributeRector::class, InvokableCommandInputAttributeRector::class
12-
]);
10+
$rectorConfig->rules([CommandHelpToAttributeRector::class, InvokableCommandInputAttributeRector::class]);
1311
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace AppBundle\Controller\Attributes;
4+
5+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7+
8+
final class CleanupArrayReturnShape extends AbstractController
9+
{
10+
/**
11+
* @return array{hello: string}
12+
*/
13+
#[Template("with_some_template.twig")]
14+
public function indexAction()
15+
{
16+
return [
17+
'hello' => 'world'
18+
];
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace AppBundle\Controller\Attributes;
27+
28+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
29+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
30+
31+
final class CleanupArrayReturnShape extends AbstractController
32+
{
33+
public function indexAction(): \Symfony\Component\HttpFoundation\Response
34+
{
35+
return $this->render('with_some_template.twig', [
36+
'hello' => 'world'
37+
]);
38+
}
39+
}
40+
41+
?>

rules/CodeQuality/Rector/ClassMethod/TemplateAnnotationToThisRenderRector.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
use PhpParser\Node\Stmt\Function_;
1717
use PhpParser\Node\Stmt\Return_;
1818
use PhpParser\NodeVisitor;
19+
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
20+
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
1921
use PHPStan\Type\ArrayType;
2022
use PHPStan\Type\Constant\ConstantArrayType;
2123
use PHPStan\Type\MixedType;
2224
use PHPStan\Type\ObjectType;
2325
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
26+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
2427
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2528
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
2629
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
@@ -176,7 +179,7 @@ private function replaceTemplateAnnotation(
176179
return $this->refactorClassMethod($classMethod, $doctrineAnnotationTagValueNode ?: $templateAttribute);
177180
}
178181

179-
// global @Template access
182+
// global @Template/#[Template] access
180183
if ($classTagValueNodeOrAttribute instanceof DoctrineAnnotationTagValueNode || $classTagValueNodeOrAttribute instanceof Attribute) {
181184
return $this->refactorClassMethod($classMethod, $classTagValueNodeOrAttribute);
182185
}
@@ -221,6 +224,9 @@ private function refactorClassMethod(
221224
return null;
222225
});
223226

227+
// remove return array shape details
228+
$this->removeReturnArrayShapeDocblock($classMethod);
229+
224230
if (! $this->emptyReturnNodeFinder->hasNoOrEmptyReturns($classMethod)) {
225231
return $hasChanged;
226232
}
@@ -393,4 +399,25 @@ private function refactorStmtsAwareNode(
393399
}
394400
return $hasChanged;
395401
}
402+
403+
private function removeReturnArrayShapeDocblock(ClassMethod $classMethod): void
404+
{
405+
$classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
406+
if (! $classMethodPhpDocInfo instanceof PhpDocInfo) {
407+
return;
408+
}
409+
410+
$returnTagValueNode = $classMethodPhpDocInfo->getReturnTagValue();
411+
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
412+
return;
413+
}
414+
415+
if (! $returnTagValueNode->type instanceof ArrayShapeNode) {
416+
return;
417+
}
418+
419+
if ($classMethodPhpDocInfo->removeByName('@return')) {
420+
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
421+
}
422+
}
396423
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Sensio\Bundle\FrameworkExtraBundle\Configuration;
4+
5+
#[\Attribute]
6+
class Template
7+
{
8+
}

0 commit comments

Comments
 (0)