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
2 changes: 1 addition & 1 deletion build/target-repository/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"require": {
"php": "^7.4|^8.0",
"phpstan/phpstan": "^2.2.2"
"phpstan/phpstan": "^2.2.6"
},
"autoload": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"nikic/php-parser": "^5.8",
"ondram/ci-detector": "^4.2",
"phpstan/phpdoc-parser": "^2.3.3",
"phpstan/phpstan": "^2.2.2",
"phpstan/phpstan": "^2.2.6",
"react/event-loop": "^1.6",
"react/promise": "^3.3",
"react/socket": "^1.17",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector
class MultipleTypes
{
/**
* @var bool|int|string
* @var int|string|bool
*/
public $value;
public function set()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace Rector\Tests\DeadCode\Rector\FunctionLike\NarrowWideUnionReturnTypeRec

final class FinalClass
{
public function getData(): int|string
public function getData(): string|int
{
if (rand(0, 1)) {
return 'text';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Rector\Tests\DeadCode\Rector\FunctionLike\NarrowWideUnionReturnTypeRector\So

final class PhpDocs
{
/** @return string|int|float */
/** @return string|int */
public function foo(): string|int|float
{
if (rand(0, 1)) {
Expand Down Expand Up @@ -71,7 +71,7 @@ use Rector\Tests\DeadCode\Rector\FunctionLike\NarrowWideUnionReturnTypeRector\So

final class PhpDocs
{
/** @return int|string */
/** @return string|int */
public function foo(): string|int
{
if (rand(0, 1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class PathInfoReturn
*
* @psalm-assert-if-true =non-empty-string $filename
*/
public static function extension($filename): array|string
public static function extension($filename): string|array
{
return pathinfo($filename, PATHINFO_EXTENSION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Fixture
*/
public function runThree(array $array, array $arrayTwo)
{
return array_map(function (string $value, \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Bar|\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Foo $second) {
return array_map(function (string $value, \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Foo|\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeForArrayMapRector\Source\Bar $second) {
return get_class($second) . $value;
}, $array, $arrayTwo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Fixture
*/
public function runTwo(array $array)
{
return array_reduce($array, function (int|string $carry, int|string $value) {
return array_reduce($array, function (int|string $carry, string|int $value) {
return $carry . $value;
}, 100);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockRetur
class ComplexArray
{
/**
* @return array<string, array<string, array<string, string>|int>|array<string, int|string>|bool>
* @return array<string, array<string, int|array<string, string>>|array<string, string|int>|bool>
*/
public function run(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockRetur
final class CoverImplicitKey
{
/**
* @return array<int, array<string, int|string>>
* @return array<int, array<string, string|int>>
*/
private static function getExpectedAllOwners(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class RepeatedItemType
) {}

/**
* @return array<string, string|array<string, int|string>>
* @return array<string, string|array<string, string|int>>
*/
public function toRequestPayload(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockRetur
final class WithDuplicatedNestedArray
{
/**
* @return array<string, string[][]|bool[]|int[]>
* @return array<string, bool[]|int[]|string[][]>
*/
public static function run(): iterable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class UnionObjects
}

/**
* @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject[]|\Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[] $items
* @param \Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\SomeReturnedObject[]|\Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Source\AnotherReturnedObject[] $items
*/
private function run(array $items)
{
Expand Down
43 changes: 18 additions & 25 deletions src/DependencyInjection/PHPStan/PHPStanContainerMemento.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,39 @@

namespace Rector\DependencyInjection\PHPStan;

use PHPStan\DependencyInjection\MemoizingContainer;
use PHPStan\DependencyInjection\Nette\NetteContainer;
use PhpParser\NodeVisitor;
use PHPStan\DependencyInjection\DirectExtensionsCollection;
use PHPStan\DependencyInjection\ExtensionsCollection;
use PHPStan\Parser\AnonymousClassVisitor;
use PHPStan\Parser\ArrayMapArgVisitor;
use PHPStan\Parser\RichParser;
use Rector\Util\Reflection\PrivatesAccessor;

/**
* Helper service to modify PHPStan container
* Helper service to modify PHPStan RichParser node visitors
* To avoid issues caused by node replacement, like @see https://github.com/rectorphp/rector/issues/9492
*/
final class PHPStanContainerMemento
{
public static function removeRichVisitors(RichParser $richParser): void
{
// the only way now seems to access container early and remove unwanted services
// here https://github.com/phpstan/phpstan-src/blob/522421b007cbfc674bebb93e823c774167ac78cd/src/Parser/RichParser.php#L90-L92
$privatesAccessor = new PrivatesAccessor();

/** @var MemoizingContainer $container */
$container = $privatesAccessor->getPrivateProperty($richParser, 'container');

/** @var NetteContainer $originalContainer */
$originalContainer = $privatesAccessor->getPrivateProperty($container, 'originalContainer');

/** @var NetteContainer $originalContainer */
$deeperContainer = $privatesAccessor->getPrivateProperty($originalContainer, 'container');

// get tags property
$tags = $privatesAccessor->getPrivateProperty($deeperContainer, 'tags');
/** @var ExtensionsCollection<NodeVisitor> $nodeVisitorsCollection */
$nodeVisitorsCollection = $privatesAccessor->getPrivateProperty($richParser, 'nodeVisitors');

// keep visitors that are useful
// remove all the rest, https://github.com/phpstan/phpstan-src/tree/1d86de8bb9371534983a8dbcd879e057d2ff028f/src/Parser
$nodeVisitorsToKeep = [
$container->findServiceNamesByType(AnonymousClassVisitor::class)[0] => true,
$container->findServiceNamesByType(ArrayMapArgVisitor::class)[0] => true,
];

$tags[RichParser::VISITOR_SERVICE_TAG] = $nodeVisitorsToKeep;

$privatesAccessor->setPrivateProperty($deeperContainer, 'tags', $tags);
// remove all the rest, https://github.com/phpstan/phpstan-src/tree/2.2.x/src/Parser
$nodeVisitorsToKeep = array_filter(
$nodeVisitorsCollection->getAll(),
static fn (NodeVisitor $nodeVisitor): bool => $nodeVisitor instanceof AnonymousClassVisitor
|| $nodeVisitor instanceof ArrayMapArgVisitor
);

$privatesAccessor->setPrivateProperty(
$richParser,
'nodeVisitors',
new DirectExtensionsCollection($nodeVisitorsToKeep)
);
}
}
Loading