Skip to content

Commit

Permalink
[Downgrade Php 7.2] Add opt-out parameter for unsafe types to avoid p…
Browse files Browse the repository at this point in the history
…iling list of safe types (#1448)
  • Loading branch information
TomasVotruba authored Dec 10, 2021
1 parent dcabd2d commit b3f13b5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
47 changes: 5 additions & 42 deletions build/config/config-downgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\DowngradePhp72\Rector\ClassMethod\DowngradeParameterTypeWideningRector;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\Set\ValueObject\DowngradeLevelSetList;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -37,50 +38,12 @@
$containerConfigurator->import(DowngradeLevelSetList::DOWN_TO_PHP_71);

$services = $containerConfigurator->services();

$services->set(DowngradeParameterTypeWideningRector::class)
->configure([
DowngradeParameterTypeWideningRector::SAFE_TYPES => [
// phsptan
Type::class,
RectorInterface::class,
// php-parser
NodeVisitorAbstract::class,
NodeVisitor::class,
ConfigurableRectorInterface::class,
OutputInterface::class,
StyleInterface::class,
PhpDocNodeVisitorInterface::class,
Node::class,
NodeNameResolverInterface::class,
// phpstan
SourceLocator::class,
\PHPStan\PhpDocParser\Ast\Node::class,
\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode::class,
\PHPStan\PhpDocParser\Ast\NodeAttributes::class,
\PhpParser\Parser::class,
\Rector\Naming\Contract\RenameParamValueObjectInterface::class,
\Symplify\RuleDocGenerator\Contract\RuleCodeSamplePrinterInterface::class,
\Symplify\RuleDocGenerator\Contract\Category\CategoryInfererInterface::class,
\PhpParser\PrettyPrinterAbstract::class,
\Helmich\TypoScriptParser\Parser\Traverser\Visitor::class,
\Symplify\SymplifyKernel\Contract\LightKernelInterface::class,
\Symfony\Component\String\Slugger\SluggerInterface::class,
\Psr\Log\LoggerInterface::class,
// phsptan
\PHPStan\Type\DynamicStaticMethodReturnTypeExtension::class,
\PHPStan\Type\DynamicFunctionReturnTypeExtension::class,
\PHPStan\Type\DynamicFunctionThrowTypeExtension::class,
\PHPStan\Type\DynamicMethodReturnTypeExtension::class,
\PHPStan\Type\DynamicMethodThrowTypeExtension::class,
\PHPStan\Type\DynamicReturnTypeExtensionRegistry::class,
\PHPStan\Type\DynamicStaticMethodReturnTypeExtension::class,
\PHPStan\Type\DynamicStaticMethodThrowTypeExtension::class,
],
DowngradeParameterTypeWideningRector::SAFE_TYPES_TO_METHODS => [
ContainerInterface::class => [
'setParameter',
'getParameter',
'hasParameter',
DowngradeParameterTypeWideningRector::UNSAFE_TYPES_TO_METHODS => [
Loader::class => [
'load'
],
],
]);
Expand Down
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,12 @@ parameters:

# 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@
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[]
*/
Expand All @@ -49,6 +56,11 @@ final class DowngradeParameterTypeWideningRector extends AbstractRector implemen
*/
private array $safeTypesToMethods = [];

/**
* @var array<string, string[]>
*/
private array $unsafeTypesToMethods = [];

public function __construct(
private readonly NativeParamToPhpDocDecorator $nativeParamToPhpDocDecorator,
private readonly ReflectionResolver $reflectionResolver,
Expand Down Expand Up @@ -97,6 +109,7 @@ public function test($input)
[
self::SAFE_TYPES => [],
self::SAFE_TYPES_TO_METHODS => [],
self::UNSAFE_TYPES_TO_METHODS => [],
]
),
]);
Expand Down Expand Up @@ -152,6 +165,15 @@ public function configure(array $configuration): void
}

$this->safeTypesToMethods = $safeTypesToMethods;

$unsafeTypesToMethods = $configuration[self::UNSAFE_TYPES_TO_METHODS] ?? [];
Assert::isArray($unsafeTypesToMethods);
foreach ($unsafeTypesToMethods as $key => $value) {
Assert::string($key);
Assert::allString($value);
}

$this->unsafeTypesToMethods = $unsafeTypesToMethods;
}

private function shouldSkip(ClassReflection $classReflection, ClassMethod $classMethod): bool
Expand Down Expand Up @@ -255,6 +277,21 @@ private function isSafeType(ClassReflection $classReflection, ClassMethod $class
}
}

foreach ($this->unsafeTypesToMethods as $unsafeType => $unsafeMethods) {
if (! $this->isNames($classMethod, $unsafeMethods)) {
continue;
}

if ($classReflection->isSubclassOf($unsafeType)) {
return false;
}

// skip self too
if ($classReflectionName === $unsafeType) {
return false;
}
}

return false;
}
}

0 comments on commit b3f13b5

Please sign in to comment.