Skip to content

Commit

Permalink
[TypeDeclaration] Add TypedPropertyFromStrictSetUpRector (#2636)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 6, 2022
1 parent 3f87791 commit 649e511
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 153 deletions.
27 changes: 25 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 411 Rules Overview
# 412 Rules Overview

<br>

Expand Down Expand Up @@ -70,7 +70,7 @@

- [Transform](#transform) (35)

- [TypeDeclaration](#typedeclaration) (29)
- [TypeDeclaration](#typedeclaration) (30)

- [Visibility](#visibility) (3)

Expand Down Expand Up @@ -9783,6 +9783,29 @@ Complete property type based on getter strict types

<br>

### TypedPropertyFromStrictSetUpRector

Add strict typed property based on `setUp()` strict typed assigns in TestCase

- class: [`Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector`](../rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictSetUpRector.php)

```diff
use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
- private $value;
+ private int $value;

public function setUp()
{
$this->value = 1000;
}
}
```

<br>

### VarAnnotationIncorrectNullableRector

Add or remove null type from `@var` phpdoc typehint based on php property type declaration
Expand Down
2 changes: 2 additions & 0 deletions config/set/type-declaration-strict.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\TypeDeclaration\Rector\Param\ParamTypeFromStrictTypedPropertyRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodReturnTypeRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
Expand All @@ -34,5 +35,6 @@
ReturnTypeFromStrictNativeFuncCallRector::class,
ReturnTypeFromStrictNewArrayRector::class,
ReturnTypeFromStrictScalarReturnExprRector::class,
TypedPropertyFromStrictSetUpRector::class,
]);
};

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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

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

use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
private $value;

public function setUp()
{
$this->value = 1000;
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class SomeClass extends TestCase
{
private int $value;

public function setUp()
{
$this->value = 1000;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class TypedPropertyFromStrictConstructorPhp73RectorTest extends AbstractRectorTestCase
final class TypedPropertyFromStrictSetUpRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
Expand All @@ -23,11 +23,11 @@ public function test(SmartFileInfo $fileInfo): void
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixturePhp73');
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(TypedPropertyFromStrictSetUpRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\ConstructorPropertyTypeInferer;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\TrustedClassMethodPropertyTypeInferer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\TypedPropertyFromStrictConstructorRectorTest
*/
final class TypedPropertyFromStrictConstructorRector extends AbstractRector
final class TypedPropertyFromStrictConstructorRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ConstructorPropertyTypeInferer $constructorPropertyTypeInferer,
private readonly TrustedClassMethodPropertyTypeInferer $trustedClassMethodPropertyTypeInferer,
private readonly VarTagRemover $varTagRemover,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly ConstructorAssignDetector $constructorAssignDetector,
Expand Down Expand Up @@ -83,12 +85,12 @@ public function refactor(Node $node): ?Node
return null;
}

$varType = $this->constructorPropertyTypeInferer->inferProperty($node);
if (! $varType instanceof Type) {
$propertyType = $this->trustedClassMethodPropertyTypeInferer->inferProperty($node, MethodName::CONSTRUCT);
if (! $propertyType instanceof Type) {
return null;
}

if ($varType instanceof MixedType) {
if ($propertyType instanceof MixedType) {
return null;
}

Expand All @@ -99,18 +101,18 @@ public function refactor(Node $node): ?Node

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $varType);
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType);
return $node;
}

$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($varType, TypeKind::PROPERTY);
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY);
if (! $propertyTypeNode instanceof Node) {
return null;
}

// public property can be anything
if ($node->isPublic()) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $varType);
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $propertyType);
return $node;
}

Expand Down

0 comments on commit 649e511

Please sign in to comment.