Skip to content

Commit

Permalink
[TypeDeclaration] Add AddParamTypeFromCallersRector (#5782)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Mar 6, 2021
1 parent 956cac7 commit 031deda
Show file tree
Hide file tree
Showing 55 changed files with 680 additions and 181 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"doctrine/annotations": "^1.11",
"doctrine/inflector": "^2.0",
"jean85/pretty-package-versions": "^1.5.1|^2.0.1",
"nette/robot-loader": "^3.2",
"nette/robot-loader": "^3.2 <=3.3.1",
"nette/utils": "^3.2",
"nikic/php-parser": "^4.10.4",
"phpstan/phpdoc-parser": "^0.4.9",
Expand Down
4 changes: 3 additions & 1 deletion config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
Expand Down Expand Up @@ -56,7 +57,7 @@
$services->alias(SymfonyApplication::class, ConsoleApplication::class);

$services->set(NoRectorsLoadedReporter::class);
$services->set(\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser::class);
$services->set(SimpleCallableNodeTraverser::class);

$services->set(TextDescriptor::class);

Expand All @@ -77,6 +78,7 @@
$services->set(PrivatesCaller::class);
$services->set(FinderSanitizer::class);
$services->set(FileSystemFilter::class);

$services->set(ParameterProvider::class)
->arg('$container', service('service_container'));

Expand Down
25 changes: 10 additions & 15 deletions config/set/type-declaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

declare(strict_types=1);

use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
return static function (
\Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator $containerConfigurator
): void {
$services = $containerConfigurator->services();
$services->set(ParamTypeDeclarationRector::class);
$services->set(ReturnTypeDeclarationRector::class);
$services->set(PropertyTypeDeclarationRector::class);
$services->set(AddClosureReturnTypeRector::class);
$services->set(AddArrayParamDocTypeRector::class);
$services->set(AddArrayReturnDocTypeRector::class);
$services->set(\Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector::class);
$services->set(\Rector\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector::class);
$services->set(\Rector\TypeDeclaration\Rector\Property\PropertyTypeDeclarationRector::class);
$services->set(\Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector::class);
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\AddArrayParamDocTypeRector::class);
$services->set(\Rector\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector::class);
// $services->set(\Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromCallersRector::class);
};
2 changes: 0 additions & 2 deletions packages/node-collector/src/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
use ReflectionMethod;

/**
* @rector-doc
* This service contains all the parsed nodes. E.g. all the functions, method call, classes, static calls etc.
* It's useful in case of context analysis, e.g. find all the usage of class method to detect, if the method is used.
*/
Expand Down Expand Up @@ -377,7 +376,6 @@ public function findCallsByClassMethod(ClassMethod $classMethod): array

/** @var string $method */
$method = $classMethod->getAttribute(AttributeKey::METHOD_NAME);

return $this->findCallsByClassAndMethod($class, $method);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -27,6 +28,7 @@ final class UnionTypeCommonTypeNarrower
* @var array<class-string<Node|\PHPStan\PhpDocParser\Ast\Node>, array<class-string<Node|\PHPStan\PhpDocParser\Ast\Node>>>
*/
private const PRIORITY_TYPES = [
FunctionLike::class => [FunctionLike::class],
BinaryOp::class => [BinaryOp::class, Expr::class],
Expr::class => [Node::class, Expr::class],
Stmt::class => [Node::class, Stmt::class],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public function autowireArrayTypeMapper(
$this->reflectionProvider = $reflectionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ArrayType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return BooleanType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function autowireCallableTypeMapper(PHPStanStaticTypeMapper $phpStanStati
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return CallableType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ final class ClassStringTypeMapper implements TypeMapperInterface, PHPStanStaticT
*/
private $phpStanStaticTypeMapper;

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ClassStringType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public function __construct(CallableTypeMapper $callableTypeMapper)
$this->callableTypeMapper = $callableTypeMapper;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ClosureType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return FloatType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

final class HasOffsetTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return HasOffsetType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return IntegerType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function autowireIntersectionTypeMapper(PHPStanStaticTypeMapper $phpStanS
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return IntersectionType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function autowireIterableTypeMapper(PHPStanStaticTypeMapper $phpStanStati
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return IterableType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

final class MixedTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return MixedType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

final class NeverTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return NeverType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

final class NonEmptyArrayTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return NonEmptyArrayType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

final class NullTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return NullType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function __construct(ReflectionProvider $reflectionProvider)
$this->reflectionProvider = $reflectionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ObjectType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ObjectWithoutClassType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

final class ParentStaticTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ParentStaticType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

final class ResourceTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ResourceType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

final class SelfObjectTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return SelfObjectType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return StaticType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ final class StrictMixedTypeMapper implements TypeMapperInterface
*/
private const MIXED = 'mixed';

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return StrictMixedType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return StringType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

final class ThisTypeMapper implements TypeMapperInterface
{
/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return ThisType::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public function __construct(StringTypeMapper $stringTypeMapper)
$this->stringTypeMapper = $stringTypeMapper;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return TypeWithClassName::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public function autowireUnionTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTy
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return UnionType::class;
Expand All @@ -107,7 +110,6 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
}

$unionTypesNodes = array_unique($unionTypesNodes);

return new AttributeAwareUnionTypeNode($unionTypesNodes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function __construct(PhpVersionProvider $phpVersionProvider)
$this->phpVersionProvider = $phpVersionProvider;
}

/**
* @return class-string<Type>
*/
public function getNodeClass(): string
{
return VoidType::class;
Expand Down
Loading

0 comments on commit 031deda

Please sign in to comment.