Skip to content

Commit

Permalink
[TypeDeclaration] Handle return self on TypedPropertyFromStrictGetter…
Browse files Browse the repository at this point in the history
…MethodReturnTypeRector on php 8.0 feature enabled (#3088)

* Add failing test fixture for TypedPropertyFromStrictGetterMethodReturnTypeRector

# Failing Test for TypedPropertyFromStrictGetterMethodReturnTypeRector

Based on https://getrector.org/demo/ee1243f2-fa5c-4460-a27d-31fb67439bbf

* [TypeDeclaration] Handle return self on TypedPropertyFromStrictGetterMethodReturnTypeRector no php 8.0 feature enabled

* rename fixture

* [ci-review] Rector Rectify

* Fix namespace

* using SelfStaticType instead of SelfObjectType

Co-authored-by: Matthias Schmidt <matthias@mttsch.dev>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Nov 23, 2022
1 parent 30afbb8 commit 335d593
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
7 changes: 6 additions & 1 deletion packages/StaticTypeMapper/PhpParser/NameNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\StaticTypeMapper\ValueObject\Type\ParentObjectWithoutClassType;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Rector\StaticTypeMapper\ValueObject\Type\SelfStaticType;

/**
* @implements PhpParserNodeMapperInterface<Name>
Expand Down Expand Up @@ -79,7 +80,7 @@ private function isExistingClass(string $name): bool
private function createClassReferenceType(
Name $name,
string $reference
): MixedType | StaticType | ObjectWithoutClassType {
): MixedType | StaticType | SelfStaticType | ObjectWithoutClassType {
$classLike = $this->betterNodeFinder->findParentType($name, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return new MixedType();
Expand All @@ -92,6 +93,10 @@ private function createClassReferenceType(
return new StaticType($classReflection);
}

if ($reference === ObjectReference::SELF) {
return new SelfStaticType($classReflection);
}

if ($reference === ObjectReference::PARENT) {
$parentClassReflection = $classReflection->getParentClass();
if ($parentClassReflection instanceof ClassReflection) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector\FixturePhp80;

final class SomeSelf
{
private $test;

public function getTest(): self
{
return $this->test;
}
}
?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector\FixturePhp80;

final class SomeSelf
{
private self $test;

public function getTest(): self
{
return $this->test;
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class TypedPropertyFromStrictGetterMethodReturnTypePhp80RectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp80');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/rule_php80.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(TypedPropertyFromStrictGetterMethodReturnTypeRector::class);

$rectorConfig->phpVersion(PhpVersionFeature::STATIC_RETURN_TYPE);
};
12 changes: 8 additions & 4 deletions rules/Php80/Rector/NotIdentical/StrContainsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ public function refactor(Node $node): ?Node

if (isset($funcCall->args[2])) {
if ($this->isName($funcCall->name, 'strpos') && $this->isPositiveInteger($funcCall->args[2]->value)) {
$funcCall->args[0] = new Arg($this->nodeFactory->createFuncCall('substr', [$funcCall->args[0], $funcCall->args[2]]));
$funcCall->args[0] = new Arg($this->nodeFactory->createFuncCall(
'substr',
[$funcCall->args[0], $funcCall->args[2]]
));
}

unset($funcCall->args[2]);
}

Expand Down Expand Up @@ -134,12 +138,12 @@ private function matchIdenticalOrNotIdenticalToFalse(Identical | NotIdentical $e
return null;
}

private function isPositiveInteger(Expr $offset): bool
private function isPositiveInteger(Expr $expr): bool
{
if (! $offset instanceof LNumber) {
if (! $expr instanceof LNumber) {
return false;
}

return $offset->value > 0;
return $expr->value > 0;
}
}

0 comments on commit 335d593

Please sign in to comment.