diff --git a/config/set/strict-booleans.php b/config/set/strict-booleans.php new file mode 100644 index 00000000000..86f731a5cdc --- /dev/null +++ b/config/set/strict-booleans.php @@ -0,0 +1,14 @@ +rules([BooleanInBooleanNotRuleFixerRector::class, DisallowedEmptyRuleFixerRector::class, BooleanInIfConditionRuleFixerRector::class, BooleanInTernaryOperatorRuleFixerRector::class, DisallowedShortTernaryRuleFixerRector::class]); +}; diff --git a/packages/Caching/ValueObject/Storage/FileCacheStorage.php b/packages/Caching/ValueObject/Storage/FileCacheStorage.php index ae744c30848..39d764c48ff 100644 --- a/packages/Caching/ValueObject/Storage/FileCacheStorage.php +++ b/packages/Caching/ValueObject/Storage/FileCacheStorage.php @@ -17,10 +17,12 @@ use Rector\Core\Exception\Cache\CachingException; final class FileCacheStorage implements CacheStorageInterface { /** + * @readonly * @var string */ private $directory; /** + * @readonly * @var \Symfony\Component\Filesystem\Filesystem */ private $filesystem; @@ -55,7 +57,7 @@ final class FileCacheStorage implements CacheStorageInterface $cacheFilePaths = $this->getCacheFilePaths($key); $this->filesystem->mkdir($cacheFilePaths->getFirstDirectory()); $this->filesystem->mkdir($cacheFilePaths->getSecondDirectory()); - $path = $cacheFilePaths->getFilePath(); + $filePath = $cacheFilePaths->getFilePath(); $tmpPath = \sprintf('%s/%s.tmp', $this->directory, Random::generate()); $errorBefore = \error_get_last(); $exported = @\var_export(new CacheItem($variableKey, $data), \true); @@ -65,13 +67,13 @@ final class FileCacheStorage implements CacheStorageInterface } // for performance reasons we don't use SmartFileSystem FileSystem::write($tmpPath, \sprintf("addVisitor($commentRemovingNodeVisitor); + parent::__construct(); } } diff --git a/packages/Set/ValueObject/SetList.php b/packages/Set/ValueObject/SetList.php index 034083ffa43..7c7904e2b11 100644 --- a/packages/Set/ValueObject/SetList.php +++ b/packages/Set/ValueObject/SetList.php @@ -21,6 +21,10 @@ final class SetList implements SetListInterface * @var string */ public const DEAD_CODE = __DIR__ . '/../../../config/set/dead-code.php'; + /** + * @var string + */ + public const STRICT_BOOLEANS = __DIR__ . '/../../../config/set/strict-booleans.php'; /** * @var string */ diff --git a/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php b/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php index d3134328e50..f8a5dbe7181 100644 --- a/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php +++ b/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php @@ -15,11 +15,11 @@ trait MovingFilesTrait { $addedFilePathsWithContents = $this->resolveAddedFilePathsWithContents(); $wasFound = \false; - foreach ($addedFilePathsWithContents as $addedFilePathsWithContent) { - if ($addedFilePathsWithContent->getFilePath() !== $expectedFilePath) { + foreach ($addedFilePathsWithContents as $addedFilePathWithContent) { + if ($addedFilePathWithContent->getFilePath() !== $expectedFilePath) { continue; } - $this->assertSame($expectedFileContents, $addedFilePathsWithContent->getFileContent()); + $this->assertSame($expectedFileContents, $addedFilePathWithContent->getFileContent()); $wasFound = \true; } if ($wasFound === \false) { @@ -37,9 +37,9 @@ trait MovingFilesTrait if ($addedFilesWithNodes === []) { return $addedFilePathsWithContents; } - foreach ($addedFilesWithNodes as $addedFileWithNodes) { - $fileContent = $nodesWithFileDestinationPrinter->printNodesWithFileDestination($addedFileWithNodes); - $addedFilePathsWithContents[] = new AddedFileWithContent($addedFileWithNodes->getFilePath(), $fileContent); + foreach ($addedFilesWithNodes as $addedFileWithNode) { + $fileContent = $nodesWithFileDestinationPrinter->printNodesWithFileDestination($addedFileWithNode); + $addedFilePathsWithContents[] = new AddedFileWithContent($addedFileWithNode->getFilePath(), $fileContent); } return $addedFilePathsWithContents; } diff --git a/rules/Naming/Naming/PropertyNaming.php b/rules/Naming/Naming/PropertyNaming.php index a6ffb2e0adf..c86d1e43c7f 100644 --- a/rules/Naming/Naming/PropertyNaming.php +++ b/rules/Naming/Naming/PropertyNaming.php @@ -184,11 +184,13 @@ final class PropertyNaming private function removeInterfaceSuffixPrefix(string $className, string $category) : string { // suffix - if (Strings::match($className, '#' . $category . '$#i')) { + $iSuffixMatch = Strings::match($className, '#' . $category . '$#i'); + if ($iSuffixMatch !== null) { return Strings::substring($className, 0, -\strlen($category)); } // prefix - if (Strings::match($className, '#^' . $category . '#i')) { + $iPrefixMatch = Strings::match($className, '#^' . $category . '#i'); + if ($iPrefixMatch !== null) { return Strings::substring($className, \strlen($category)); } // starts with "I\W+"? diff --git a/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php b/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php index b274254df81..321055a6315 100644 --- a/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php +++ b/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php @@ -72,6 +72,9 @@ CODE_SAMPLE public function refactorWithScope(Node $node, Scope $scope) : ?Expr { $exprType = $scope->getType($node->expr); + if ($exprType->isBoolean()->yes()) { + return null; + } return $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $node->expr, $this->treatAsNonEmpty); } } diff --git a/rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php b/rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php deleted file mode 100644 index bc2b2bfbaa4..00000000000 --- a/rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php +++ /dev/null @@ -1,100 +0,0 @@ -dependencyClassMethodDecorator = $dependencyClassMethodDecorator; - } - public function getRuleDefinition() : RuleDefinition - { - $errorMessage = \sprintf('Fixer for PHPStan reports by strict type rule - "%s"', 'PHPStan\\Rules\\Classes\\RequireParentConstructCallRule'); - return new RuleDefinition($errorMessage, [new CodeSample(<<<'CODE_SAMPLE' -class SunshineCommand extends ParentClassWithConstructor -{ - public function __construct() - { - $value = 5; - } -} -CODE_SAMPLE -, <<<'CODE_SAMPLE' -class SunshineCommand extends ParentClassWithConstructor -{ - public function __construct(ParentDependency $parentDependency) - { - $value = 5; - - parent::__construct($parentDependency); - } -} -CODE_SAMPLE -)]); - } - /** - * @return array> - */ - public function getNodeTypes() : array - { - return [ClassMethod::class]; - } - /** - * @param ClassMethod $node - */ - public function refactorWithScope(Node $node, Scope $scope) : ?Node - { - if (!$this->isName($node, MethodName::CONSTRUCT)) { - return null; - } - $classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class); - if (!$classLike instanceof Class_) { - return null; - } - if ($this->hasParentCallOfMethod($node)) { - return null; - } - $this->dependencyClassMethodDecorator->decorateConstructorWithParentDependencies($classLike, $node, $scope); - return $node; - } - /** - * Looks for "parent::__construct" - */ - private function hasParentCallOfMethod(ClassMethod $classMethod) : bool - { - return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node) : bool { - if (!$node instanceof StaticCall) { - return \false; - } - if (!$this->isName($node->class, ObjectReference::PARENT)) { - return \false; - } - return $this->isName($node->name, MethodName::CONSTRUCT); - }); - } -} diff --git a/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php b/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php index 75c166dd365..57d989720e0 100644 --- a/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php +++ b/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php @@ -22,13 +22,6 @@ final class StaticCallToFuncCallRector extends AbstractRector implements Configu * @var StaticCallToFuncCall[] */ private $staticCallsToFunctions = []; - /** - * @param StaticCallToFuncCall[] $staticCallsToFunctions - */ - public function __construct(array $staticCallsToFunctions = []) - { - $this->staticCallsToFunctions = $staticCallsToFunctions; - } public function getRuleDefinition() : RuleDefinition { return new RuleDefinition('Turns static call to function call.', [new ConfiguredCodeSample('OldClass::oldMethod("args");', 'new_function("args");', [new StaticCallToFuncCall('OldClass', 'oldMethod', 'new_function')])]); diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index d034cf6865e..ccc9a9893ba 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '4457cfad19971c45ff953b238f456590bb6fd67f'; + public const PACKAGE_VERSION = '18a2a0f37852ed6f5fb52c07e2c1137cf556f5b4'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-06-08 15:34:21'; + public const RELEASE_DATE = '2023-06-08 16:18:19'; /** * @var int */ diff --git a/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php b/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php index f38e3511a65..9f41e88f4e5 100644 --- a/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php +++ b/src/DependencyInjection/CompilerPass/AutowireArrayParameterCompilerPass.php @@ -16,6 +16,8 @@ use RectorPrefix202306\Symfony\Component\DependencyInjection\ContainerBuilder; use RectorPrefix202306\Symfony\Component\DependencyInjection\Definition; use RectorPrefix202306\Symfony\Component\DependencyInjection\Reference; /** + * @deprecated Make the required services explicit, for faster autowire + * * @inspiration https://github.com/nette/di/pull/178 * @see \Rector\Core\Tests\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPassTest */ @@ -85,7 +87,8 @@ final class AutowireArrayParameterCompilerPass implements CompilerPassInterface $resolvedClassName = $parameterBag->resolveValue($definition->getClass()); // skip 3rd party classes, they're autowired by own config $excludedNamespacePattern = '#^(' . \implode('|', self::EXCLUDED_NAMESPACES) . ')\\\\#'; - if (Strings::match($resolvedClassName, $excludedNamespacePattern)) { + $excludedNamespaceMatch = Strings::match($resolvedClassName, $excludedNamespacePattern); + if ($excludedNamespaceMatch !== null) { return \true; } if (\in_array($resolvedClassName, self::EXCLUDED_FATAL_CLASSES, \true)) { @@ -106,7 +109,7 @@ final class AutowireArrayParameterCompilerPass implements CompilerPassInterface } /** @var ReflectionMethod $constructorReflectionMethod */ $constructorReflectionMethod = $reflectionClass->getConstructor(); - return !$constructorReflectionMethod->getParameters(); + return $constructorReflectionMethod->getParameters() === []; } private function processParameters(ContainerBuilder $containerBuilder, ReflectionMethod $reflectionMethod, Definition $definition) : void { diff --git a/src/DependencyInjection/DefinitionFinder.php b/src/DependencyInjection/DefinitionFinder.php index ccdbe034b2e..0921ea95cb0 100644 --- a/src/DependencyInjection/DefinitionFinder.php +++ b/src/DependencyInjection/DefinitionFinder.php @@ -21,6 +21,7 @@ final class DefinitionFinder $definitions = []; $containerBuilderDefinitions = $containerBuilder->getDefinitions(); foreach ($containerBuilderDefinitions as $name => $definition) { + /** @var Definition $definition */ $class = $definition->getClass() ?: $name; if (!$this->doesClassExists($class)) { continue; diff --git a/src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php b/src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php index 3a7a50b3198..c1b33c254f0 100644 --- a/src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php +++ b/src/PhpParser/NodeTraverser/FileWithoutNamespaceNodeTraverser.php @@ -19,6 +19,7 @@ final class FileWithoutNamespaceNodeTraverser extends NodeTraverser public function __construct(NodeFinder $nodeFinder) { $this->nodeFinder = $nodeFinder; + parent::__construct(); } /** * @template TNode as Node diff --git a/src/PhpParser/NodeTraverser/RectorNodeTraverser.php b/src/PhpParser/NodeTraverser/RectorNodeTraverser.php index ac683abf3f1..6c8e8cec843 100644 --- a/src/PhpParser/NodeTraverser/RectorNodeTraverser.php +++ b/src/PhpParser/NodeTraverser/RectorNodeTraverser.php @@ -30,6 +30,7 @@ final class RectorNodeTraverser extends NodeTraverser { $this->phpRectors = $phpRectors; $this->phpVersionedFilter = $phpVersionedFilter; + parent::__construct(); } /** * @template TNode as Node diff --git a/vendor/autoload.php b/vendor/autoload.php index b10280be75c..fc2bfcb3652 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit06a25b354321cf0b16cf6435b43f4254::getLoader(); +return ComposerAutoloaderInit039be9eb7fad82232ccfbd1567a3b09a::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index b9e7ed71312..8ad12393845 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2568,7 +2568,6 @@ return array( 'Rector\\Strict\\NodeFactory\\ExactCompareFactory' => $baseDir . '/rules/Strict/NodeFactory/ExactCompareFactory.php', 'Rector\\Strict\\Rector\\AbstractFalsyScalarRuleFixerRector' => $baseDir . '/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php', 'Rector\\Strict\\Rector\\BooleanNot\\BooleanInBooleanNotRuleFixerRector' => $baseDir . '/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php', - 'Rector\\Strict\\Rector\\ClassMethod\\AddConstructorParentCallRector' => $baseDir . '/rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php', 'Rector\\Strict\\Rector\\Empty_\\DisallowedEmptyRuleFixerRector' => $baseDir . '/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php', 'Rector\\Strict\\Rector\\If_\\BooleanInIfConditionRuleFixerRector' => $baseDir . '/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php', 'Rector\\Strict\\Rector\\Ternary\\BooleanInTernaryOperatorRuleFixerRector' => $baseDir . '/rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 1a7140955ec..9e562878127 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit06a25b354321cf0b16cf6435b43f4254 +class ComposerAutoloaderInit039be9eb7fad82232ccfbd1567a3b09a { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit06a25b354321cf0b16cf6435b43f4254 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit06a25b354321cf0b16cf6435b43f4254', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit039be9eb7fad82232ccfbd1567a3b09a', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit06a25b354321cf0b16cf6435b43f4254', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit039be9eb7fad82232ccfbd1567a3b09a', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit06a25b354321cf0b16cf6435b43f4254::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit039be9eb7fad82232ccfbd1567a3b09a::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit06a25b354321cf0b16cf6435b43f4254::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit039be9eb7fad82232ccfbd1567a3b09a::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 6d4d20e1d4d..9c4cc419383 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit06a25b354321cf0b16cf6435b43f4254 +class ComposerStaticInit039be9eb7fad82232ccfbd1567a3b09a { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -2819,7 +2819,6 @@ class ComposerStaticInit06a25b354321cf0b16cf6435b43f4254 'Rector\\Strict\\NodeFactory\\ExactCompareFactory' => __DIR__ . '/../..' . '/rules/Strict/NodeFactory/ExactCompareFactory.php', 'Rector\\Strict\\Rector\\AbstractFalsyScalarRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/AbstractFalsyScalarRuleFixerRector.php', 'Rector\\Strict\\Rector\\BooleanNot\\BooleanInBooleanNotRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/BooleanNot/BooleanInBooleanNotRuleFixerRector.php', - 'Rector\\Strict\\Rector\\ClassMethod\\AddConstructorParentCallRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php', 'Rector\\Strict\\Rector\\Empty_\\DisallowedEmptyRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php', 'Rector\\Strict\\Rector\\If_\\BooleanInIfConditionRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/If_/BooleanInIfConditionRuleFixerRector.php', 'Rector\\Strict\\Rector\\Ternary\\BooleanInTernaryOperatorRuleFixerRector' => __DIR__ . '/../..' . '/rules/Strict/Rector/Ternary/BooleanInTernaryOperatorRuleFixerRector.php', @@ -3181,9 +3180,9 @@ class ComposerStaticInit06a25b354321cf0b16cf6435b43f4254 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit06a25b354321cf0b16cf6435b43f4254::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit06a25b354321cf0b16cf6435b43f4254::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit06a25b354321cf0b16cf6435b43f4254::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit039be9eb7fad82232ccfbd1567a3b09a::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit039be9eb7fad82232ccfbd1567a3b09a::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit039be9eb7fad82232ccfbd1567a3b09a::$classMap; }, null, ClassLoader::class); }