diff --git a/src/Psalm/Internal/Codebase/Functions.php b/src/Psalm/Internal/Codebase/Functions.php index eb73c2932be..54e1bdca1f0 100644 --- a/src/Psalm/Internal/Codebase/Functions.php +++ b/src/Psalm/Internal/Codebase/Functions.php @@ -530,12 +530,8 @@ public function isCallMapFunctionPure( if ($function_id === 'serialize' && isset($args[0]) && $type_provider) { $serialize_type = $type_provider->getType($args[0]->value); - if ($serialize_type) { - foreach ($serialize_type->getAtomicTypes() as $atomic_serialize_type) { - if ($atomic_serialize_type->isObjectType()) { - return false; - } - } + if ($serialize_type && $serialize_type->containsObjectType()) { + return false; } } diff --git a/src/Psalm/Internal/TypeVisitor/ContainsObjectTypeVisitor.php b/src/Psalm/Internal/TypeVisitor/ContainsObjectTypeVisitor.php new file mode 100644 index 00000000000..c2af2184f55 --- /dev/null +++ b/src/Psalm/Internal/TypeVisitor/ContainsObjectTypeVisitor.php @@ -0,0 +1,36 @@ +as->hasObjectType()) + ) { + $this->contains_object_type = true; + return NodeVisitor::STOP_TRAVERSAL; + } + + return null; + } + + public function matches(): bool + { + return $this->contains_object_type; + } +} diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index 6ff34be3156..6a82d192e9e 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -9,6 +9,7 @@ use Psalm\Internal\Type\TypeCombiner; use Psalm\Internal\TypeVisitor\ContainsClassLikeVisitor; use Psalm\Internal\TypeVisitor\ContainsLiteralVisitor; +use Psalm\Internal\TypeVisitor\ContainsObjectTypeVisitor; use Psalm\Internal\TypeVisitor\FromDocblockSetter; use Psalm\Internal\TypeVisitor\TemplateTypeCollector; use Psalm\Internal\TypeVisitor\TypeChecker; @@ -1492,6 +1493,15 @@ public function containsAnyLiteral(): bool return $literal_visitor->matches(); } + public function containsObjectType(): bool + { + $object_type_visitor = new ContainsObjectTypeVisitor(); + + $object_type_visitor->traverseArray($this->types); + + return $object_type_visitor->matches(); + } + /** * @return list */ diff --git a/tests/UnusedCodeTest.php b/tests/UnusedCodeTest.php index 373ad1cf72b..e0e1baec322 100644 --- a/tests/UnusedCodeTest.php +++ b/tests/UnusedCodeTest.php @@ -754,6 +754,7 @@ public function __wakeup(): void function test(): bool { try { serialize(new Foo()); + serialize([new Foo()]); unserialize(""); } catch (\Throwable) { return false;