Skip to content

Commit

Permalink
typed property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 22, 2021
1 parent 87d71a9 commit bfd2ae3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,8 @@ parameters:
# buggy phpstan clas-string
- '#Method (.*?) should return class\-string but returns string#'
- '#Method (.*?) should return array<class\-string\> but returns array<int, string\>#'

-
message: '#Unreachable statement \- code above always terminates#'
paths:
- rules/php80/src/Rector/Property/TypedPropertyFromStrictConstructorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@

declare(strict_types=1);

namespace Rector\Php80\Rector\Class_;
namespace Rector\Php80\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\MixedType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\ConstructorPropertyTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Php80\Tests\Rector\Class_\TypedPropertyFromStrictConstructorRector\TypedPropertyFromStrictConstructorRectorTest
* @see \Rector\Php80\Tests\Rector\Property\TypedPropertyFromStrictConstructorRector\TypedPropertyFromStrictConstructorRectorTest
*/
final class TypedPropertyFromStrictConstructorRector extends AbstractRector
{
/**
* @var ConstructorPropertyTypeInferer
*/
private $constructorPropertyTypeInferer;

public function __construct(ConstructorPropertyTypeInferer $constructorPropertyTypeInferer)
{
$this->constructorPropertyTypeInferer = $constructorPropertyTypeInferer;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add typed properties based only on strict constructor types', [
Expand Down Expand Up @@ -54,28 +67,37 @@ public function __construct(string $name)
*/
public function getNodeTypes(): array
{
return [\PhpParser\Node\Stmt\Class_::class];
return [Property::class];
}

/**
* @param \PhpParser\Node\Stmt\Class_ $node
* @param Property $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
return null;
}

$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if ($constructClassMethod === null) {
if ($node->type !== null) {
return null;
}

// @todo resolve param to propery assign
foreach ($constructClassMethod->getParams() as $parameter) {
$varType = $this->constructorPropertyTypeInferer->inferProperty($node);
if ($varType instanceof MixedType) {
return null;
}

$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$varType,
PHPStanStaticTypeMapper::KIND_PROPERTY
);

if ($propertyTypeNode === null) {
return null;
}

// change the node
$node->type = $propertyTypeNode;

return $node;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Rector\Php80\Tests\Rector\Class_\TypedPropertyFromStrictConstructorRector\Fixture;
namespace Rector\Php80\Tests\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

class SomeObject
class SomeClass
{
private $name;

Expand All @@ -16,9 +16,9 @@ class SomeObject
-----
<?php

namespace Rector\Php80\Tests\Rector\Class_\TypedPropertyFromStrictConstructorRector\Fixture;
namespace Rector\Php80\Tests\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

class SomeObject
class SomeClass
{
private string $name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

declare(strict_types=1);

namespace Rector\Php80\Tests\Rector\Class_\TypedPropertyFromStrictConstructorRector;
namespace Rector\Php80\Tests\Rector\Property\TypedPropertyFromStrictConstructorRector;

use Rector\Php80\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class TypedPropertyFromStrictConstructorRectorTest extends AbstractRectorTestCase
Expand All @@ -23,6 +24,6 @@ public function provideData(): \Iterator

protected function getRectorClass(): string
{
return \Rector\Php80\Rector\Class_\TypedPropertyFromStrictConstructorRector::class;
return TypedPropertyFromStrictConstructorRector::class;
}
}

0 comments on commit bfd2ae3

Please sign in to comment.