Skip to content

Commit

Permalink
improve self/static support
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Oct 26, 2021
1 parent c00a50d commit a279265
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
12 changes: 3 additions & 9 deletions packages/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StaticType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Php\PhpVersionProvider;
Expand Down Expand Up @@ -50,15 +49,10 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): Type
*/
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
if ($type instanceof ThisType) {
// @todo wait for PHPStan to differentiate between self/static
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE)) {
return new Name(ObjectReference::STATIC()->getValue());
}

return new Name(ObjectReference::SELF()->getValue());
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE)) {
return new Name(ObjectReference::STATIC()->getValue());
}

return null;
return new Name(ObjectReference::SELF()->getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;

final class ReturnStatic
{
public function run()
{
return new static();
}

public function run2()
{
if (rand(0, 1)) {
return new static();
}

return null;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ReturnTypeDeclarationRector\Fixture;

final class ReturnStatic
{
public function run(): self
{
return new static();
}

public function run2(): ?self
{
if (rand(0, 1)) {
return new static();
}

return null;
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function provideData(): Iterator

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/before_union_types.php';
return __DIR__ . '/config/before_static_type.php';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::UNION_TYPES - 1);
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::STATIC_RETURN_TYPE - 1);
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, __DIR__ . '/../../../../../../phpstan-for-rector.neon');

$services = $containerConfigurator->services();
Expand Down

This file was deleted.

0 comments on commit a279265

Please sign in to comment.