Skip to content

Commit

Permalink
Add stubs for mysqli and mysqli_result classes
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored and ondrejmirtes committed Sep 29, 2023
1 parent d25b897 commit e3023e0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 3 deletions.
17 changes: 16 additions & 1 deletion stubs/mysqli.stub
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
<?php

class mysqli_stmt
class mysqli
{
/**
* @var int<-1,max>|numeric-string
*/
public $affected_rows;
}

class mysqli_result
{
/**
* @var int<0,max>|numeric-string
*/
public $num_rows;
}

class mysqli_stmt
{
/**
* @var int<-1,max>|numeric-string
*/
Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,9 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/callsite-cast-narrowing.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8775.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8752.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysql-stmt.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli-affected-rows.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli-result-num-rows.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysqli-stmt-affected-rows-and-num-rows.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/list-shapes.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3013.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7607.php');
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/mysqli-affected-rows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MySqlAffectedRowsType;

use mysqli;
use function PHPStan\Testing\assertType;

final class Foo {
public function bar(): void
{
$mysqli = new mysqli();
$mysqli->query('UPDATE x SET y = 0;');
assertType('int<-1, max>|numeric-string', $mysqli->affected_rows);
}
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/mysqli-result-num-rows.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace MySQLiResultNumRowsType;

use mysqli;
use function PHPStan\Testing\assertType;

final class Foo {
public function bar(): void
{
$mysqli = new mysqli();
$mysqliResult = $mysqli->query('SELECT x FROM z;');
assertType('int<0, max>|numeric-string', $mysqliResult->num_rows);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MySQLiStmt;
namespace MySQLiStmtAffectedRowsAndNumRowsTypes;

use mysqli;
use function PHPStan\Testing\assertType;
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Methods/data/returnTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,28 @@ public function doBaz3(): string
}

}

interface MySQLiAffectedRowsReturnTypeInterface
{
/**
* @return int|numeric-string
*/
function exec(\mysqli $connection, string $sql);
}

final class MySQLiAffectedRowsReturnType implements MySQLiAffectedRowsReturnTypeInterface
{
/**
* @return int<0, max>|numeric-string
*/
function exec(\mysqli $mysqli, string $sql)
{
$result = $mysqli->query($sql);

if ($result === false || 0 > $mysqli->affected_rows) {
throw new \RuntimeException();
}

return $mysqli->affected_rows;
}
}

0 comments on commit e3023e0

Please sign in to comment.