Skip to content

Commit

Permalink
[TypeDeclaration] Remove PhpDocTypeChanger->changeVarType() on TypedP…
Browse files Browse the repository at this point in the history
…ropertyFromAssignsRector (#3163)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 6, 2022
1 parent 44c95d4 commit f35326d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 130 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

final class StaticPropertyWithDefaultNull
final class PropertyWithDefaultNull
{
private $cacheFile = null;
private $cacheFiles = null;
Expand Down Expand Up @@ -31,12 +31,9 @@ final class StaticPropertyWithDefaultNull

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

final class StaticPropertyWithDefaultNull
final class PropertyWithDefaultNull
{
private ?string $cacheFile = null;
/**
* @var string[]|null
*/
private ?array $cacheFiles = null;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

final class SkipAnonymousExtendsExistingClassInUnion
{
private $x;

public function __construct()
{
if (rand(0,1)) {
$this->x = new \DateTime('now');
} else {
$this->x = new class extends \DateTime {};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

final class AnonymousExtendsExistingClassInUnionRemoveDocblock
final class AnonymousExtendsExistingClassDocblockExists
{
/**
* @var \DateTime|null
Expand All @@ -22,12 +22,15 @@ public function __construct()

?>
-----
<?php
<?php declare(strict_types=1);

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

final class AnonymousExtendsExistingClassInUnion
final class AnonymousExtendsExistingClassDocblockExists
{
/**
* @var \DateTime|null
*/
private \DateTime|null $x = null;

public function __construct()
Expand Down

This file was deleted.

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

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

final class SkipUnionProtectedProperty
{
protected $stringOrInteger = 'hi';

public function setNumber()
{
if (mt_rand(0, 100)) {
$this->stringOrInteger = 'hey';
} else {
$this->stringOrInteger = 1000;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,37 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): Node|null|array
{
if ($this->inlineHTMLAnalyzer->hasInlineHTML($node)) {
$processNode = clone $node;
if ($this->inlineHTMLAnalyzer->hasInlineHTML($processNode)) {
return null;
}

$expectedNamespace = $this->psr4AutoloadNamespaceMatcher->getExpectedNamespace($this->file, $node);
$expectedNamespace = $this->psr4AutoloadNamespaceMatcher->getExpectedNamespace($this->file, $processNode);
if ($expectedNamespace === null) {
return null;
}

// is namespace and already correctly named?
if ($node instanceof Namespace_ && $this->nodeNameResolver->isCaseSensitiveName(
$node,
if ($processNode instanceof Namespace_ && $this->nodeNameResolver->isCaseSensitiveName(
$processNode,
$expectedNamespace
)) {
return null;
}

if ($node instanceof Namespace_ && $this->hasNamespaceInPreviousNamespace($node)) {
if ($processNode instanceof Namespace_ && $this->hasNamespaceInPreviousNamespace($processNode)) {
return null;
}

// to put declare_strict types on correct place
if ($node instanceof FileWithoutNamespace) {
return $this->refactorFileWithoutNamespace($node, $expectedNamespace);
if ($processNode instanceof FileWithoutNamespace) {
return $this->refactorFileWithoutNamespace($processNode, $expectedNamespace);
}

$node->name = new Name($expectedNamespace);
$this->fullyQualifyStmtsAnalyzer->process($node->stmts);
$processNode->name = new Name($expectedNamespace);
$this->fullyQualifyStmtsAnalyzer->process($processNode->stmts);

return $node;
return $processNode;
}

private function hasNamespaceInPreviousNamespace(Namespace_ $namespace): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,25 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$processNode = clone $node;
$this->newNamespace = null;

if ($node instanceof FileWithoutNamespace) {
$changedStmts = $this->refactorStmts($node->stmts);
if ($processNode instanceof FileWithoutNamespace) {
$changedStmts = $this->refactorStmts($processNode->stmts);
if ($changedStmts === null) {
return null;
}

$node->stmts = $changedStmts;
$processNode->stmts = $changedStmts;

// add a new namespace?
if ($this->newNamespace !== null) {
return new Namespace_(new Name($this->newNamespace), $changedStmts);
}
}

if ($node instanceof Namespace_) {
return $this->refactorNamespace($node);
if ($processNode instanceof Namespace_) {
return $this->refactorNamespace($processNode);
}

return null;
Expand Down
18 changes: 14 additions & 4 deletions rules/TypeDeclaration/NodeTypeAnalyzer/PropertyTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public function decoratePropertyUnionType(
UnionType $unionType,
Name|ComplexType $typeNode,
Property $property,
PhpDocInfo $phpDocInfo
PhpDocInfo $phpDocInfo,
bool $changeVarTypeFallback = true
): void {
if (! $this->unionTypeAnalyzer->isNullable($unionType)) {
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::UNION_TYPES)) {
$property->type = $typeNode;
} else {
return;
}

if ($changeVarTypeFallback) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $unionType);
}

Expand All @@ -52,9 +56,15 @@ public function decoratePropertyUnionType(
}

// has array with defined type? add docs
if ($this->isDocBlockRequired($unionType)) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $unionType);
if (! $this->isDocBlockRequired($unionType)) {
return;
}

if (! $changeVarTypeFallback) {
return;
}

$this->phpDocTypeChanger->changeVarType($phpDocInfo, $unionType);
}

private function isDocBlockRequired(UnionType $unionType): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -49,7 +47,6 @@ final class TypedPropertyFromAssignsRector extends AbstractRector implements All
public function __construct(
private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer,
private readonly PropertyTypeDecorator $propertyTypeDecorator,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly VarTagRemover $varTagRemover,
private readonly MakePropertyTypedGuard $makePropertyTypedGuard,
private readonly PhpVersionProvider $phpVersionProvider,
Expand Down Expand Up @@ -129,41 +126,39 @@ public function refactor(Node $node): ?Node

$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($inferredType, TypeKind::PROPERTY);
if ($typeNode === null) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $inferredType);
return $this->processChangedPhpDocInfo($phpDocInfo, $node);
return null;
}

if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $inferredType);
return $this->processChangedPhpDocInfo($phpDocInfo, $node);
return null;
}

// non-private property can be anything with not inline public configured
if (! $node->isPrivate() && ! $this->inlinePublic) {
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $inferredType);
return $this->processChangedPhpDocInfo($phpDocInfo, $node);
return null;
}

if ($inferredType instanceof UnionType) {
$this->propertyTypeDecorator->decoratePropertyUnionType($inferredType, $typeNode, $node, $phpDocInfo);
$this->propertyTypeDecorator->decoratePropertyUnionType(
$inferredType,
$typeNode,
$node,
$phpDocInfo,
false
);
} else {
$node->type = $typeNode;
}

if (! $node->type instanceof Node) {
return null;
}

$this->varTagRemover->removeVarTagIfUseless($phpDocInfo, $node);

return $node;
}

private function processChangedPhpDocInfo(PhpDocInfo $phpDocInfo, Node $node): ?Node
{
if ($phpDocInfo->hasChanged()) {
return $node;
}

return null;
}

private function decorateTypeWithNullableIfDefaultPropertyNull(Property $property, Type $inferredType): Type
{
$defaultExpr = $property->props[0]->default;
Expand Down

0 comments on commit f35326d

Please sign in to comment.