Skip to content

Commit 02071ab

Browse files
authored
Infer object type mysqli_fetch_object (#2675)
1 parent c88f00c commit 02071ab

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

resources/functionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7482,7 +7482,7 @@
74827482
'mysqli_fetch_field_direct' => ['(stdClass&object{name: string, orgname: string, table: string, orgtable: string, def: string, db: string, catalog: "def", max_length: int, length: int, charsetnr: string, flags: int, type: int, decimals: int})|false', 'result'=>'mysqli_result', 'fieldnr'=>'int'],
74837483
'mysqli_fetch_fields' => ['list<stdClass&object{name: string, orgname: string, table: string, orgtable: string, def: string, db: string, catalog: "def", max_length: int, length: int, charsetnr: string, flags: int, type: int, decimals: int}>', 'result'=>'mysqli_result'],
74847484
'mysqli_fetch_lengths' => ['array|false', 'result'=>'mysqli_result'],
7485-
'mysqli_fetch_object' => ['object|null', 'result'=>'mysqli_result', 'class_name='=>'string', 'params='=>'?array'],
7485+
'mysqli_fetch_object' => ['object|false|null', 'result'=>'mysqli_result', 'class_name='=>'string', 'params='=>'?array'],
74867486
'mysqli_fetch_row' => ['array|null', 'result'=>'mysqli_result'],
74877487
'mysqli_field_count' => ['int', 'link'=>'mysqli'],
74887488
'mysqli_field_seek' => ['bool', 'result'=>'mysqli_result', 'fieldnr'=>'int'],

stubs/mysqli.stub

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,26 @@ class mysqli_result
1414
* @var int<0,max>|numeric-string
1515
*/
1616
public $num_rows;
17+
18+
/**
19+
* @template T of object
20+
* @param class-string<T> $class
21+
* @param array<mixed> $constructor_args
22+
* @return T|null|false
23+
*/
24+
function fetch_object(string $class = 'stdClass', array $constructor_args = []) {}
1725
}
1826

27+
28+
/**
29+
* @template T of object
30+
*
31+
* @param class-string<T> $class
32+
* @param array<mixed> $constructor_args
33+
* @return T|null|false
34+
*/
35+
function mysqli_fetch_object(mysqli_result $result, string $class = 'stdClass', array $constructor_args = []) {}
36+
1937
class mysqli_stmt
2038
{
2139
/**

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,7 @@ public function dataFileAsserts(): iterable
14591459
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10037.php');
14601460
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/PhpDoc/data/bug-10594.php');
14611461
yield from $this->gatherAssertTypes(__DIR__ . '/data/set-type-type-specifying.php');
1462+
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli_fetch_object.php');
14621463
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10468.php');
14631464
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6613.php');
14641465
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10187.php');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace MysqliFetchObject;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function doFoo(\mysqli_result $result) {
8+
assertType('stdClass|false|null', $result->fetch_object());
9+
assertType('MysqliFetchObject\MyClass|false|null', $result->fetch_object(MyClass::class));
10+
11+
assertType('stdClass|false|null', mysqli_fetch_object($result));
12+
assertType('MysqliFetchObject\MyClass|false|null', mysqli_fetch_object($result, MyClass::class));
13+
}
14+
15+
class MyClass {}
16+

0 commit comments

Comments
 (0)