Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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\PHPStanStaticTypeMapper\TypeMapper;

use PHPStan\Type\ThisType;
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
Expand Down Expand Up @@ -51,13 +52,19 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
// special case, for autocomplete of return type
if ($type instanceof SimpleStaticType) {
return new Name(ObjectReference::STATIC);
return $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE)
? new Name(ObjectReference::STATIC)
: new Name(ObjectReference::SELF);
}

if ($type instanceof SelfStaticType) {
return new Name(ObjectReference::SELF);
}

if ($type instanceof ThisType) {
return new Name(ObjectReference::SELF);
}
Comment on lines +64 to +66
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@staabm this will make add self to return even on non-final class => ?self on use return $this, so it can downgrade the functionality.

I guess we need another "special fixer rule" to change "self" to "static" when possible (depends on php version)

so this change need more consideration :)


if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::STATIC_RETURN_TYPE)) {
return new Name(ObjectReference::STATIC);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;

final class NullableReturnThis
{
public function run()
{
if (rand(0, 1)) {
return null;
}

return $this;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector\Fixture;

final class NullableReturnThis
{
public function run(): ?self
{
if (rand(0, 1)) {
return null;
}

return $this;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ public function refactor(Node $node): ?ClassMethod
if ($param->type instanceof Node) {
continue;
}

if ($param->variadic) {
continue;
}

if (! $this->paramTypeAddGuard->isLegal($param, $node)) {
continue;
}
Expand Down