Skip to content
Open
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
1 change: 1 addition & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
- 'e2e/parallel-reflection-resolver'
- 'e2e/parallel with space'
- 'e2e/print-new-node'
- 'e2e/phpstan-container-bootstrap'

name: End to end test - ${{ matrix.directory }}

Expand Down
2 changes: 2 additions & 0 deletions bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Rector\Bootstrap\AutoloadFileParameterResolver;
use Rector\Bootstrap\RectorConfigsResolver;
use Rector\ChangesReporting\Output\JsonOutputFormatter;
use Rector\Config\RectorConfig;
use Rector\Configuration\Option;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\DependencyInjection\LazyContainerFactory;
use Rector\DependencyInjection\RectorContainerFactory;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
Expand Down
12 changes: 12 additions & 0 deletions e2e/phpstan-container-bootstrap/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

if (!isset($container)) {
echo 'missing global $container variable';
} elseif (get_class($container) === 'PHPStan\DependencyInjection\MemoizingContainer') {
echo "working as expected\n";
exit(0);
} else {
echo '$container has wrong class ' . get_class($container);
}

exit(1);
7 changes: 7 additions & 0 deletions e2e/phpstan-container-bootstrap/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"require": {
"php": "^8.1"
},
"minimum-stability": "dev",
"prefer-stable": true
}
12 changes: 12 additions & 0 deletions e2e/phpstan-container-bootstrap/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([__DIR__ . '/src']);

$rectorConfig->bootstrapFiles(['bootstrap.php']);

$rectorConfig->rule(AddParamBasedOnParentClassMethodRector::class);
};
3 changes: 3 additions & 0 deletions e2e/phpstan-container-bootstrap/src/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo "hello world";
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ parameters:
identifier: offsetAccess.nonArray
path: src/PhpParser/NodeTraverser/RectorNodeTraverser.php

# variable is expected in included/required file
-
identifier: closure.unusedUse
message: '#Anonymous function has an unused use \$container#'
paths:
- src/Autoloading/BootstrapFilesIncluder.php

# false positive
-
message: '#Method (.*?)refactor\(\) (never returns|should return) (.*?)#'
Expand Down
8 changes: 6 additions & 2 deletions src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use RecursiveIteratorIterator;
use SplFileInfo;
use Webmozart\Assert\Assert;
use PHPStan\DependencyInjection\Container;

/**
* @see \Rector\Tests\Autoloading\BootstrapFilesIncluderTest
Expand All @@ -21,7 +22,7 @@ final class BootstrapFilesIncluder
* Inspired by
* @see https://github.com/phpstan/phpstan-src/commit/aad1bf888ab7b5808898ee5fe2228bb8bb4e4cf1
*/
public function includeBootstrapFiles(): void
public function includeBootstrapFiles(Container $container): void
{
$bootstrapFiles = SimpleParameterProvider::provideArrayParameter(Option::BOOTSTRAP_FILES);

Expand All @@ -33,7 +34,10 @@ public function includeBootstrapFiles(): void
throw new ShouldNotHappenException(sprintf('Bootstrap file "%s" does not exist.', $bootstrapFile));
}

require $bootstrapFile;
// mimic PHPStan bootstrap file inclusion (bootstrap files have access to the global $container variable)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo, I am thinking if this can be configurable via RectorConfig/RectorConfigBuilder, eg: enableForwardPHPStanBootstrap() config, whether it enable by default or not is fine, but should be configurable.

(static function (string $file) use ($container): void {
require $file;
Comment thread
samsonasik marked this conversation as resolved.
})($bootstrapFile);
}

$this->requireRectorStubs();
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/RectorContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Autoloading\BootstrapFilesIncluder;
use Rector\Caching\Detector\ChangedFilesDetector;
use Rector\Config\RectorConfig;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\ValueObject\Bootstrap\BootstrapConfigs;

final class RectorContainerFactory
Expand All @@ -25,7 +26,7 @@ public function createFromBootstrapConfigs(BootstrapConfigs $bootstrapConfigs):

/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
$bootstrapFilesIncluder = $rectorConfig->get(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();
$bootstrapFilesIncluder->includeBootstrapFiles($rectorConfig->get(PHPStanServicesFactory::class)->getContainer());

return $rectorConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ public function createDynamicSourceLocatorProvider(): DynamicSourceLocatorProvid
return $this->container->getByType(DynamicSourceLocatorProvider::class);
}

/**
* @api
*/
public function getContainer(): Container
{
return $this->container;
}

/**
* @return string[]
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Rector\Contract\Rector\RectorInterface;
use Rector\DependencyInjection\Laravel\ContainerMemento;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -116,7 +117,7 @@ protected function setUp(): void

/** @var BootstrapFilesIncluder $bootstrapFilesIncluder */
$bootstrapFilesIncluder = $this->make(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();
$bootstrapFilesIncluder->includeBootstrapFiles($rectorConfig->get(PHPStanServicesFactory::class)->getContainer());
}

protected function tearDown(): void
Expand Down
3 changes: 2 additions & 1 deletion tests/Autoloading/BootstrapFilesIncluderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use Rector\Autoloading\BootstrapFilesIncluder;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

final class BootstrapFilesIncluderTest extends AbstractLazyTestCase
Expand All @@ -14,6 +15,6 @@ final class BootstrapFilesIncluderTest extends AbstractLazyTestCase
public function test(): void
{
$bootstrapFilesIncluder = $this->make(BootstrapFilesIncluder::class);
$bootstrapFilesIncluder->includeBootstrapFiles();
$bootstrapFilesIncluder->includeBootstrapFiles($this->getContainer()->get(PHPStanServicesFactory::class)->getContainer());
}
}
Loading