From c322032a07f4fb418618f8de2ef9b475b57ebd0f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 22 Nov 2024 20:22:33 +0700 Subject: [PATCH] [DeadCode] Skip used by get_object_vars() when implements JsonSerializable on RemoveUnusedPromotedPropertyRector (#6472) --- ..._object_vars_from_jsonserializable.php.inc | 15 ++++++++++ .../PropertyWriteonlyAnalyzer.php | 29 +++++++++++++++++-- .../RemoveUnusedPromotedPropertyRector.php | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_used_by_get_object_vars_from_jsonserializable.php.inc diff --git a/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_used_by_get_object_vars_from_jsonserializable.php.inc b/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_used_by_get_object_vars_from_jsonserializable.php.inc new file mode 100644 index 00000000000..5173e1a55a9 --- /dev/null +++ b/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_used_by_get_object_vars_from_jsonserializable.php.inc @@ -0,0 +1,15 @@ +betterNodeFinder->findFirst($class, static function (Node $node): bool { + $isImplementsJsonSerializable = $this->nodeTypeResolver->isObjectType( + $class, + new ObjectType('JsonSerializable') + ); + + return (bool) $this->betterNodeFinder->findFirst($class, function (Node $node) use ( + $isImplementsJsonSerializable + ): bool { + if ($isImplementsJsonSerializable && $node instanceof FuncCall && $this->nodeNameResolver->isName( + $node, + 'get_object_vars' + ) && ! $node->isFirstClassCallable()) { + $firstArg = $node->getArgs()[0] ?? null; + if ($firstArg instanceof Arg && $firstArg->value instanceof Variable && $firstArg->value->name === 'this') { + return true; + } + } + if (! $node instanceof PropertyFetch && ! $node instanceof NullsafePropertyFetch) { return false; } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php index 9ac7d4bf2ef..d47ee0983a8 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php @@ -191,6 +191,6 @@ private function shouldSkipClass(Class_ $class): bool } } - return false; + return $this->propertyWriteonlyAnalyzer->hasClassDynamicPropertyNames($class); } }