Skip to content

Commit

Permalink
Merge pull request #8650 from VincentLanglet/serialize
Browse files Browse the repository at this point in the history
Do not report serialize as unused
  • Loading branch information
orklah committed Nov 2, 2022
2 parents 761d67f + 0bdfc4b commit 85b5436
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Psalm/Internal/Codebase/Functions.php
Expand Up @@ -518,12 +518,27 @@ public function isCallMapFunctionPure(

//gettext
'bindtextdomain',

// unserialize
'unserialize'
];

if (in_array(strtolower($function_id), $impure_functions, true)) {
return false;
}

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 (strpos($function_id, 'image') === 0) {
return false;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/UnusedCodeTest.php
Expand Up @@ -737,6 +737,31 @@ public function unserialize($_serialized) : void {}
new Foo();'
],
'ignoreSerializeAndUnserialize' => [
'<?php
class Foo
{
public function __sleep(): array
{
throw new BadMethodCallException();
}
public function __wakeup(): void
{
throw new BadMethodCallException();
}
}
function test(): bool {
try {
serialize(new Foo());
unserialize("");
} catch (\Throwable) {
return false;
}
return true;
}'
],
'useIteratorMethodsWhenCallingForeach' => [
'<?php
/** @psalm-suppress UnimplementedInterfaceMethod */
Expand Down

0 comments on commit 85b5436

Please sign in to comment.