Skip to content

Commit

Permalink
Merge pull request #10055 from thbley/master
Browse files Browse the repository at this point in the history
Add type detection for PDOStatement::fetchAll(PDO::FETCH_CLASS, SomeClass::class)
  • Loading branch information
orklah authored Jul 27, 2023
2 parents 6c0a09a + 88ddb89 commit 73ebe22
Show file tree
Hide file tree
Showing 2 changed files with 407 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,88 +27,268 @@ public static function getClassLikeNames(): array
public static function getMethodReturnType(MethodReturnTypeProviderEvent $event): ?Union
{
$config = Config::getInstance();
$method_name_lowercase = $event->getMethodNameLowercase();

if (!$config->php_extensions["pdo"]) {
return null;
}

if ($method_name_lowercase === 'fetch') {
return self::handleFetch($event);
}

if ($method_name_lowercase === 'fetchall') {
return self::handleFetchAll($event);
}

return null;
}

private static function handleFetch(MethodReturnTypeProviderEvent $event): ?Union
{
$source = $event->getSource();
$call_args = $event->getCallArgs();
$method_name_lowercase = $event->getMethodNameLowercase();
if ($method_name_lowercase === 'fetch'
&& $config->php_extensions["pdo"]
&& isset($call_args[0])
$fetch_mode = 0;

if (isset($call_args[0])
&& ($first_arg_type = $source->getNodeTypeProvider()->getType($call_args[0]->value))
&& $first_arg_type->isSingleIntLiteral()
) {
$fetch_mode = $first_arg_type->getSingleIntLiteral()->value;
}

switch ($fetch_mode) {
case 2: // PDO::FETCH_ASSOC - array<string,scalar|null>|false
return new Union([
new TArray([
Type::getString(),
new Union([
new TScalar(),
new TNull(),
]),
switch ($fetch_mode) {
case 2: // PDO::FETCH_ASSOC - array<string,scalar|null>|false
return new Union([
new TArray([
Type::getString(),
new Union([
new TScalar(),
new TNull(),
]),
new TFalse(),
]);

case 4: // PDO::FETCH_BOTH - array<array-key,scalar|null>|false
return new Union([
new TArray([
Type::getArrayKey(),
new Union([
new TScalar(),
new TNull(),
]),
new TFalse(),
]);

case 4: // PDO::FETCH_BOTH - array<array-key,scalar|null>|false
return new Union([
new TArray([
Type::getArrayKey(),
new Union([
new TScalar(),
new TNull(),
]),
]),
new TFalse(),
]);

case 6: // PDO::FETCH_BOUND - bool
return Type::getBool();

case 7: // PDO::FETCH_COLUMN - scalar|null|false
return new Union([
new TScalar(),
new TNull(),
new TFalse(),
]);

case 8: // PDO::FETCH_CLASS - object|false
return new Union([
new TObject(),
new TFalse(),
]);

case 1: // PDO::FETCH_LAZY - object|false
// This actually returns a PDORow object, but that class is
// undocumented, and its attributes are all dynamic anyway
return new Union([
new TObject(),
new TFalse(),
]);

case 11: // PDO::FETCH_NAMED - array<string, scalar|null|list<scalar|null>>|false
return new Union([
new TArray([
Type::getString(),
new Union([
new TScalar(),
new TNull(),
Type::getListAtomic(
new Union([
new TScalar(),
new TNull(),
]),
),
]),
]),
new TFalse(),
]);

case 12: // PDO::FETCH_KEY_PAIR - array<array-key,scalar|null>
return new Union([
new TArray([
Type::getArrayKey(),
new Union([
new TScalar(),
new TNull(),
]),
]),
]);

case 3: // PDO::FETCH_NUM - list<scalar|null>|false
return new Union([
Type::getListAtomic(
new Union([
new TScalar(),
new TNull(),
]),
),
new TFalse(),
]);

case 5: // PDO::FETCH_OBJ - stdClass|false
return new Union([
new TNamedObject('stdClass'),
new TFalse(),
]);
}

return null;
}

private static function handleFetchAll(MethodReturnTypeProviderEvent $event): ?Union
{
$source = $event->getSource();
$call_args = $event->getCallArgs();
$fetch_mode = 0;

if (isset($call_args[0])
&& ($first_arg_type = $source->getNodeTypeProvider()->getType($call_args[0]->value))
&& $first_arg_type->isSingleIntLiteral()
) {
$fetch_mode = $first_arg_type->getSingleIntLiteral()->value;
}

$fetch_class_name = null;

if (isset($call_args[1])
&& ($second_arg_type = $source->getNodeTypeProvider()->getType($call_args[1]->value))
&& $second_arg_type->isSingleStringLiteral()
) {
$fetch_class_name = $second_arg_type->getSingleStringLiteral()->value;
}

switch ($fetch_mode) {
case 2: // PDO::FETCH_ASSOC - list<array<string,scalar|null>>
return new Union([
Type::getListAtomic(
new Union([
new TArray([
Type::getString(),
new Union([
new TScalar(),
new TNull(),
]),
]),
]),
new TFalse(),
]);

case 6: // PDO::FETCH_BOUND - bool
return Type::getBool();

case 8: // PDO::FETCH_CLASS - object|false
return new Union([
new TObject(),
new TFalse(),
]);

case 1: // PDO::FETCH_LAZY - object|false
// This actually returns a PDORow object, but that class is
// undocumented, and its attributes are all dynamic anyway
return new Union([
new TObject(),
new TFalse(),
]);

case 11: // PDO::FETCH_NAMED - array<string, scalar|list<scalar>>|false
return new Union([
new TArray([
Type::getString(),
new Union([
new TScalar(),
Type::getListAtomic(Type::getScalar()),
),
]);

case 4: // PDO::FETCH_BOTH - list<array<array-key,scalar|null>>
return new Union([
Type::getListAtomic(
new Union([
new TArray([
Type::getArrayKey(),
new Union([
new TScalar(),
new TNull(),
]),
]),
]),
new TFalse(),
]);

case 3: // PDO::FETCH_NUM - list<scalar|null>|false
return new Union([
Type::getListAtomic(
new Union([
new TScalar(),
new TNull(),
),
]);

case 6: // PDO::FETCH_BOUND - list<bool>
return new Union([
Type::getListAtomic(
Type::getBool(),
),
]);

case 7: // PDO::FETCH_COLUMN - list<scalar|null>
return new Union([
Type::getListAtomic(
new Union([
new TScalar(),
new TNull(),
]),
),
]);

case 8: // PDO::FETCH_CLASS - list<object>
return new Union([
Type::getListAtomic(
new Union([
$fetch_class_name ? new TNamedObject($fetch_class_name) : new TObject(),
]),
),
]);

case 11: // PDO::FETCH_NAMED - list<array<string, scalar|null|list<scalar|null>>>
return new Union([
Type::getListAtomic(
new Union([
new TArray([
Type::getString(),
new Union([
new TScalar(),
new TNull(),
Type::getListAtomic(
new Union([
new TScalar(),
new TNull(),
]),
),
]),
]),
),
new TFalse(),
]);

case 5: // PDO::FETCH_OBJ - stdClass|false
return new Union([
new TNamedObject('stdClass'),
new TFalse(),
]);
}
]),
),
]);

case 12: // PDO::FETCH_KEY_PAIR - array<array-key,scalar|null>
return new Union([
new TArray([
Type::getArrayKey(),
new Union([
new TScalar(),
new TNull(),
]),
]),
]);

case 3: // PDO::FETCH_NUM - list<list<scalar|null>>
return new Union([
Type::getListAtomic(
new Union([
Type::getListAtomic(
new Union([
new TScalar(),
new TNull(),
]),
),
]),
),
]);

case 5: // PDO::FETCH_OBJ - list<stdClass>
return new Union([
Type::getListAtomic(
new Union([
new TNamedObject('stdClass'),
]),
),
]);
}

return null;
Expand Down
Loading

0 comments on commit 73ebe22

Please sign in to comment.