Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,36 @@ public function createReprintedNode(Node $node): Node
{
// reset original node, to allow the printer to re-use the node
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->removeFilePositions($node);

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$node,
static function (Node $subNode): Node {
function (Node $subNode): Node {
$subNode->setAttribute(AttributeKey::ORIGINAL_NODE, null);
$this->removeFilePositions($subNode);
return $subNode;
}
);

return $node;
}

/**
* Positions may come from another file, e.g. a parent class constructor
*/
private function removeFilePositions(Node $node): void
{
$attributes = $node->getAttributes();
unset(
$attributes['startFilePos'],
$attributes['endFilePos'],
$attributes['startTokenPos'],
$attributes['endTokenPos']
);

$node->setAttributes($attributes);
}

private function createArrayItem(mixed $item, string|int|null $key = null): ArrayItem
{
$arrayItem = null;
Expand Down
23 changes: 20 additions & 3 deletions tests/Caching/Detector/ChangedFilesDetectorPortableCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

use FilesystemIterator;
use Nette\Utils\FileSystem;
use Rector\Caching\Cache;
use Rector\Caching\Config\FileHashComputer;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\FileSystem\FilePathHelper;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Util\FileHasher;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use SplFileInfo;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Webmozart\Assert\Assert;

final class ChangedFilesDetectorPortableCacheTest extends AbstractLazyTestCase
Expand Down Expand Up @@ -51,8 +57,6 @@ protected function setUp(): void
{
parent::setUp();

$this->changedFilesDetector = $this->make(ChangedFilesDetector::class);

$workingDirectory = getcwd();
Assert::string($workingDirectory);
$this->originalWorkingDirectory = $workingDirectory;
Expand All @@ -65,6 +69,14 @@ protected function setUp(): void
$this->firstCheckoutDirectory = $this->rootDirectory . '/first/project';
$this->secondCheckoutDirectory = $this->rootDirectory . '/second/project';

// own cache directory, as the shared Cache service keeps whichever directory it was first built with
$this->changedFilesDetector = new ChangedFilesDetector(
$this->make(FileHashComputer::class),
new Cache(new FileCacheStorage($this->getCacheDirectory(), $this->make(SymfonyFilesystem::class))),
$this->make(FileHasher::class),
$this->make(FilePathHelper::class)
);

$this->createCheckout($this->firstCheckoutDirectory);
$this->createCheckout($this->secondCheckoutDirectory);

Expand Down Expand Up @@ -281,9 +293,14 @@ private function cacheProjectFilesIn(string $directory): void
}
}

private function getCacheDirectory(): string
{
return $this->rootDirectory . '/cache';
}

private function countCacheEntries(): int
{
$cacheDirectory = SimpleParameterProvider::provideStringParameter(Option::CACHE_DIR);
$cacheDirectory = $this->getCacheDirectory();
if (! is_dir($cacheDirectory)) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Issues\InheritedConstructorFromOtherFile\Fixture;

use Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source\SomeParentWithDeepConstructor;

final class ChildWithoutConstructor extends SomeParentWithDeepConstructor
{
}

?>
-----
<?php

namespace Rector\Tests\Issues\InheritedConstructorFromOtherFile\Fixture;

use Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source\SomeParentWithDeepConstructor;

final class ChildWithoutConstructor extends SomeParentWithDeepConstructor
{
public function __construct(protected string $name, private readonly \Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source\SomeService $someService)
{
parent::__construct($name);
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Issues\InheritedConstructorFromOtherFile;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class InheritedConstructorFromOtherFileTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

declare(strict_types=1);

namespace Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Type\ObjectType;
use Rector\NodeManipulator\ClassDependencyManipulator;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\MethodName;

final class AddConstructorDependencyRector extends AbstractRector
{
public function __construct(
private readonly ClassDependencyManipulator $classDependencyManipulator
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('', []);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if ($node->getMethod(MethodName::CONSTRUCT) !== null) {
return null;
}

$this->classDependencyManipulator->addConstructorDependency(
$node,
new PropertyMetadata('someService', new ObjectType(SomeService::class))
);

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source;

abstract class SomeParentWithDeepConstructor
{
/**
* Pushes the constructor further into this file than the child fixture file is long
*/
public function padding(): array
{
return [
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
'padding padding padding padding padding padding padding padding padding',
];
}

public function __construct(protected string $name)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source;

final class SomeService
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Tests\Issues\InheritedConstructorFromOtherFile\Source\AddConstructorDependencyRector;

return RectorConfig::configure()
->withRules([AddConstructorDependencyRector::class]);
Loading