[Downgrade] Drop SAFE_TYPES and SAFE_TYPES_TO_METHODS constants, use UNSAFE_TYPES_TO_METHODS instead (#1449)

This commit is contained in:
Tomas Votruba 2021-12-10 14:50:46 +01:00 committed by GitHub
parent 5d45598fbe
commit b4fabbdeba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 8 additions and 611 deletions

View File

@ -6,6 +6,7 @@ use Rector\Core\Configuration\Option;
use Rector\Core\Stubs\PHPStanStubLoader;
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@ -30,9 +31,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DowngradeParameterTypeWideningRector::class)
->configure([
DowngradeParameterTypeWideningRector::UNSAFE_TYPES_TO_METHODS => [
LoaderInterface::class => [
'load'
],
LoaderInterface::class => ['load'],
Loader::class => ['import']
],
]);
};

View File

@ -4594,8 +4594,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DowngradeParameterTypeWideningRector::class)
->configure([
DowngradeParameterTypeWideningRector::SAFE_TYPES => [],
DowngradeParameterTypeWideningRector::SAFE_TYPES_TO_METHODS => [],
DowngradeParameterTypeWideningRector::UNSAFE_TYPES_TO_METHODS => [],
]);
};
```

View File

@ -130,23 +130,6 @@ parameters:
- '#Use another value object over array with string\-keys and objects, array<string, ValueObject\>#'
- '#Do not use factory/method call in constructor\. Put factory in config and get service with dependency injection#'
-
message: '#"(getComments|getDocComment|setDocComment)\(\)" call on "PhpParser\\Node" type is not allowed#'
paths:
- src/PhpParser/NodeTransformer.php
- src/Rector/AbstractRector.php
- src/Exclusion/ExclusionManager.php
# playing around with doc block format
- packages/Comments/CommentRemover.php
- rules/CodingStyle/Rector/Assign/PHPStormVarAnnotationRector.php
- rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php
- rules/DeadCode/Rector/Expression/RemoveDeadStmtRector.php
- rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php
- rules/PhpSpecToPHPUnit/Rector/MethodCall/PhpSpecMocksToPHPUnitMocksRector.php
- packages/BetterPhpDocParser/Comment/CommentsMerger.php
- packages/Comments/NodeDocBlock/DocBlockUpdater.php
- packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
# false positives checked in another method
-
message: '#If condition is always false#'
@ -237,11 +220,6 @@ parameters:
- '#Method Rector\\Core\\PhpParser\\Node\\BetterNodeFinder\:\:findParentType\(\) should return T of PhpParser\\Node\|null but returns class\-string<T of PhpParser\\Node\>\|T of PhpParser\\Node#'
-
message: '#"%s" in sprintf\(\) format must be quoted#'
paths:
- packages/BetterPhpDocParser/ValueObject/PhpDoc/VariadicAwareParamTagValueNode.php
- '#Property Rector\\Core\\PhpParser\\Node\\AssignAndBinaryMap\:\:\$binaryOpToAssignClasses \(array<class\-string<PhpParser\\Node\\Expr\\BinaryOp\>, class\-string<PhpParser\\Node\\Expr\\BinaryOp\>\>\) does not accept array#'
-
@ -252,7 +230,6 @@ parameters:
- packages/NodeNameResolver/NodeNameResolver.php
- packages/NodeNameResolver/NodeNameResolver/ClassNameResolver.php
- packages/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
- src/Validation/Collector/EmptyConfigurableRectorCollector.php
# known types
- '#Call to an undefined method PHPStan\\Type\\ConstantType\:\:getValue\(\)#'
@ -383,10 +360,6 @@ parameters:
message: '#"@\\rename\(\$tmpPath, \$path\)" is forbidden to use#'
paths:
- packages/Caching/ValueObject/Storage/FileCacheStorage.php
-
message: '#"%s" in sprintf\(\) format must be quoted#'
paths:
- packages/Caching/ValueObject/Storage/FileCacheStorage.php
-
message: '#"@\\var_export\(new \\Rector\\Caching\\ValueObject\\CacheItem\(\$variableKey, \$data\), true\)" is forbidden to use#'
paths:
@ -601,10 +574,6 @@ parameters:
- src/Application/*Processor.php
- rules/Composer/Application/FileProcessor/ComposerFileProcessor.php
-
message: '#Method Rector\\Core\\Application\\ApplicationFileProcessor\:\:processFiles\(\) should return array\{system_errors\: array<Rector\\Core\\ValueObject\\Application\\SystemError\>, file_diffs\: array<Rector\\Core\\ValueObject\\Reporting\\FileDiff\>\} but returns array#'
path: src/Application/ApplicationFileProcessor.php
- '#Call to function property_exists\(\) with PhpParser\\Node\\Stmt\\ClassLike and (.*?) will always evaluate to true#'
# code sample configuration
@ -616,24 +585,8 @@ parameters:
- rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php
- rules/Transform/Rector/Assign/PropertyFetchToMethodCallRector.php
# fixed in symplify dev-main
-
message: '#Do not use chained method calls\. Put each on separated lines#'
paths:
- rules/Privatization/Naming/ConstantNaming.php
- rules/Naming/Naming/VariableNaming.php
# skipped on purpose, as ctor overrie
- '#Rector\\StaticTypeMapper\\ValueObject\\Type\\SimpleStaticType\:\:__construct\(\) does not call parent constructor from PHPStan\\Type\\StaticType#'
# complex detection
- '#Cognitive complexity for "Rector\\Core\\DependencyInjection\\Collector\\ConfigureCallValuesCollector\:\:addConfigureCallValues\(\)" is \d+, keep it under 10#'
# will be removed soon
-
message: '#Class cognitive complexity is 31, keep it under 30#'
path: rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php
-
message: '#Cognitive complexity for "Rector\\DowngradePhp72\\Rector\\ClassMethod\\DowngradeParameterTypeWideningRector\:\:isSafeType\(\)" is 14, keep it under 10#'
path: rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php

View File

@ -1,55 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
class SomeClass
{
public function hello(string $world = 'world') {
printf('Hello %s', $world);
}
}
class SomeOtherClassUsingAnAnonymousClass
{
public function doSomething(): void
{
$class = new class () extends SomeClass {
public function hello(string $world = 'world') {
printf('Hi %s', $world);
}
};
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
class SomeClass
{
/**
* @param string $world
*/
public function hello($world = 'world') {
printf('Hello %s', $world);
}
}
class SomeOtherClassUsingAnAnonymousClass
{
public function doSomething(): void
{
$class = new class () extends SomeClass {
/**
* @param string $world
*/
public function hello($world = 'world') {
printf('Hi %s', $world);
}
};
}
}
?>

View File

@ -1,65 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface SomeInterface
{
public function hello(string $world = 'world');
}
class SomeClass2
{
public function doSomething(): void
{
$class = new class (function () {
return $this->doSomethingElse();
}) implements SomeInterface {
public function hello(string $world = 'world') {
printf('Hi %s', $world);
}
};
}
public function doSomethingElse(): void
{
print('Hello again');
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface SomeInterface
{
/**
* @param string $world
*/
public function hello($world = 'world');
}
class SomeClass2
{
public function doSomething(): void
{
$class = new class (function () {
return $this->doSomethingElse();
}) implements SomeInterface {
/**
* @param string $world
*/
public function hello($world = 'world') {
printf('Hi %s', $world);
}
};
}
public function doSomethingElse(): void
{
print('Hello again');
}
}
?>

View File

@ -1,57 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface SomeInterface2
{
public function test(\SplFileInfo $file);
}
abstract class SomeAbstractClass implements SomeInterface2
{
public function test2(\SplFileInfo $file, string $world = 'world') {
printf('Hello %s', $world);
}
}
return new class extends SomeAbstractClass {
public function test(\SplFileInfo $file) {
$this->test2($file);
}
};
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface SomeInterface2
{
/**
* @param \SplFileInfo $file
*/
public function test($file);
}
abstract class SomeAbstractClass implements SomeInterface2
{
/**
* @param \SplFileInfo $file
* @param string $world
*/
public function test2($file, $world = 'world') {
printf('Hello %s', $world);
}
}
return new class extends SomeAbstractClass {
/**
* @param \SplFileInfo $file
*/
public function test($file) {
$this->test2($file);
}
};
?>

View File

@ -1,55 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface A
{
public function test(array $input);
}
class B implements A
{
public function test($input)
{
}
}
final class MostChild implements A
{
public function test(array $input)
{
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface A
{
/**
* @param mixed[] $input
*/
public function test($input);
}
class B implements A
{
public function test($input)
{
}
}
final class MostChild implements A
{
/**
* @param mixed[] $input
*/
public function test($input)
{
}
}
?>

View File

@ -1,42 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\AnotherContainerInterface;
final class YetAnotherContainer implements AnotherContainerInterface
{
use AnotherServiceLocatorTrait;
}
trait AnotherServiceLocatorTrait
{
public function get(string $name)
{
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Source\AnotherContainerInterface;
final class YetAnotherContainer implements AnotherContainerInterface
{
use AnotherServiceLocatorTrait;
}
trait AnotherServiceLocatorTrait
{
/**
* @param string $name
*/
public function get($name)
{
}
}
?>

View File

@ -1,42 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
abstract class ParentClass implements \SplObserver
{
public function update(\SplSubject $subject): void
{
}
}
class IndirectImplementsNativeInterface extends ParentClass
{
public function update($subject): void
{
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
abstract class ParentClass implements \SplObserver
{
/**
* @param \SplSubject $subject
*/
public function update($subject): void
{
}
}
class IndirectImplementsNativeInterface extends ParentClass
{
public function update($subject): void
{
}
}
?>

View File

@ -1,48 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface WhateverInterface
{
public function test(string $input);
}
abstract class AbstractSomeAncestorClass implements WhateverInterface
{
}
class SomeChildClass extends AbstractSomeAncestorClass
{
public function test($input) // type omitted for $input
{
/* ... */
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface WhateverInterface
{
/**
* @param string $input
*/
public function test($input);
}
abstract class AbstractSomeAncestorClass implements WhateverInterface
{
}
class SomeChildClass extends AbstractSomeAncestorClass
{
public function test($input) // type omitted for $input
{
/* ... */
}
}
?>

View File

@ -1,38 +0,0 @@
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface SomeAskingInterfaceWithNullable
{
public function ask(?callable $callable = null);
}
final class AskForMore implements SomeAskingInterfaceWithNullable
{
public function ask($callable = null)
{
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture;
interface SomeAskingInterfaceWithNullable
{
/**
* @param callable|null $callable
*/
public function ask($callable = null);
}
final class AskForMore implements SomeAskingInterfaceWithNullable
{
public function ask($callable = null)
{
}
}
?>

View File

@ -3,8 +3,8 @@
declare(strict_types=1);
use Psr\Container\ContainerInterface;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
use Rector\Tests\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector\Fixture\SomeContainerInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
@ -12,9 +12,9 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DowngradeParameterTypeWideningRector::class)
->configure([
DowngradeParameterTypeWideningRector::SAFE_TYPES => [RectorInterface::class],
DowngradeParameterTypeWideningRector::SAFE_TYPES_TO_METHODS => [
ContainerInterface::class => ['setParameter', 'getParameter', 'hasParameter'],
DowngradeParameterTypeWideningRector::UNSAFE_TYPES_TO_METHODS => [
ContainerInterface::class => ['set', 'get', 'has', 'initialized'],
SomeContainerInterface::class => ['set', 'has'],
],
]);
};

View File

@ -29,33 +29,11 @@ use Webmozart\Assert\Assert;
*/
final class DowngradeParameterTypeWideningRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @deprecated Use self::UNSAFE_TYPES_TO_METHODS instead
* @var string
*/
final public const SAFE_TYPES = 'safe_types';
/**
* @deprecated Use self::UNSAFE_TYPES_TO_METHODS instead
* @var string
*/
final public const SAFE_TYPES_TO_METHODS = 'safe_types_to_methods';
/**
* @var string
*/
final public const UNSAFE_TYPES_TO_METHODS = 'unsafe_types_to_methods';
/**
* @var string[]
*/
private array $safeTypes = [];
/**
* @var array<string, string[]>
*/
private array $safeTypesToMethods = [];
/**
* @var array<string, string[]>
*/
@ -107,8 +85,6 @@ final class SomeClass implements SomeInterface
CODE_SAMPLE
,
[
self::SAFE_TYPES => [],
self::SAFE_TYPES_TO_METHODS => [],
self::UNSAFE_TYPES_TO_METHODS => [],
]
),
@ -152,20 +128,6 @@ CODE_SAMPLE
*/
public function configure(array $configuration): void
{
$safeTypes = $configuration[self::SAFE_TYPES] ?? [];
Assert::isArray($safeTypes);
Assert::allString($safeTypes);
$this->safeTypes = $safeTypes;
$safeTypesToMethods = $configuration[self::SAFE_TYPES_TO_METHODS] ?? [];
Assert::isArray($safeTypesToMethods);
foreach ($safeTypesToMethods as $key => $value) {
Assert::string($key);
Assert::allString($value);
}
$this->safeTypesToMethods = $safeTypesToMethods;
$unsafeTypesToMethods = $configuration[self::UNSAFE_TYPES_TO_METHODS] ?? [];
Assert::isArray($unsafeTypesToMethods);
foreach ($unsafeTypesToMethods as $key => $value) {
@ -251,32 +213,6 @@ CODE_SAMPLE
private function isSafeType(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
$classReflectionName = $classReflection->getName();
foreach ($this->safeTypes as $safeType) {
if ($classReflection->isSubclassOf($safeType)) {
return true;
}
// skip self too
if ($classReflectionName === $safeType) {
return true;
}
}
foreach ($this->safeTypesToMethods as $safeType => $safeMethods) {
if (! $this->isNames($classMethod, $safeMethods)) {
continue;
}
if ($classReflection->isSubclassOf($safeType)) {
return true;
}
// skip self too
if ($classReflectionName === $safeType) {
return true;
}
}
foreach ($this->unsafeTypesToMethods as $unsafeType => $unsafeMethods) {
if (! $this->isNames($classMethod, $unsafeMethods)) {
continue;

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\DowngradeCombo;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class CallableInterfaceDowngradeTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/callable_interface_downgrade.php';
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace Rector\Core\Tests\Issues\DowngradeCombo\Fixture;
final class StyleWithCallable implements InterfaceWithCallable
{
public function ask($validator = null)
{
}
}
interface InterfaceWithCallable
{
public function ask(callable $validator = null);
}
?>
-----
<?php
namespace Rector\Core\Tests\Issues\DowngradeCombo\Fixture;
final class StyleWithCallable implements InterfaceWithCallable
{
public function ask($validator = null)
{
}
}
interface InterfaceWithCallable
{
/**
* @param callable|null $validator
*/
public function ask($validator = null);
}
?>

View File

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Set\ValueObject\DowngradeSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(DowngradeSetList::PHP_71);
$containerConfigurator->import(DowngradeSetList::PHP_72);
$containerConfigurator->import(DowngradeSetList::PHP_73);
$containerConfigurator->import(DowngradeSetList::PHP_74);
$containerConfigurator->import(DowngradeSetList::PHP_80);
};

View File

@ -38,10 +38,6 @@ final class StaticEasyPrefixer
// this is public API of a Rector rule
'Symplify\RuleDocGenerator\*',
// not needed since Rector 0.12.7, but keep for BC layer for couple of months
// for configuring sets with ValueObjectInliner
'Symplify\SymfonyPhpConfig\*',
];
/**