Skip to content

Commit

Permalink
[TypeDeclaration] Handle assign $this on TypedPropertyFromAssignsRect…
Browse files Browse the repository at this point in the history
…or (#5333)

* [TypeDeclaration] Handle assign $this on TypedPropertyFromAssignsRector

* fixed

* fix namesapce

* clean up

* [ci-review] Rector Rectify

* clean up

* clean up

* clean up

* clean up

* clean up

* clean up

* clean up

* clean up

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 6, 2023
1 parent 043d43c commit 79c57f0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/PHPStanStaticTypeMapper/TypeMapper/StaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;
use Rector\StaticTypeMapper\ValueObject\Type\SimpleStaticType;

Expand Down Expand Up @@ -45,23 +46,22 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
}

/**
* @param StaticType $type
* @param SimpleStaticType|StaticType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
// special case, for autocomplete of return type
if ($type instanceof SimpleStaticType) {
return new Name(ObjectReference::STATIC);
if ($type instanceof SelfStaticType) {
return new Name(ObjectReference::SELF);
}

if ($type instanceof SelfStaticType) {
if ($typeKind !== TypeKind::RETURN) {
return new Name(ObjectReference::SELF);
}

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

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

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

final class AssignThisBecomeSelf
{
private static $instance = null;

private function __construct()
{
static::$instance = $this;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

final class AssignThisBecomeSelf
{
private static ?self $instance = null;

private function __construct()
{
static::$instance = $this;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\FixtureComplexTypes;

final class AssignThisBecomeSelf
{
private static $instance = null;

private function __construct()
{
static::$instance = $this;
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\FixtureComplexTypes;

final class AssignThisBecomeSelf
{
private static ?self $instance = null;

private function __construct()
{
static::$instance = $this;
}
}

?>

0 comments on commit 79c57f0

Please sign in to comment.