Skip to content

Commit

Permalink
[Php54][Php80] Handle no scope on LongArrayToShortArrayRector+Annotat…
Browse files Browse the repository at this point in the history
…ionToAttributeRector (#2749)

* [Php54][Php80] Handle no scope on LongArrayToShortArrayRector+AnnotationToAttributeRector

* Fixed 🎉

* clean up

* phpstan

* phpstan

* final touch: eol

* cs fix
  • Loading branch information
samsonasik committed Aug 9, 2022
1 parent a80b3d3 commit 93b63b0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/NodeAnalyzer/ScopeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function isScopeResolvableFromFile(Node $node, ?MutatingScope $mutatingSc
}

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
return ! $parentNode instanceof Node;
if (! $parentNode instanceof Node) {
return true;
}

return ! $this->hasScope($parentNode);
}
}
33 changes: 33 additions & 0 deletions tests/Issues/ScopeNotAvailable/ArrayAnnotationToAttributeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\ScopeNotAvailable;

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

final class ArrayAnnotationToAttributeTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureArrayAnnotationToAttribute');
}

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

namespace Rector\Core\Tests\Issues\FixtureArrayAnnotationToAttribute\Fixture;

use Doctrine\ORM\Mapping as ORM;

class ArrayInAttribtue
{
public function __construct(
/**
* @ORM\ManyToOne(targetEntity=OfferPrice::class, inversedBy="myEntities", cascade={"persist"})
*/
private OfferPrice $offerPrice
)
{
}

}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\FixtureArrayAnnotationToAttribute\Fixture;

use Doctrine\ORM\Mapping as ORM;

class ArrayInAttribtue
{
public function __construct(
#[ORM\ManyToOne(targetEntity: OfferPrice::class, inversedBy: 'myEntities', cascade: ['persist'])] private OfferPrice $offerPrice
)
{
}

}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;

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

$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
new AnnotationToAttribute('Doctrine\ORM\Mapping\ManyToOne'),
]);
};

0 comments on commit 93b63b0

Please sign in to comment.