Skip to content

Commit

Permalink
[Arguments] Use native types over weak string to define types (#903)
Browse files Browse the repository at this point in the history
* [Arguments] Use native types over weak string to define types

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Sep 19, 2021
1 parent ab182ff commit a8a79d0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
14 changes: 13 additions & 1 deletion build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ This Rector adds new default arguments in calls of defined methods and class typ
- class: [`Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector`](../rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php)

```php
use PHPStan\Type\ObjectType;
use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Arguments\ValueObject\ArgumentAdder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand All @@ -120,7 +121,18 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ArgumentAdderRector::class)
->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => ValueObjectInliner::inline([
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, 'SomeType', null),
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, new ObjectType(
'SomeType',
null,
null,
[],
null,
null,
[],
[],
[],
[
]), null),
]),
]]);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

declare(strict_types=1);

use PHPStan\Type\ArrayType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope;
use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Arguments\ValueObject\ArgumentAdder;
Expand All @@ -13,6 +17,9 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$arrayType = new ArrayType(new MixedType(), new MixedType());

$services->set(ArgumentAdderRector::class)
->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => ValueObjectInliner::inline([
Expand All @@ -23,18 +30,25 @@
0,
'request',
null,
'Illuminate\Http\Illuminate\Http'
new ObjectType('Illuminate\Http\Illuminate\Http')
),
new ArgumentAdder(SomeContainerBuilder::class, 'compile', 0, 'isCompiled', false),
new ArgumentAdder(SomeContainerBuilder::class, 'addCompilerPass', 2, 'priority', 0, 'int'),
new ArgumentAdder(
SomeContainerBuilder::class,
'addCompilerPass',
2,
'priority',
0,
new IntegerType()
),
// scoped
new ArgumentAdder(
SomeParentClient::class,
'submit',
2,
'serverParameters',
[],
'array',
$arrayType,
ArgumentAddingScope::SCOPE_PARENT_CALL
),
new ArgumentAdder(
Expand All @@ -43,10 +57,10 @@
2,
'serverParameters',
[],
'array',
$arrayType,
ArgumentAddingScope::SCOPE_CLASS_METHOD
),
new ArgumentAdder(SomeClass::class, 'withoutTypeOrDefaultValue', 0, 'arguments', [], 'array'),
new ArgumentAdder(SomeClass::class, 'withoutTypeOrDefaultValue', 0, 'arguments', [], $arrayType),
]),
]]);
};
15 changes: 10 additions & 5 deletions rules/Arguments/NodeAnalyzer/ChangedArgumentsDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
namespace Rector\Arguments\NodeAnalyzer;

use PhpParser\Node\Param;
use PHPStan\Type\Type;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;

final class ChangedArgumentsDetector
{
public function __construct(
private ValueResolver $valueResolver,
private NodeNameResolver $nodeNameResolver
private StaticTypeMapper $staticTypeMapper,
private TypeComparator $typeComparator
) {
}

Expand All @@ -28,16 +31,18 @@ public function isDefaultValueChanged(Param $param, $value): bool
return ! $this->valueResolver->isValue($param->default, $value);
}

public function isTypeChanged(Param $param, ?string $type): bool
public function isTypeChanged(Param $param, ?Type $newType): bool
{
if ($param->type === null) {
return false;
}

if ($type === null) {
if ($newType === null) {
return true;
}

return ! $this->nodeNameResolver->isName($param->type, $type);
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);

return ! $this->typeComparator->areTypesEqual($currentParamType, $newType);
}
}
15 changes: 9 additions & 6 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Arguments\NodeAnalyzer\ArgumentAddingScope;
use Rector\Arguments\NodeAnalyzer\ChangedArgumentsDetector;
use Rector\Arguments\ValueObject\ArgumentAdder;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -53,9 +53,11 @@ public function __construct(

public function getRuleDefinition(): RuleDefinition
{
$objectType = new ObjectType('SomeType');

$exampleConfiguration = [
self::ADDED_ARGUMENTS => [
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, 'SomeType'),
new ArgumentAdder('SomeExampleClass', 'someMethod', 0, 'someArgument', true, $objectType),
],
];

Expand Down Expand Up @@ -234,7 +236,7 @@ private function addClassMethodParam(
ClassMethod $classMethod,
ArgumentAdder $argumentAdder,
mixed $defaultValue,
?string $type,
?Type $type,
int $position
): void {
$argumentName = $argumentAdder->getArgumentName();
Expand All @@ -243,8 +245,9 @@ private function addClassMethodParam(
}

$param = new Param(new Variable($argumentName), BuilderHelpers::normalizeValue($defaultValue));
if ($type) {
$param->type = ctype_upper($type[0]) ? new FullyQualified($type) : new Identifier($type);
if ($type !== null) {
$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PARAM());
$param->type = $typeNode;
}

$classMethod->params[$position] = $param;
Expand Down
5 changes: 3 additions & 2 deletions rules/Arguments/ValueObject/ArgumentAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Arguments\ValueObject;

use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

final class ArgumentAdder
{
Expand All @@ -17,7 +18,7 @@ public function __construct(
private int $position,
private ?string $argumentName = null,
private $argumentDefaultValue = null,
private ?string $argumentType = null,
private Type | null $argumentType = null,
private ?string $scope = null
) {
}
Expand Down Expand Up @@ -50,7 +51,7 @@ public function getArgumentDefaultValue()
return $this->argumentDefaultValue;
}

public function getArgumentType(): ?string
public function getArgumentType(): ?Type
{
return $this->argumentType;
}
Expand Down

0 comments on commit a8a79d0

Please sign in to comment.