Skip to content

Commit

Permalink
[Privatization] Skip magic method on PrivatizeFinalClassMethodRector (#…
Browse files Browse the repository at this point in the history
…6734)

* [Privatization] Skip magic method on PrivatizeFinalClassMethodRector

* [Privatization] Skip magic method on PrivatizeFinalClassMethodRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Feb 13, 2025
1 parent e1841ae commit 449e5ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector\Fixture;

/**
* both classes need to be in same fixture on purpose to reproduce the issue
*/
readonly abstract class A {
public static function a():A {
return new B();
}
}

final readonly class B extends A {
protected function __construct() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

if ($classMethod->isMagic()) {
return true;
}

// if has parent call, its probably overriding parent one → skip it
$hasParentCall = (bool) $this->betterNodeFinder->findFirst(
(array) $classMethod->stmts,
Expand Down
22 changes: 11 additions & 11 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,15 @@ public function create(): RectorConfig
$rectorConfig->import(__DIR__ . '/../../config/config.php');

$rectorConfig->singleton(Application::class, static function (Container $container): Application {
$application = $container->make(ConsoleApplication::class);
$consoleApplication = $container->make(ConsoleApplication::class);

$commandNamesToHide = ['list', 'completion', 'help', 'worker'];
foreach ($commandNamesToHide as $commandNameToHide) {
$commandToHide = $application->get($commandNameToHide);
$commandToHide = $consoleApplication->get($commandNameToHide);
$commandToHide->setHidden();
}

return $application;
return $consoleApplication;
});

$rectorConfig->when(ConsoleApplication::class)
Expand Down Expand Up @@ -618,8 +618,8 @@ static function (
ArrayAnnotationToAttributeMapper $arrayAnnotationToAttributeMapper,
Container $container
): void {
$annotationToAttributesMapper = $container->make(AnnotationToAttributeMapper::class);
$arrayAnnotationToAttributeMapper->autowire($annotationToAttributesMapper);
$annotationToAttributeMapper = $container->make(AnnotationToAttributeMapper::class);
$arrayAnnotationToAttributeMapper->autowire($annotationToAttributeMapper);
}
);

Expand Down Expand Up @@ -710,21 +710,21 @@ private function registerTagged(Container $container, array $classes, string $ta
private function createPHPStanServices(RectorConfig $rectorConfig): void
{
$rectorConfig->singleton(Parser::class, static function (Container $container) {
$phpstanServiceFactory = $container->make(PHPStanServicesFactory::class);
return $phpstanServiceFactory->createPHPStanParser();
$phpStanServicesFactory = $container->make(PHPStanServicesFactory::class);
return $phpStanServicesFactory->createPHPStanParser();
});

$rectorConfig->singleton(Lexer::class, static function (Container $container) {
$phpstanServiceFactory = $container->make(PHPStanServicesFactory::class);
return $phpstanServiceFactory->createEmulativeLexer();
$phpStanServicesFactory = $container->make(PHPStanServicesFactory::class);
return $phpStanServicesFactory->createEmulativeLexer();
});

foreach (self::PUBLIC_PHPSTAN_SERVICE_TYPES as $publicPhpstanServiceType) {
$rectorConfig->singleton($publicPhpstanServiceType, static function (Container $container) use (
$publicPhpstanServiceType
) {
$phpstanServiceFactory = $container->make(PHPStanServicesFactory::class);
return $phpstanServiceFactory->getByType($publicPhpstanServiceType);
$phpStanServicesFactory = $container->make(PHPStanServicesFactory::class);
return $phpStanServicesFactory->getByType($publicPhpstanServiceType);
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/functions/node_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ function print_node(Node | array $node): void
*/
function dump_node(Node|array $node): void
{
$symfonyStyle = Container::getInstance()
$rectorStyle = Container::getInstance()
->make(SymfonyStyleFactory::class)
->create();

// we turn up the verbosity so it's visible in tests overriding the
// default which is to be quite during tests
$symfonyStyle->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
$symfonyStyle->newLine();
$rectorStyle->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
$rectorStyle->newLine();

$nodePrinter = new NodePrinter($symfonyStyle);
$nodePrinter = new NodePrinter($rectorStyle);
$nodePrinter->printNodes($node);
}
}

0 comments on commit 449e5ff

Please sign in to comment.