Skip to content

Commit

Permalink
[PHP 8.0] Make Downgrade widening union depend on ClassMethod, the na…
Browse files Browse the repository at this point in the history
…rrow scope (#375)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 4, 2021
1 parent ae71516 commit a9b1bbb
Show file tree
Hide file tree
Showing 67 changed files with 333 additions and 410 deletions.
2 changes: 1 addition & 1 deletion build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -4766,7 +4766,7 @@ Remove the "object" param and return type, add a `@param` and `@return` tags ins

Change param type to match the lowest type in whole family tree

- class: [`Rector\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector`](../rules/DowngradePhp72/Rector/Class_/DowngradeParameterTypeWideningRector.php)
- class: [`Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector`](../rules/DowngradePhp72/Rector/Class_/DowngradeParameterTypeWideningRector.php)

```diff
interface SomeInterface
Expand Down
2 changes: 1 addition & 1 deletion config/set/downgrade-php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector;
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): void
$newType,
TypeKind::RETURN()
);

$currentReturnTagValueNode = $phpDocInfo->getReturnTagValue();

if ($currentReturnTagValueNode !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public function resolve(Node $node): Type
if ($classNode instanceof Trait_) {
/** @var string $traitName */
$traitName = $classNode->getAttribute(AttributeKey::CLASS_NAME);

$scope = $this->traitNodeScopeCollector->getScopeForTraitAndNode($traitName, $node);
$scope = $this->traitNodeScopeCollector->getScopeForTrait($traitName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function resolveNodeScope(Variable $variable): ?Scope
if ($classLike instanceof Trait_) {
/** @var string $traitName */
$traitName = $variable->getAttribute(AttributeKey::CLASS_NAME);
$traitNodeScope = $this->traitNodeScopeCollector->getScopeForTraitAndNode($traitName, $variable);
$traitNodeScope = $this->traitNodeScopeCollector->getScopeForTrait($traitName);

if ($traitNodeScope !== null) {
return $traitNodeScope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

namespace Rector\NodeTypeResolver\PHPStan\Collector;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\VirtualNode;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;

final class TraitNodeScopeCollector
{
Expand All @@ -16,38 +13,18 @@ final class TraitNodeScopeCollector
*/
private array $scopeByTraitNodeHash = [];

public function __construct(
private BetterStandardPrinter $betterStandardPrinter
) {
}

public function addForTraitAndNode(string $traitName, Node $node, Scope $scope): void
public function addForTrait(string $traitName, Scope $scope): void
{
if ($node instanceof VirtualNode) {
return;
}

$traitNodeHash = $this->createHash($traitName, $node);

// probably set from another class
if (isset($this->scopeByTraitNodeHash[$traitNodeHash])) {
if (isset($this->scopeByTraitNodeHash[$traitName])) {
return;
}

$this->scopeByTraitNodeHash[$traitNodeHash] = $scope;
$this->scopeByTraitNodeHash[$traitName] = $scope;
}

public function getScopeForTraitAndNode(string $traitName, Node $node): ?Scope
public function getScopeForTrait(string $traitName): ?Scope
{
$traitNodeHash = $this->createHash($traitName, $node);

return $this->scopeByTraitNodeHash[$traitNodeHash] ?? null;
}

private function createHash(string $traitName, Node $node): string
{
$printedNode = $this->betterStandardPrinter->print($node);

return sha1($traitName . $printedNode);
return $this->scopeByTraitNodeHash[$traitName] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeTraverser;
use PHPStan\AnalysedCodeException;
use PHPStan\Analyser\MutatingScope;
Expand Down Expand Up @@ -71,10 +72,15 @@ public function processNodes(array $nodes, SmartFileInfo $smartFileInfo): array
$nodeCallback = function (Node $node, Scope $scope): void {
// traversing trait inside class that is using it scope (from referenced) - the trait traversed by Rector is different (directly from parsed file)
if ($scope->isInTrait()) {
/** @var ClassReflection $classReflection */
$classReflection = $scope->getTraitReflection();
$traitName = $classReflection->getName();
$this->traitNodeScopeCollector->addForTraitAndNode($traitName, $node, $scope);
// has just entereted trait, to avoid adding it for ever ynode
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Trait_) {
/** @var ClassReflection $classReflection */
$classReflection = $scope->getTraitReflection();
$traitName = $classReflection->getName();

$this->traitNodeScopeCollector->addForTrait($traitName, $scope);
}

return;
}
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,8 @@ parameters:
-
message: '#foreach\(\.\.\.\), while\(\), for\(\) or if\(\.\.\.\) cannot contains a complex expression\. Extract it to a new variable assign on line before#'
path: src/Application/ApplicationFileProcessor.php


- '#Callable callable\(PHPStan\\Type\\Type\)\: PHPStan\\Type\\Type invoked with 2 parameters, 1 required#'
# leave for now
- '#Cognitive complexity for "Rector\\TypeDeclaration\\TypeNormalizer\:\:normalizeArrayTypeAndArrayNever\(\)" is 14, keep it under 9#'

This file was deleted.

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

namespace Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

use Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some\Trait_;
use Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some\SomeTrait;

final class SameEndWithExistingImport
{
public function __construct(\Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some\Trait_ $firstTrait)
public function __construct(\Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some\SomeTrait $firstTrait)
{
}
}
Expand All @@ -17,11 +17,11 @@ final class SameEndWithExistingImport

namespace Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Fixture;

use Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some\Trait_;
use Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some\SomeTrait;

final class SameEndWithExistingImport
{
public function __construct(Trait_ $firstTrait)
public function __construct(SomeTrait $firstTrait)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Another;

final class Trait_
final class NestedTrait
{

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

namespace Rector\Tests\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector\Source\Some;

final class Trait_
final class SomeTrait
{

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

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

/**
* @see https://3v4l.org/8B03D
Expand All @@ -21,7 +21,7 @@ final class Asker implements SomeAskingInterface
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

/**
* @see https://3v4l.org/8B03D
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface SomeContainerInterface
{
Expand All @@ -25,7 +25,7 @@ class SomeUniqueContainer implements SomeContainerInterface
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface SomeContainerInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface A
{
Expand All @@ -25,7 +25,7 @@ final class MostChild implements A
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface A
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\AnotherContainerInterface;

final class YetAnotherContainer implements AnotherContainerInterface
{
Expand All @@ -14,16 +16,13 @@ trait AnotherServiceLocatorTrait
}
}

interface AnotherContainerInterface
{
public function get($name);
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\AnotherContainerInterface;

final class YetAnotherContainer implements AnotherContainerInterface
{
Expand All @@ -40,9 +39,4 @@ trait AnotherServiceLocatorTrait
}
}

interface AnotherContainerInterface
{
public function get($name);
}

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

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface WhateverInterface
{
Expand All @@ -23,7 +23,7 @@ class SomeChildClass extends AbstractSomeAncestorClass
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface WhateverInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\SomeExternalContainerInterface;

class KeepInterfaceDonwgraded implements SomeExternalContainerInterface
{
public function get($id)
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

final class KeepTraitAndInterfaceSame implements AnyContainerInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface SomeAskingInterfaceWithNullable
{
Expand All @@ -18,7 +18,7 @@ final class AskForMore implements SomeAskingInterfaceWithNullable
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface SomeAskingInterfaceWithNullable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface ParentWithString
{
Expand All @@ -18,7 +18,7 @@ class ChildString implements ParentWithString
-----
<?php

namespace Rector\Tests\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;

interface ParentWithString
{
Expand Down

0 comments on commit a9b1bbb

Please sign in to comment.