[symfony] add AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector (#4341)

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Abdul Malik Ikhsan 2020-10-02 17:29:18 +07:00 committed by GitHub
parent 4103cbce07
commit 3ff3e37ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 211 additions and 72 deletions

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
use Rector\Symfony\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector::class);
};

View File

@ -123,7 +123,7 @@ final class NodeRepository
* To prevent circular reference
* @required
*/
public function autowireParsedNodesByType(NodeTypeResolver $nodeTypeResolver): void
public function autowireNodeRepository(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -41,7 +41,7 @@ final class ParsedClassConstFetchNodeCollector
* To prevent circular reference
* @required
*/
public function autowireParsedNodesByType(NodeTypeResolver $nodeTypeResolver): void
public function autowireParsedClassConstFetchNodeCollector(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -43,7 +43,7 @@ final class ParsedPropertyFetchNodeCollector
* To prevent circular reference
* @required
*/
public function autowireParsedNodesByType(NodeTypeResolver $nodeTypeResolver): void
public function autowireParsedPropertyFetchNodeCollector(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -19,7 +19,7 @@ final class ClassConstFetchNameResolver implements NodeNameResolverInterface
/**
* @required
*/
public function autowireClassNameResolver(NodeNameResolver $nodeNameResolver): void
public function autowireClassConstFetchNameResolver(NodeNameResolver $nodeNameResolver): void
{
$this->nodeNameResolver = $nodeNameResolver;
}

View File

@ -19,7 +19,7 @@ final class ClassConstNameResolver implements NodeNameResolverInterface
/**
* @required
*/
public function autowireClassNameResolver(NodeNameResolver $nodeNameResolver): void
public function autowireClassConstNameResolver(NodeNameResolver $nodeNameResolver): void
{
$this->nodeNameResolver = $nodeNameResolver;
}

View File

@ -19,7 +19,7 @@ final class ParamNameResolver implements NodeNameResolverInterface
/**
* @required
*/
public function autowireClassNameResolver(NodeNameResolver $nodeNameResolver): void
public function autowireParamNameResolver(NodeNameResolver $nodeNameResolver): void
{
$this->nodeNameResolver = $nodeNameResolver;
}

View File

@ -19,7 +19,7 @@ final class PropertyNameResolver implements NodeNameResolverInterface
/**
* @required
*/
public function autowireClassNameResolver(NodeNameResolver $nodeNameResolver): void
public function autowirePropertyNameResolver(NodeNameResolver $nodeNameResolver): void
{
$this->nodeNameResolver = $nodeNameResolver;
}

View File

@ -19,7 +19,7 @@ final class UseNameResolver implements NodeNameResolverInterface
/**
* @required
*/
public function autowireClassNameResolver(NodeNameResolver $nodeNameResolver): void
public function autowireUseNameResolver(NodeNameResolver $nodeNameResolver): void
{
$this->nodeNameResolver = $nodeNameResolver;
}

View File

@ -21,7 +21,7 @@ final class ArrayDimFetchTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowireArrayDimFetchTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -20,7 +20,7 @@ final class CastTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowireCastTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -20,7 +20,7 @@ final class ClassConstFetchTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowireClassConstFetchTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -24,7 +24,7 @@ final class ClassMethodOrClassConstTypeResolver implements NodeTypeResolverInter
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowireClassMethodOrClassConstTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -55,7 +55,7 @@ final class ParamTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowirePropertyTypeResolver(
public function autowireParamTypeResolver(
NodeTypeResolver $nodeTypeResolver,
StaticTypeMapper $staticTypeMapper
): void {

View File

@ -76,7 +76,7 @@ final class PropertyFetchTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowirePropertyFetchTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -34,7 +34,7 @@ final class StaticCallTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowirePropertyTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowireStaticCallTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -89,7 +89,7 @@ final class VariableTypeResolver implements NodeTypeResolverInterface
/**
* @required
*/
public function autowireNodeTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowireVariableTypeResolver(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -25,7 +25,7 @@ final class CallableTypeMapper implements TypeMapperInterface
/**
* @required
*/
public function autowireIterableTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
public function autowireCallableTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}

View File

@ -641,6 +641,11 @@ final class SetList
*/
public const SYMFONY_PHPUNIT = __DIR__ . '/../../../../config/set/symfony-phpunit.php';
/**
* @var string
*/
public const SYMFONY_AUTOWIRE = __DIR__ . '/../../../../config/set/symfony-autowire.php';
/**
* @var string
*/

View File

@ -32,7 +32,7 @@ final class ArrayTypeMapper implements PhpDocTypeMapperInterface
/**
* @required
*/
public function autowireGenericPhpDocTypeMapper(PhpDocTypeMapper $phpDocTypeMapper): void
public function autowireArrayTypeMapper(PhpDocTypeMapper $phpDocTypeMapper): void
{
$this->phpDocTypeMapper = $phpDocTypeMapper;
}

View File

@ -29,7 +29,7 @@ final class NullableTypeMapper implements PhpDocTypeMapperInterface
/**
* @required
*/
public function autowireGenericPhpDocTypeMapper(PhpDocTypeMapper $phpDocTypeMapper): void
public function autowireNullableTypeMapper(PhpDocTypeMapper $phpDocTypeMapper): void
{
$this->phpDocTypeMapper = $phpDocTypeMapper;
}

View File

@ -630,47 +630,6 @@ parameters:
- src/Testing/PHPUnit/Runnable/RunnableClassFinder.php # 29
- utils/node-documentation-generator/src/ValueObject/NodeInfo.php # 29
-
message: '#Method with @required need to be named autowire\+class name#'
paths:
- packages/node-collector/src/NodeCollector/NodeRepository.php # 126
- packages/node-collector/src/NodeCollector/ParsedClassConstFetchNodeCollector.php # 44
- packages/node-collector/src/NodeCollector/ParsedPropertyFetchNodeCollector.php # 46
- packages/node-name-resolver/src/NodeNameResolver/ClassConstFetchNameResolver.php # 22
- packages/node-name-resolver/src/NodeNameResolver/ClassConstNameResolver.php # 22
- packages/node-name-resolver/src/NodeNameResolver/ParamNameResolver.php # 22
- packages/node-name-resolver/src/NodeNameResolver/PropertyNameResolver.php # 22
- packages/node-name-resolver/src/NodeNameResolver/UseNameResolver.php # 22
- packages/node-type-resolver/src/NodeTypeResolver/ArrayDimFetchTypeResolver.php # 24
- packages/node-type-resolver/src/NodeTypeResolver/CastTypeResolver.php # 23
- packages/node-type-resolver/src/NodeTypeResolver/ClassConstFetchTypeResolver.php # 23
- packages/node-type-resolver/src/NodeTypeResolver/ClassMethodOrClassConstTypeResolver.php # 27
- packages/node-type-resolver/src/NodeTypeResolver/ParamTypeResolver.php # 58
- packages/node-type-resolver/src/NodeTypeResolver/PropertyFetchTypeResolver.php # 79
- packages/node-type-resolver/src/NodeTypeResolver/StaticCallTypeResolver.php # 37
- packages/node-type-resolver/src/NodeTypeResolver/VariableTypeResolver.php # 92
- packages/phpstan-static-type-mapper/src/TypeMapper/CallableTypeMapper.php # 28
- packages/static-type-mapper/src/PhpDocParser/ArrayTypeMapper.php # 35
- packages/static-type-mapper/src/PhpDocParser/NullableTypeMapper.php # 32
- rules/naming/src/Guard/PropertyConflictingNameGuard/MatchPropertyTypeConflictingNameGuard.php # 14
- rules/naming/src/Guard/PropertyConflictingNameGuard/UnderscoreCamelCaseConflictingNameGuard.php # 14
- rules/symfony/src/Rector/MethodCall/AbstractToConstructorInjectionRector.php # 37
- rules/type-declaration/src/TypeInferer/ParamTypeInferer/PHPUnitDataProviderParamTypeInferer.php # 51
- src/Console/Command/AbstractCommand.php # 26
- src/Rector/AbstractRector.php # 125
- src/Rector/AbstractRector/BetterStandardPrinterTrait.php # 37
- src/Rector/AbstractRector/BetterStandardPrinterTrait.php # 37
- src/Rector/AbstractRector/CallableNodeTraverserTrait.php # 24
- src/Rector/AbstractRector/CallableNodeTraverserTrait.php # 24
- src/Rector/AbstractRector/ComplexRemovalTrait.php # 59
- src/Rector/AbstractRector/ComplexRemovalTrait.php # 59
- src/Rector/AbstractRector/ConstFetchAnalyzerTrait.php # 24
- src/Rector/AbstractRector/ConstFetchAnalyzerTrait.php # 24
- src/Rector/AbstractRector/NodeTypeResolverTrait.php # 59
- src/Rector/AbstractRector/NodeTypeResolverTrait.php # 59
- src/Rector/AbstractRector/VisibilityTrait.php # 29
- src/Rector/AbstractRector/VisibilityTrait.php # 29
-
message: '#Do not use setter on a service#'
paths:

View File

@ -24,6 +24,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
SetList::DEFLUENT,
SetList::TYPE_DECLARATION,
SetList::PHPUNIT_CODE_QUALITY,
Setlist::SYMFONY_AUTOWIRE,
]);
$parameters->set(Option::PATHS, [

View File

@ -11,7 +11,7 @@ final class MatchPropertyTypeConflictingNameGuard extends AbstractPropertyConfli
/**
* @required
*/
public function autowireMatchPropertyTypePropertyConflictingNameGuard(
public function autowireMatchPropertyTypeConflictingNameGuard(
MatchPropertyTypeExpectedNameResolver $matchPropertyTypeExpectedNameResolver
): void {
$this->expectedNameResolver = $matchPropertyTypeExpectedNameResolver;

View File

@ -11,7 +11,7 @@ final class UnderscoreCamelCaseConflictingNameGuard extends AbstractPropertyConf
/**
* @required
*/
public function autowireUnderscoreCamelCasePropertyConflictingNameGuard(
public function autowireUnderscoreCamelCaseConflictingNameGuard(
UnderscoreCamelCaseExpectedNameResolver $underscoreCamelCaseExpectedNameResolver
): void {
$this->expectedNameResolver = $underscoreCamelCaseExpectedNameResolver;

View File

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Rector\Symfony\Rector\ClassMethod;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
/**
* @see \Rector\Symfony\Tests\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRectorTest
*/
final class AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector extends AbstractRector
{
/**
* @var string
* @see https://regex101.com/r/gn2P0C/1
*/
private const REQUIRED_DOCBLOCK_REGEX = '#\*\s+@required\n?#';
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Use autowire + class name suffix for method with @required annotation', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
/** @required */
public function foo()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
/** @required */
public function autowireSomeClass()
{
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$docComment = $node->getDocComment();
if ($docComment === null) {
return null;
}
if (! Strings::match($docComment->getText(), self::REQUIRED_DOCBLOCK_REGEX)) {
return null;
}
$class = $node->getAttribute('parent');
/** @var Identifier $name */
$name = $class->name;
$className = $name->toString();
$expectedMethodName = 'autowire' . $className;
if ((string) $node->name === $expectedMethodName) {
return null;
}
/** @var Identifier $method */
$method = $node->name;
$method->name = $expectedMethodName;
return $node;
}
}

View File

@ -34,7 +34,7 @@ abstract class AbstractToConstructorInjectionRector extends AbstractRector
/**
* @required
*/
public function autowireAbstractToConstructorInjectionRectorDependencies(
public function autowireAbstractToConstructorInjectionRector(
PropertyNaming $propertyNaming,
ServiceMapProvider $applicationServiceMapProvider
): void {

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\Symfony\Tests\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Symfony\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector;
use Symplify\SmartFileSystem\SmartFileInfo;
final class AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector::class;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Symfony\Tests\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector\Fixture;
final class SomeClass
{
/** @required */
public function foo()
{
}
}
?>
-----
<?php
namespace Rector\Symfony\Tests\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector\Fixture;
final class SomeClass
{
/** @required */
public function autowireSomeClass()
{
}
}
?>

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Symfony\Tests\Rector\ClassMethod\AutoWireWithClassNameSuffixForMethodWithRequiredAnnotationRector\Fixture;
final class SomeClass2
{
/** @required */
public function autowireSomeClass2()
{
}
}
?>

View File

@ -48,7 +48,7 @@ final class PHPUnitDataProviderParamTypeInferer implements ParamTypeInfererInter
* Prevents circular reference
* @required
*/
public function autowirePHPUnitDataProviderParamTypeResolver(NodeTypeResolver $nodeTypeResolver): void
public function autowirePHPUnitDataProviderParamTypeInferer(NodeTypeResolver $nodeTypeResolver): void
{
$this->nodeTypeResolver = $nodeTypeResolver;
}

View File

@ -23,7 +23,7 @@ abstract class AbstractCommand extends Command
/**
* @required
*/
public function autowireDescriptor(TextDescriptor $textDescriptor): void
public function autowireAbstractCommand(TextDescriptor $textDescriptor): void
{
$this->textDescriptor = $textDescriptor;
}

View File

@ -122,7 +122,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
/**
* @required
*/
public function autowireAbstractRectorDependencies(
public function autowireAbstractRector(
SymfonyStyle $symfonyStyle,
PhpVersionProvider $phpVersionProvider,
BuilderFactory $builderFactory,

View File

@ -34,7 +34,7 @@ trait BetterStandardPrinterTrait
/**
* @required
*/
public function autowireBetterStandardPrinter(
public function autowireBetterStandardPrinterTrait(
BetterStandardPrinter $betterStandardPrinter,
BetterNodeFinder $betterNodeFinder,
SmartFileSystem $smartFileSystem

View File

@ -21,7 +21,7 @@ trait CallableNodeTraverserTrait
/**
* @required
*/
public function setCallableNodeTraverser(CallableNodeTraverser $callableNodeTraverser): void
public function autowireCallableNodeTraverserTrait(CallableNodeTraverser $callableNodeTraverser): void
{
$this->callableNodeTraverser = $callableNodeTraverser;
}

View File

@ -56,7 +56,7 @@ trait ComplexRemovalTrait
/**
* @required
*/
public function autowireComplextRemovalTrait(
public function autowireComplexRemovalTrait(
PropertyManipulator $propertyManipulator,
ParsedNodeCollector $parsedNodeCollector,
LivingCodeManipulator $livingCodeManipulator,

View File

@ -21,7 +21,7 @@ trait ConstFetchAnalyzerTrait
/**
* @required
*/
public function setConstFetchAnalyzer(ConstFetchManipulator $constFetchManipulator): void
public function autowireConstFetchAnalyzerTrait(ConstFetchManipulator $constFetchManipulator): void
{
$this->constFetchManipulator = $constFetchManipulator;
}

View File

@ -56,7 +56,7 @@ trait NodeTypeResolverTrait
/**
* @required
*/
public function autowireTypeAnalyzerDependencies(
public function autowireNodeTypeResolverTrait(
NodeTypeResolver $nodeTypeResolver,
ArrayTypeAnalyzer $arrayTypeAnalyzer,
CountableTypeAnalyzer $countableTypeAnalyzer,

View File

@ -26,7 +26,7 @@ trait VisibilityTrait
/**
* @required
*/
public function autowireVisbilityTrait(VisibilityManipulator $visibilityManipulator): void
public function autowireVisibilityTrait(VisibilityManipulator $visibilityManipulator): void
{
$this->visibilityManipulator = $visibilityManipulator;
}