Skip to content

Commit

Permalink
move string downgrade types to object classes (#4801)
Browse files Browse the repository at this point in the history
* move string downgrade types to object classes

* fix nullable type unwrap

* remove uneeded open tags from samples

* remove uneeded union type check
  • Loading branch information
TomasVotruba committed Dec 6, 2020
1 parent 3b22532 commit bfd1953
Show file tree
Hide file tree
Showing 32 changed files with 49 additions and 179 deletions.
18 changes: 11 additions & 7 deletions packages/phpstan-static-type-mapper/src/Utils/TypeUnwrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,29 @@ final class TypeUnwrapper
/**
* E.g. null|ClassType → ClassType
*/
public function unwrapNullableType(UnionType $unionType): ?Type
public function unwrapNullableType(Type $type): Type
{
if (count($unionType->getTypes()) !== 2) {
return null;
if (! $type instanceof UnionType) {
return $type;
}

if (count($type->getTypes()) !== 2) {
return $type;
}

if (! $unionType->isSuperTypeOf(new NullType())->yes()) {
return null;
if (! $type->isSuperTypeOf(new NullType())->yes()) {
return $type;
}

foreach ($unionType->getTypes() as $unionedType) {
foreach ($type->getTypes() as $unionedType) {
if ($unionedType instanceof NullType) {
continue;
}

return $unionedType;
}

return null;
return $type;
}

public function unwrapFirstObjectTypeFromUnionType(Type $type): Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
Expand All @@ -54,8 +52,6 @@ public function someMethod()
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
use Doctrine\ORM\EntityRepository;
class SomeRepository extends EntityRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
$a = 5;
$b = 10;
$result = !($a > 20 || $b <= 50);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
$a = 5;
$b = 10;
$result = $a <= 20 && $b > 50;
Expand Down
14 changes: 2 additions & 12 deletions rules/code-quality/src/Rector/If_/SimplifyIfReturnBoolRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Type\StaticTypeAnalyzer;
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -35,22 +34,13 @@ final class SimplifyIfReturnBoolRector extends AbstractRector
*/
private $staticTypeAnalyzer;

/**
* @var TypeUnwrapper
*/
private $typeUnwrapper;

/**
* @var CommentsMerger
*/
private $commentsMerger;

public function __construct(
CommentsMerger $commentsMerger,
StaticTypeAnalyzer $staticTypeAnalyzer,
TypeUnwrapper $typeUnwrapper
) {
$this->typeUnwrapper = $typeUnwrapper;
public function __construct(CommentsMerger $commentsMerger, StaticTypeAnalyzer $staticTypeAnalyzer)
{
$this->staticTypeAnalyzer = $staticTypeAnalyzer;
$this->commentsMerger = $commentsMerger;
}
Expand Down
5 changes: 1 addition & 4 deletions rules/defluent/src/NodeAnalyzer/ExprStringTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PhpParser\Node\Expr;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
Expand All @@ -32,9 +31,7 @@ public function __construct(NodeTypeResolver $nodeTypeResolver, TypeUnwrapper $t
public function resolve(Expr $expr): ?string
{
$exprStaticType = $this->nodeTypeResolver->getStaticType($expr);
if ($exprStaticType instanceof UnionType) {
$exprStaticType = $this->typeUnwrapper->unwrapNullableType($exprStaticType);
}
$exprStaticType = $this->typeUnwrapper->unwrapNullableType($exprStaticType);

if (! $exprStaticType instanceof TypeWithClassName) {
// nothing we can do, unless
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public const PUBLIC_CONST_B = 2;
Expand All @@ -33,8 +31,6 @@ class SomeClass
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
const PUBLIC_CONST_B = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function refactor(Node $node): ?Node
}

$this->decorateFunctionLikeWithReturnTagValueNode($node);

$node->returnType = null;

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public function run(iterable $iterator)
Expand All @@ -34,8 +32,6 @@ public function run(iterable $iterator)
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public function run(): iterable
Expand All @@ -34,8 +32,6 @@ public function run(): iterable
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public function run(?string $input)
Expand All @@ -34,8 +32,6 @@ public function run(?string $input)
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public function getResponseOrNothing(bool $flag): ?string
Expand All @@ -38,8 +36,6 @@ public function getResponseOrNothing(bool $flag): ?string
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp71\Rector\FunctionLike;

use PHPStan\Type\VoidType;
use Rector\DowngradePhp72\Rector\FunctionLike\AbstractDowngradeReturnTypeDeclarationRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -20,28 +21,22 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public function run(): void
{
// do something
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
* @return void
*/
public function run()
{
// do something
}
}
CODE_SAMPLE
Expand All @@ -50,8 +45,8 @@ public function run()
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'void';
return VoidType::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface DowngradeTypeRectorInterface
/**
* Name of the type to remove
*/
public function getTypeNameToRemove(): string;
public function getTypeToRemove(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Rector\DowngradePhp72\Rector\FunctionLike;

use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use Rector\DowngradePhp71\Rector\FunctionLike\AbstractDowngradeParamDeclarationRector;
use Rector\DowngradePhp72\Contract\Rector\DowngradeTypeRectorInterface;
Expand All @@ -23,27 +21,14 @@ public function shouldRemoveParamDeclaration(Param $param, FunctionLike $functio
return false;
}

// It can either be the type, or the nullable type (eg: ?object)
$isNullableType = $param->type instanceof NullableType;
if (! $param->type instanceof Identifier && ! $isNullableType) {
return false;
}

// If it is the NullableType, extract the name from its inner type
if ($isNullableType) {
/** @var NullableType */
$nullableType = $param->type;
$typeName = $this->getName($nullableType->type);
} else {
$typeName = $this->getName($param->type);
}
$type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
$type = $this->typeUnwrapper->unwrapNullableType($type);

// Check it is the type to be removed
return $typeName === $this->getTypeNameToRemove();
return is_a($type, $this->getTypeToRemove(), true);
}

protected function getRectorDefinitionDescription(): string
{
return sprintf("Remove the '%s' param type, add a @param tag instead", $this->getTypeNameToRemove());
return sprintf("Remove the '%s' param type, add a @param tag instead", $this->getTypeToRemove());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\DowngradePhp72\Rector\FunctionLike;

use PhpParser\Node\FunctionLike;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\DowngradePhp71\Rector\FunctionLike\AbstractDowngradeReturnDeclarationRector;
Expand All @@ -22,22 +21,14 @@ public function shouldRemoveReturnDeclaration(FunctionLike $functionLike): bool
return false;
}

// It can either be the type, or the nullable type (eg: ?object)
$isNullableType = $functionLike->returnType instanceof NullableType;
if ($isNullableType) {
/** @var NullableType */
$nullableType = $functionLike->returnType;
$typeName = $this->getName($nullableType->type);
} else {
$typeName = $this->getName($functionLike->returnType);
}
$type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType);
$type = $this->typeUnwrapper->unwrapNullableType($type);

// Check it is the type to be removed
return $typeName === $this->getTypeNameToRemove();
return is_a($type, $this->getTypeToRemove(), true);
}

protected function getRectorDefinitionDescription(): string
{
return sprintf("Remove the '%s' function type, add a @return tag instead", $this->getTypeNameToRemove());
return sprintf("Remove the '%s' function type, add a @return tag instead", $this->getTypeToRemove());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp72\Rector\FunctionLike;

use PHPStan\Type\ObjectWithoutClassType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -19,8 +20,6 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
public function someFunction(object $someObject)
Expand All @@ -30,8 +29,6 @@ public function someFunction(object $someObject)
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
<?php
class SomeClass
{
/**
Expand All @@ -47,8 +44,8 @@ public function someFunction($someObject)
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'object';
return ObjectWithoutClassType::class;
}
}
Loading

0 comments on commit bfd1953

Please sign in to comment.