Updated Rector to commit 18a2a0f37852ed6f5fb52c07e2c1137cf556f5b4

18a2a0f378 Add STRICT_BOOLEANS set (#4116)
This commit is contained in:
Tomas Votruba 2023-06-08 16:22:20 +00:00
parent 850512e46d
commit 7f654942fd
18 changed files with 58 additions and 135 deletions

View File

@ -0,0 +1,14 @@
<?php
declare (strict_types=1);
namespace RectorPrefix202306;
use Rector\Config\RectorConfig;
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector;
use Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([BooleanInBooleanNotRuleFixerRector::class, DisallowedEmptyRuleFixerRector::class, BooleanInIfConditionRuleFixerRector::class, BooleanInTernaryOperatorRuleFixerRector::class, DisallowedShortTernaryRuleFixerRector::class]);
};

View File

@ -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("<?php declare(strict_types = 1);\n\nreturn %s;", $exported));
$renameSuccess = @\rename($tmpPath, $path);
$renameSuccess = @\rename($tmpPath, $filePath);
if ($renameSuccess) {
return;
}
@\unlink($tmpPath);
if (\DIRECTORY_SEPARATOR === '/' || !\file_exists($path)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $path));
if (\DIRECTORY_SEPARATOR === '/' || !\file_exists($filePath)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}
}
public function clean(string $key) : void

View File

@ -10,5 +10,6 @@ final class CommentRemovingNodeTraverser extends NodeTraverser
public function __construct(CommentRemovingNodeVisitor $commentRemovingNodeVisitor)
{
$this->addVisitor($commentRemovingNodeVisitor);
parent::__construct();
}
}

View File

@ -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
*/

View File

@ -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;
}

View File

@ -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+"?

View File

@ -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);
}
}

View File

@ -1,100 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Strict\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\NodeManipulator\Dependency\DependencyClassMethodDecorator;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Fixer Rector for PHPStan rule:
* https://github.com/phpstan/phpstan-strict-rules/blob/b7dd96a5503919a43b3cd06a2dced9d4252492f2/src/Rules/Classes/RequireParentConstructCallRule.php
*
* @see \Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\AddConstructorParentCallRectorTest
*/
final class AddConstructorParentCallRector extends AbstractScopeAwareRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\Dependency\DependencyClassMethodDecorator
*/
private $dependencyClassMethodDecorator;
public function __construct(DependencyClassMethodDecorator $dependencyClassMethodDecorator)
{
$this->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<class-string<Node>>
*/
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);
});
}
}

View File

@ -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')])]);

View File

@ -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
*/

View File

@ -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
{

View File

@ -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;

View File

@ -19,6 +19,7 @@ final class FileWithoutNamespaceNodeTraverser extends NodeTraverser
public function __construct(NodeFinder $nodeFinder)
{
$this->nodeFinder = $nodeFinder;
parent::__construct();
}
/**
* @template TNode as Node

View File

@ -30,6 +30,7 @@ final class RectorNodeTraverser extends NodeTraverser
{
$this->phpRectors = $phpRectors;
$this->phpVersionedFilter = $phpVersionedFilter;
parent::__construct();
}
/**
* @template TNode as Node

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit06a25b354321cf0b16cf6435b43f4254::getLoader();
return ComposerAutoloaderInit039be9eb7fad82232ccfbd1567a3b09a::getLoader();

View File

@ -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',

View File

@ -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;

View File

@ -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);
}