Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move string downgrade types to object classes #4801

Merged
merged 4 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +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;
Expand All @@ -23,27 +22,16 @@ 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);
if ($type instanceof NullableType) {
$type = $type->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 @@ -22,22 +22,16 @@ 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);
if ($type instanceof NullableType) {
$type = $type->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 Down Expand Up @@ -47,8 +48,8 @@ public function someFunction($someObject)
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'object';
return ObjectWithoutClassType::class;
}
}
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 Down Expand Up @@ -49,8 +50,8 @@ public function getSomeObject()
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'object';
return ObjectWithoutClassType::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp80\Rector\FunctionLike;

use PHPStan\Type\MixedType;
use Rector\DowngradePhp72\Rector\FunctionLike\AbstractDowngradeParamTypeDeclarationRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -48,8 +49,8 @@ public function someFunction($anything)
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'mixed';
return MixedType::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp80\Rector\FunctionLike;

use PHPStan\Type\MixedType;
use Rector\DowngradePhp72\Rector\FunctionLike\AbstractDowngradeReturnTypeDeclarationRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -56,8 +57,8 @@ public function getAnything(bool $flag)
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'mixed';
return MixedType::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\DowngradePhp80\Rector\FunctionLike;

use PHPStan\Type\StaticType;
use Rector\DowngradePhp72\Rector\FunctionLike\AbstractDowngradeReturnTypeDeclarationRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -50,8 +51,8 @@ public function getStatic()
);
}

public function getTypeNameToRemove(): string
public function getTypeToRemove(): string
{
return 'static';
return StaticType::class;
}
}