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

This file was deleted.

6 changes: 3 additions & 3 deletions rules/CodingStyle/Rector/Enum_/EnumCaseToPascalCaseRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ private function convertToPascalCase(string $name): string
fn ($part): string =>
// If part is all uppercase, convert to ucfirst(strtolower())
// If part is already mixed or PascalCase, keep as is except ucfirst
ctype_upper($part)
? ucfirst(strtolower($part))
: ucfirst($part),
ctype_upper((string) $part)
? ucfirst(strtolower((string) $part))
: ucfirst((string) $part),
$parts
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ArrayType;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -148,7 +149,8 @@ private function resolveDimFetchVariableNames(Closure|ArrowFunction $closureExpr
if ($arrayDimFetch->var instanceof Variable) {
$type = $this->nodeTypeResolver->getNativeType($arrayDimFetch->var);

if ($type->isString()->yes()) {
// skip string values
if (! $arrayDimFetch->dim instanceof String_ && ($type->isString()->yes() || $type->isString()->maybe())) {
continue;
}

Expand Down
51 changes: 51 additions & 0 deletions src/DependencyInjection/PHPStan/PHPStanContainerMemento.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Rector\DependencyInjection\PHPStan;

use PHPStan\DependencyInjection\MemoizingContainer;
use PHPStan\DependencyInjection\Nette\NetteContainer;
use PHPStan\Parser\AnonymousClassVisitor;
use PHPStan\Parser\RichParser;
use PHPStan\Parser\VariadicFunctionsVisitor;
use PHPStan\Parser\VariadicMethodsVisitor;
use Rector\Util\Reflection\PrivatesAccessor;

/**
* Helper service to modify PHPStan container
* 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');

// 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(VariadicFunctionsVisitor::class)[0] => true,
$container->findServiceNamesByType(VariadicMethodsVisitor::class)[0] => true,
];

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

$privatesAccessor->setPrivateProperty($deeperContainer, 'tags', $tags);
}
}
7 changes: 7 additions & 0 deletions src/PhpParser/Parser/RectorParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,22 @@
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use PHPStan\Parser\Parser;
use PHPStan\Parser\RichParser;
use Rector\DependencyInjection\PHPStan\PHPStanContainerMemento;
use Rector\PhpParser\ValueObject\StmtsAndTokens;
use Rector\Util\Reflection\PrivatesAccessor;

final readonly class RectorParser
{
/**
* @param RichParser $parser
*/
public function __construct(
private Parser $parser,
private PrivatesAccessor $privatesAccessor
) {

PHPStanContainerMemento::removeRichVisitors($parser);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions tests/PhpParser/Printer/Fixture/some_array_map.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<?php

namespace Rector\Tests\PhpParser\Printer\Fixture;

$result = array_map(array: [1, 2, 3], callback: fn(int $value) => $value);
$result = \array_map(array: [1, 2, 3], callback: fn(int $value) => $value);
5 changes: 5 additions & 0 deletions tests/PhpParser/Printer/PHPStanPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Parser\Parser;
use PHPStan\Parser\RichParser;
use Rector\DependencyInjection\PHPStan\PHPStanContainerMemento;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use ReflectionProperty;

/**
* Test case for: https://github.com/rectorphp/rector/issues/9492
* Most likely caused by https://github.com/phpstan/phpstan-src/pull/3763
*
* @see https://github.com/phpstan/phpstan-src/blob/2.1.x/src/Parser/ArrayMapArgVisitor.php
*/
final class PHPStanPrinterTest extends AbstractLazyTestCase
{
Expand All @@ -21,6 +24,8 @@ public function testAddingCommentOnSomeNodesFail(): void
/** @var RichParser $phpstanParser */
$phpstanParser = $this->make(Parser::class);

PHPStanContainerMemento::removeRichVisitors($phpstanParser);

$stmts = $phpstanParser->parseFile(__DIR__ . '/Fixture/some_array_map.php');

// get private property "parser"
Expand Down