1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

#9974 removed references_in_scope for setFetchMode

This commit is contained in:
Thomas Bley 2023-07-27 15:53:05 +02:00
parent 13d53ecbf1
commit d600705b0c
2 changed files with 22 additions and 45 deletions

View File

@ -33,10 +33,6 @@ class PdoStatementReturnTypeProvider implements MethodReturnTypeProviderInterfac
return null;
}
if ($method_name_lowercase === 'setfetchmode') {
return self::handleSetFetchMode($event);
}
if ($method_name_lowercase === 'fetch') {
return self::handleFetch($event);
}
@ -48,39 +44,11 @@ class PdoStatementReturnTypeProvider implements MethodReturnTypeProviderInterfac
return null;
}
private static function handleSetFetchMode(MethodReturnTypeProviderEvent $event): ?Union
{
$source = $event->getSource();
$call_args = $event->getCallArgs();
$context = $event->getContext();
unset($context->references_in_scope['fetch_mode']);
unset($context->references_in_scope['fetch_class']);
if (isset($call_args[0])
&& ($first_arg_type = $source->getNodeTypeProvider()->getType($call_args[0]->value))
&& $first_arg_type->isSingleIntLiteral()
) {
$context->references_in_scope['fetch_mode'] = (string) $first_arg_type->getSingleIntLiteral()->value;
}
if (isset($call_args[1])
&& ($second_arg_type = $source->getNodeTypeProvider()->getType($call_args[1]->value))
&& $second_arg_type->isSingleStringLiteral()
) {
$context->references_in_scope['fetch_class'] = $second_arg_type->getSingleStringLiteral()->value;
}
return null;
}
private static function handleFetch(MethodReturnTypeProviderEvent $event): ?Union
{
$source = $event->getSource();
$call_args = $event->getCallArgs();
$context = $event->getContext();
$fetch_mode = $context->references_in_scope['fetch_mode'] ?? null;
$fetch_mode = 0;
if (isset($call_args[0])
&& ($first_arg_type = $source->getNodeTypeProvider()->getType($call_args[0]->value))
@ -89,8 +57,6 @@ class PdoStatementReturnTypeProvider implements MethodReturnTypeProviderInterfac
$fetch_mode = $first_arg_type->getSingleIntLiteral()->value;
}
$fetch_class_name = $context->references_in_scope['fetch_class'] ?? null;
switch ($fetch_mode) {
case 2: // PDO::FETCH_ASSOC - array<string,scalar|null>|false
return new Union([
@ -128,7 +94,7 @@ class PdoStatementReturnTypeProvider implements MethodReturnTypeProviderInterfac
case 8: // PDO::FETCH_CLASS - object|false
return new Union([
$fetch_class_name ? new TNamedObject($fetch_class_name) : new TObject(),
new TObject(),
new TFalse(),
]);
@ -194,9 +160,7 @@ class PdoStatementReturnTypeProvider implements MethodReturnTypeProviderInterfac
{
$source = $event->getSource();
$call_args = $event->getCallArgs();
$context = $event->getContext();
$fetch_mode = $context->references_in_scope['fetch_mode'] ?? null;
$fetch_mode = 0;
if (isset($call_args[0])
&& ($first_arg_type = $source->getNodeTypeProvider()->getType($call_args[0]->value))
@ -205,7 +169,7 @@ class PdoStatementReturnTypeProvider implements MethodReturnTypeProviderInterfac
$fetch_mode = $first_arg_type->getSingleIntLiteral()->value;
}
$fetch_class_name = $context->references_in_scope['fetch_class'] ?? null;
$fetch_class_name = null;
if (isset($call_args[1])
&& ($second_arg_type = $source->getNodeTypeProvider()->getType($call_args[1]->value))

View File

@ -509,22 +509,35 @@ class MethodCallTest extends TestCase
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("select \"a\" as a");
$stmt->setFetchMode(PDO::FETCH_CLASS, A::class);
$stmt2 = $db->prepare("select \"a\" as a");
$stmt2->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$stmt2->execute();
/** @psalm-suppress MixedAssignment */
$a = $stmt->fetch();
$b = $stmt->fetchAll();
$c = $stmt->fetch(PDO::FETCH_CLASS);
$d = $stmt->fetchAll(PDO::FETCH_CLASS);
$e = $stmt->fetchAll(PDO::FETCH_CLASS, B::class);
$f = $stmt->fetch(PDO::FETCH_ASSOC);
$g = $stmt->fetchAll(PDO::FETCH_ASSOC);',
$g = $stmt->fetchAll(PDO::FETCH_ASSOC);
/** @psalm-suppress MixedAssignment */
$h = $stmt2->fetch();
$i = $stmt2->fetchAll();
$j = $stmt2->fetch(PDO::FETCH_BOTH);
$k = $stmt2->fetchAll(PDO::FETCH_BOTH);',
'assertions' => [
'$a' => 'A|false',
'$b' => 'list<A>',
'$c' => 'A|false',
'$d' => 'list<A>',
'$a' => 'mixed',
'$b' => 'array<array-key, mixed>|false',
'$c' => 'false|object',
'$d' => 'list<object>',
'$e' => 'list<B>',
'$f' => 'array<string, null|scalar>|false',
'$g' => 'list<array<string, null|scalar>>',
'$h' => 'mixed',
'$i' => 'array<array-key, mixed>|false',
'$j' => 'array<array-key, null|scalar>|false',
'$k' => 'list<array<array-key, null|scalar>>',
],
],
'datePeriodConstructor' => [