diff --git a/composer.json b/composer.json index 6c071235f..3a75783ee 100644 --- a/composer.json +++ b/composer.json @@ -33,15 +33,15 @@ "autoload": { "psr-4": { "Rector\\SwissKnife\\": "src" - }, - "classmap": [ - "stubs" - ] + } }, "autoload-dev": { "psr-4": { "Rector\\SwissKnife\\Tests\\": "tests" - } + }, + "classmap": [ + "stubs" + ] }, "config": { "sort-packages": true, diff --git a/phpstan.neon b/phpstan.neon index 8d4af5e8e..5c21c1c60 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,10 +9,11 @@ parameters: errorFormat: symplify # see https://github.com/symplify/phpstan-rules + symplify: + ctor: true symfonyReturnType: true laravelReturnType: true pathStrings: true - ctor: true excludePaths: - */Fixture/* @@ -43,4 +44,3 @@ parameters: - identifier: public.method.unused message: '#Public method "Rector\\SwissKnife\\(.*?)Command\:\:run\(\)" is never used#' - diff --git a/src/Command/FinalizeClassesCommand.php b/src/Command/FinalizeClassesCommand.php index 2b708adea..70001c1e6 100644 --- a/src/Command/FinalizeClassesCommand.php +++ b/src/Command/FinalizeClassesCommand.php @@ -6,7 +6,6 @@ use Entropy\Console\Contract\CommandInterface; use Entropy\Console\Enum\ExitCode; -use Entropy\Console\Output\OutputColorizer; use Entropy\Console\Output\OutputPrinter; use Entropy\Console\Output\ProgressBar; use Nette\Utils\FileSystem; @@ -28,7 +27,6 @@ public function __construct( private OutputPrinter $outputPrinter, - private OutputColorizer $outputColorizer, private ParentClassResolver $parentClassResolver, private EntityClassResolver $entityClassResolver, private CachedPhpParser $cachedPhpParser, @@ -59,7 +57,7 @@ public function run( // double to count for both parent and entity resolver $stepRatio = $skipMocked ? 3 : 2; - $progressBar = new ProgressBar($this->outputColorizer); + $progressBar = new ProgressBar(); $progressBar->start($stepRatio * count($phpFileInfos)); } diff --git a/src/Command/GenerateSymfonySmokeTestsCommand.php b/src/Command/GenerateSymfonySmokeTestsCommand.php index 277a245c3..f59b5bd3e 100644 --- a/src/Command/GenerateSymfonySmokeTestsCommand.php +++ b/src/Command/GenerateSymfonySmokeTestsCommand.php @@ -13,6 +13,7 @@ use Rector\SwissKnife\SmokeTestgen\Templating\TemplateDecorator; use Rector\SwissKnife\SmokeTestgen\TestTemplateResolver; use Rector\SwissKnife\SmokeTestgen\Utils\TestPathResolver; +use RuntimeException; use Webmozart\Assert\Assert; final readonly class GenerateSymfonySmokeTestsCommand implements CommandInterface @@ -42,10 +43,15 @@ public function run(): int { $this->outputPrinter->writeln('Resolving directory for smoke tests'); - $smokeTestsDirectory = $this->testsDirectoryResolver->resolveSmokeUnitTestDirectory(getcwd()); + $projectDirectory = getcwd(); + if (! is_string($projectDirectory)) { + throw new RuntimeException('Current working directory could not be resolved.'); + } + + $smokeTestsDirectory = $this->testsDirectoryResolver->resolveSmokeUnitTestDirectory($projectDirectory); $this->outputPrinter->writeln(' * ' . $smokeTestsDirectory); - $requirePackages = $this->resolveProjectRequiredPackageNames(getcwd()); + $requirePackages = $this->resolveProjectRequiredPackageNames($projectDirectory); $testByPackageSubscribers = $this->testTemplateResolver->matchProjectPackages($requirePackages); if ($testByPackageSubscribers === []) { diff --git a/src/Command/PrivatizeConstantsCommand.php b/src/Command/PrivatizeConstantsCommand.php index 3e3cea17d..e53d911b6 100644 --- a/src/Command/PrivatizeConstantsCommand.php +++ b/src/Command/PrivatizeConstantsCommand.php @@ -6,7 +6,6 @@ use Entropy\Console\Contract\CommandInterface; use Entropy\Console\Enum\ExitCode; -use Entropy\Console\Output\OutputColorizer; use Entropy\Console\Output\OutputPrinter; use Entropy\Console\Output\ProgressBar; use Nette\Utils\FileSystem; @@ -26,7 +25,6 @@ { public function __construct( private OutputPrinter $outputPrinter, - private OutputColorizer $outputColorizer, private ClassConstantFetchFinder $classConstantFetchFinder, private ClassConstFinder $classConstFinder, private TwigTemplateConstantExtractor $twigTemplateConstantExtractor, @@ -66,7 +64,7 @@ public function run( $this->outputPrinter->title('Finding class const fetches...'); - $progressBar = new ProgressBar($this->outputColorizer); + $progressBar = new ProgressBar(); $progressBar->start(count($phpFileInfos)); $phpClassConstantFetches = $this->classConstantFetchFinder->find($phpFileInfos, $progressBar, $isDebug); diff --git a/src/Command/SearchRegexCommand.php b/src/Command/SearchRegexCommand.php index afded14aa..f0e279d07 100644 --- a/src/Command/SearchRegexCommand.php +++ b/src/Command/SearchRegexCommand.php @@ -6,18 +6,17 @@ use Entropy\Console\Contract\CommandInterface; use Entropy\Console\Enum\ExitCode; -use Entropy\Console\Output\OutputColorizer; use Entropy\Console\Output\OutputPrinter; use Entropy\Console\Output\ProgressBar; use Nette\Utils\Strings; use Rector\SwissKnife\Finder\PhpFilesFinder; +use RuntimeException; use Webmozart\Assert\Assert; final readonly class SearchRegexCommand implements CommandInterface { public function __construct( private OutputPrinter $outputPrinter, - private OutputColorizer $outputColorizer, ) { } @@ -31,6 +30,9 @@ public function run(string $regex, ?string $projectDirectory = null): int { if ($projectDirectory === null) { $projectDirectory = getcwd(); + if (! is_string($projectDirectory)) { + throw new RuntimeException('Current working directory could not be resolved.'); + } } Assert::directory($projectDirectory); @@ -46,7 +48,7 @@ public function run(string $regex, ?string $projectDirectory = null): int $foundCasesCount = 0; $markedFiles = []; - $progressBar = new ProgressBar($this->outputColorizer); + $progressBar = new ProgressBar(); $progressBar->start(count($phpFileInfos)); foreach ($phpFileInfos as $phpFileInfo) { diff --git a/src/PhpParser/NodeVisitor/FindNonPrivateClassConstNodeVisitor.php b/src/PhpParser/NodeVisitor/FindNonPrivateClassConstNodeVisitor.php index 4d617fd89..1b7890abe 100644 --- a/src/PhpParser/NodeVisitor/FindNonPrivateClassConstNodeVisitor.php +++ b/src/PhpParser/NodeVisitor/FindNonPrivateClassConstNodeVisitor.php @@ -68,14 +68,10 @@ private function isConstantDefinedInParentClassAlso(Class_ $class, string $const return in_array($constantName, $this->getClassConstantNames($parentClassName), true); } } - - foreach ($class->implements as $implement) { - if (in_array($constantName, $this->getClassConstantNames($implement->toString()), true)) { - return true; - } - } - - return false; + return array_any( + $class->implements, + fn ($implement): bool => in_array($constantName, $this->getClassConstantNames($implement->toString()), true) + ); } /** diff --git a/src/Testing/UnitTestFilter.php b/src/Testing/UnitTestFilter.php index 65b58ed7b..79ad0faec 100644 --- a/src/Testing/UnitTestFilter.php +++ b/src/Testing/UnitTestFilter.php @@ -30,14 +30,9 @@ private function isUnitTest(string $class): bool if (! is_a($class, 'PHPUnit\Framework\TestCase', true) && ! is_a($class, 'PHPUnit_Framework_TestCase', true)) { return false; } - - foreach (self::NON_UNIT_TEST_CASE_CLASSES as $nonUnitTestCaseClass) { - // required special behavior - if (is_a($class, $nonUnitTestCaseClass, true)) { - return false; - } - } - - return true; + return array_all( + self::NON_UNIT_TEST_CASE_CLASSES, + fn ($nonUnitTestCaseClass): bool => ! is_a($class, $nonUnitTestCaseClass, true) + ); } } diff --git a/tests/PhpParser/Finder/ClassConstantFetchFinder/ClassConstantFetchFinderTest.php b/tests/PhpParser/Finder/ClassConstantFetchFinder/ClassConstantFetchFinderTest.php index f2e8a2de7..d50551ced 100644 --- a/tests/PhpParser/Finder/ClassConstantFetchFinder/ClassConstantFetchFinderTest.php +++ b/tests/PhpParser/Finder/ClassConstantFetchFinder/ClassConstantFetchFinderTest.php @@ -4,7 +4,6 @@ namespace Rector\SwissKnife\Tests\PhpParser\Finder\ClassConstantFetchFinder; -use Entropy\Console\Output\OutputColorizer; use Entropy\Console\Output\ProgressBar; use Override; use Rector\SwissKnife\Contract\ClassConstantFetchInterface; @@ -53,7 +52,7 @@ public function testParseError(): void ); $directory = __DIR__ . '/Fixture/Error'; - $progressBar = new ProgressBar(new OutputColorizer()); + $progressBar = new ProgressBar(); $fileInfos = PhpFilesFinder::find([$directory]); $this->classConstantFetchFinder->find($fileInfos, $progressBar, false); } @@ -63,7 +62,7 @@ public function testParseError(): void */ private function findInDirectory(string $directory): array { - $progressBar = new ProgressBar(new OutputColorizer()); + $progressBar = new ProgressBar(); $fileInfos = PhpFilesFinder::find([$directory]); return $this->classConstantFetchFinder->find($fileInfos, $progressBar, false); diff --git a/tests/PhpParser/NodeVisitor/MockedClassNameCollectingNodeVisitor/MockedClassNameCollectingNodeVisitorTest.php b/tests/PhpParser/NodeVisitor/MockedClassNameCollectingNodeVisitor/MockedClassNameCollectingNodeVisitorTest.php index 9f645f6ee..bd6ba62cc 100644 --- a/tests/PhpParser/NodeVisitor/MockedClassNameCollectingNodeVisitor/MockedClassNameCollectingNodeVisitorTest.php +++ b/tests/PhpParser/NodeVisitor/MockedClassNameCollectingNodeVisitor/MockedClassNameCollectingNodeVisitorTest.php @@ -24,7 +24,8 @@ public function test(string $filePath, array $expectedClassNames): void $nodeTraverser = new NodeTraverser(); $nodeTraverser->addVisitor($mockedClassNameCollectingNodeVisitor); - $parser = (new ParserFactory())->createForNewestSupportedVersion(); + $parser = new ParserFactory() + ->createForNewestSupportedVersion(); $stmts = $parser->parse((string) file_get_contents($filePath)); $this->assertNotNull($stmts);