From e3023e0c451fb62030fe08b78039a5480d89a912 Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Mon, 20 Mar 2023 20:25:21 -0300 Subject: [PATCH] Add stubs for `mysqli` and `mysqli_result` classes --- stubs/mysqli.stub | 17 ++++++++++++- .../Analyser/NodeScopeResolverTest.php | 4 ++- .../Analyser/data/mysqli-affected-rows.php | 15 +++++++++++ .../Analyser/data/mysqli-result-num-rows.php | 15 +++++++++++ ...ysqli-stmt-affected-rows-and-num-rows.php} | 2 +- .../Rules/Methods/data/returnTypes.php | 25 +++++++++++++++++++ 6 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/PHPStan/Analyser/data/mysqli-affected-rows.php create mode 100644 tests/PHPStan/Analyser/data/mysqli-result-num-rows.php rename tests/PHPStan/Analyser/data/{mysql-stmt.php => mysqli-stmt-affected-rows-and-num-rows.php} (89%) diff --git a/stubs/mysqli.stub b/stubs/mysqli.stub index c20ce70009..77823aaa54 100644 --- a/stubs/mysqli.stub +++ b/stubs/mysqli.stub @@ -1,8 +1,23 @@ |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 */ diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index b9d10bf17f..879c380c60 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -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'); diff --git a/tests/PHPStan/Analyser/data/mysqli-affected-rows.php b/tests/PHPStan/Analyser/data/mysqli-affected-rows.php new file mode 100644 index 0000000000..6f227da9d6 --- /dev/null +++ b/tests/PHPStan/Analyser/data/mysqli-affected-rows.php @@ -0,0 +1,15 @@ +query('UPDATE x SET y = 0;'); + assertType('int<-1, max>|numeric-string', $mysqli->affected_rows); + } +} diff --git a/tests/PHPStan/Analyser/data/mysqli-result-num-rows.php b/tests/PHPStan/Analyser/data/mysqli-result-num-rows.php new file mode 100644 index 0000000000..14aa92bd6b --- /dev/null +++ b/tests/PHPStan/Analyser/data/mysqli-result-num-rows.php @@ -0,0 +1,15 @@ +query('SELECT x FROM z;'); + assertType('int<0, max>|numeric-string', $mysqliResult->num_rows); + } +} diff --git a/tests/PHPStan/Analyser/data/mysql-stmt.php b/tests/PHPStan/Analyser/data/mysqli-stmt-affected-rows-and-num-rows.php similarity index 89% rename from tests/PHPStan/Analyser/data/mysql-stmt.php rename to tests/PHPStan/Analyser/data/mysqli-stmt-affected-rows-and-num-rows.php index f29bd597ee..1ab625db4f 100644 --- a/tests/PHPStan/Analyser/data/mysql-stmt.php +++ b/tests/PHPStan/Analyser/data/mysqli-stmt-affected-rows-and-num-rows.php @@ -1,6 +1,6 @@ |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; + } +}