Skip to content

Commit

Permalink
[stabilize] Deprecate FinalizePublicClassConstantRector as not reliab…
Browse files Browse the repository at this point in the history
…le and causes uncontroller changed (#5534)
  • Loading branch information
TomasVotruba committed Jan 31, 2024
1 parent 27791d1 commit 2efd564
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 181 deletions.
2 changes: 0 additions & 2 deletions config/set/php81.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector;
use Rector\Php81\Rector\ClassMethod\NewInInitializerRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
Expand All @@ -19,7 +18,6 @@
ReturnNeverTypeRector::class,
MyCLabsClassToEnumRector::class,
MyCLabsMethodCallToEnumConstRector::class,
FinalizePublicClassConstantRector::class,
ReadOnlyPropertyRector::class,
SpatieEnumClassToEnumRector::class,
SpatieEnumMethodCallToEnumConstRector::class,
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ parameters:
- '#Call to method PHPUnit\\Framework\\Assert\:\:assert(.*?) will always evaluate to (true|false)#'

- '#Doing instanceof PHPStan\\Type\\.* is error\-prone and deprecated(\. Use Type\:\:.*\(\) (or .* )?instead)?#'
- '#Fetching class constant class of deprecated class Rector\\Php81\\Rector\\ClassConst\\FinalizePublicClassConstantRector#'

# phpstan 1.10.0
- '#Parameter 3 should use "PHPStan\\Reflection\\ParameterReflectionWithPhpDocs" type as the only type passed to this method#'
Expand Down Expand Up @@ -463,6 +464,7 @@ parameters:

# optional as changes behavior, should be used explicitly outside PHP upgrade
- '#Register "Rector\\Php73\\Rector\\FuncCall\\JsonThrowOnErrorRector" service to "php73\.php" config set#'
- '#Register "Rector\\Php81\\Rector\\ClassConst\\FinalizePublicClassConstantRector" service to "php81\.php" config set#'

# soon to be extended
-
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

71 changes: 14 additions & 57 deletions rules/Php81/Rector/ClassConst/FinalizePublicClassConstantRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,18 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://php.watch/versions/8.1/final-class-const
*
* @see \Rector\Tests\Php81\Rector\ClassConst\FinalizePublicClassConstantRector\FinalizePublicClassConstantRectorTest
* @deprecated This was deprecated, as its functionality caused bugs. Without knowing the full dependency tree, its risky to change
*/
final class FinalizePublicClassConstantRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
final class FinalizePublicClassConstantRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly FamilyRelationsAnalyzer $familyRelationsAnalyzer,
private readonly ReflectionProvider $reflectionProvider,
private readonly VisibilityManipulator $visibilityManipulator
) {
}
private bool $hasWarned = false;

public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -63,42 +52,22 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
if ($node->isFinal()) {
if ($this->hasWarned) {
return null;
}

if (! $scope->isInClass()) {
return null;
}
trigger_error(
sprintf(
'The "%s" rule was deprecated, as its functionality caused bugs. Without knowing the full dependency tree, its risky to change.',
self::class
)
);

$classReflection = $scope->getClassReflection();
if ($classReflection->isAnonymous()) {
return null;
}
sleep(3);

$hasChanged = false;
foreach ($node->getConstants() as $classConst) {
if (! $classConst->isPublic()) {
continue;
}

if ($classConst->isFinal()) {
continue;
}

if ($this->isClassHasChildren($node)) {
continue;
}

$hasChanged = true;
$this->visibilityManipulator->makeFinal($classConst);
}

if ($hasChanged) {
return $node;
}
$this->hasWarned = true;

return null;
}
Expand All @@ -107,16 +76,4 @@ public function provideMinPhpVersion(): int
{
return PhpVersionFeature::FINAL_CLASS_CONSTANTS;
}

private function isClassHasChildren(Class_ $class): bool
{
$className = (string) $this->nodeNameResolver->getName($class);
if (! $this->reflectionProvider->hasClass($className)) {
return false;
}

$classReflection = $this->reflectionProvider->getClass($className);

return $this->familyRelationsAnalyzer->getChildrenOfClassReflection($classReflection) !== [];
}
}
3 changes: 3 additions & 0 deletions rules/Privatization/NodeManipulator/VisibilityManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function makeNonAbstract(ClassMethod | Class_ $node): void
$node->flags -= Class_::MODIFIER_ABSTRACT;
}

/**
* @api
*/
public function makeFinal(Class_ | ClassMethod | ClassConst $node): void
{
$this->addVisibilityFlag($node, Visibility::FINAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public function __construct(
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change private classMethod param type to strict type, based on passed strict types',
'Change private method param type to strict type, based on passed strict types',
[
new CodeSample(
<<<'CODE_SAMPLE'
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(int $value)
Expand All @@ -53,8 +53,8 @@ private function resolve($value)
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(int $value)
Expand All @@ -67,9 +67,10 @@ private function resolve(int $value)
}
}
CODE_SAMPLE
),

]);
),

]
);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions utils/Command/MissingInSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\DeadCode\Rector\ClassMethod\RemoveNullTagValueNodeRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\TypeDeclaration\Rector\BooleanAnd\BinaryOpNullableToInstanceofRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
Expand Down Expand Up @@ -39,6 +40,8 @@ final class MissingInSetCommand extends Command
// personal preference, enable on purpose
ArraySpreadInsteadOfArrayMergeRector::class,
FinalizeClassesWithoutChildrenRector::class,
// deprecated
FinalizePublicClassConstantRector::class,
];

public function __construct(
Expand Down

0 comments on commit 2efd564

Please sign in to comment.