diff --git a/system/Database/BaseResult.php b/system/Database/BaseResult.php index 532549262283..1b8fcdbd0cf8 100644 --- a/system/Database/BaseResult.php +++ b/system/Database/BaseResult.php @@ -387,8 +387,6 @@ public function setRow($key, $value = null) /** * Returns the "first" row of the current results. - * - * @return array|object|null */ public function getFirstRow(string $type = 'object') { @@ -399,8 +397,6 @@ public function getFirstRow(string $type = 'object') /** * Returns the "last" row of the current results. - * - * @return array|object|null */ public function getLastRow(string $type = 'object') { @@ -411,8 +407,6 @@ public function getLastRow(string $type = 'object') /** * Returns the "next" row of the current results. - * - * @return array|object|null */ public function getNextRow(string $type = 'object') { @@ -426,8 +420,6 @@ public function getNextRow(string $type = 'object') /** * Returns the "previous" row of the current results. - * - * @return array|object|null */ public function getPreviousRow(string $type = 'object') { diff --git a/system/Database/ResultInterface.php b/system/Database/ResultInterface.php index 89febfef835c..0157a1df8149 100644 --- a/system/Database/ResultInterface.php +++ b/system/Database/ResultInterface.php @@ -113,28 +113,44 @@ public function setRow($key, $value = null); /** * Returns the "first" row of the current results. * - * @return array|object|null + * @template T of object + * + * @param 'array'|'object'|class-string $type The type of result object. 'array', 'object' or class name. + * + * @return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : T|null)) */ public function getFirstRow(string $type = 'object'); /** * Returns the "last" row of the current results. * - * @return array|object|null + * @template T of object + * + * @param 'array'|'object'|class-string $type The type of result object. 'array', 'object' or class name. + * + * @return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : T|null)) */ public function getLastRow(string $type = 'object'); /** * Returns the "next" row of the current results. * - * @return array|object|null + * @template T of object + * + * @param 'array'|'object'|class-string $type The type of result object. 'array', 'object' or class name. + * + * @return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : T|null)) */ public function getNextRow(string $type = 'object'); /** * Returns the "previous" row of the current results. * - * @return array|object|null + * @template T of object + * + * @param 'array'|'object'|class-string $type The type of result object. 'array', 'object' or class name. + * + * @return ($type is 'object' ? stdClass|null : ($type is 'array' ? array|null : T|null)) */ public function getPreviousRow(string $type = 'object'); diff --git a/system/Test/Mock/MockConnection.php b/system/Test/Mock/MockConnection.php index 0db8aec916d5..e19e635568ea 100644 --- a/system/Test/Mock/MockConnection.php +++ b/system/Test/Mock/MockConnection.php @@ -196,7 +196,7 @@ public function error(): array public function insertID(): int { - return $this->connID->insert_id; + return 0; } /** diff --git a/tests/system/Database/Live/FabricatorLiveTest.php b/tests/system/Database/Live/FabricatorLiveTest.php index a865d2fef4c8..2a45b3ce19cc 100644 --- a/tests/system/Database/Live/FabricatorLiveTest.php +++ b/tests/system/Database/Live/FabricatorLiveTest.php @@ -18,6 +18,7 @@ use CodeIgniter\Test\DatabaseTestTrait; use CodeIgniter\Test\Fabricator; use PHPUnit\Framework\Attributes\Group; +use stdClass; use Tests\Support\Models\UserModel; use Tests\Support\Models\ValidModel; @@ -39,6 +40,7 @@ public function testCreateAddsToDatabase(): void $fabricator->setOverrides(['country' => 'Spain']); $result = $fabricator->create(); + $this->assertInstanceOf(stdClass::class, $result); $this->seeInDatabase('user', ['name' => $result->name]); } diff --git a/tests/system/Database/Live/GetTest.php b/tests/system/Database/Live/GetTest.php index 57a124d4fd0b..91035fe6b1d5 100644 --- a/tests/system/Database/Live/GetTest.php +++ b/tests/system/Database/Live/GetTest.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Database\Live; +use CodeIgniter\Database\BaseResult; use CodeIgniter\Database\Exceptions\DatabaseException; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\DatabaseTestTrait; @@ -227,13 +228,12 @@ public function testGetAnotherDataSeek(): void public function testFreeResult(): void { $data = $this->db->table('job')->where('id', 4)->get(); + $this->assertInstanceOf(BaseResult::class, $data); $details = $data->getResult(); - $this->assertSame('Musician', $details[0]->name); $data->freeResult(); - $this->assertFalse($data->resultID); } @@ -292,6 +292,7 @@ public function testGetNextRow(): void public function testGetPreviousRow(): void { $user = $this->db->table('user')->get(); + $this->assertInstanceOf(BaseResult::class, $user); $user->currentRow = 3; diff --git a/tests/system/Test/FabricatorTest.php b/tests/system/Test/FabricatorTest.php index 1c3609f29b14..0af1329aad8a 100644 --- a/tests/system/Test/FabricatorTest.php +++ b/tests/system/Test/FabricatorTest.php @@ -16,6 +16,7 @@ use CodeIgniter\Config\Factories; use CodeIgniter\Model; use PHPUnit\Framework\Attributes\Group; +use stdClass; use Tests\Support\Models\EntityModel; use Tests\Support\Models\EventModel; use Tests\Support\Models\FabricatorModel; @@ -410,6 +411,7 @@ public function testCreateMockSetsDatabaseFields(): void $fabricator = new Fabricator(FabricatorModel::class); $result = $fabricator->create(null, true); + $this->assertInstanceOf(stdClass::class, $result); $this->assertIsInt($result->id); $this->assertIsInt($result->created_at); diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 93241798b548..58a8f091d28e 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 1446 errors +# total 1434 errors includes: - argument.type.neon @@ -13,7 +13,6 @@ includes: - missingType.iterableValue.neon - missingType.parameter.neon - property.defaultValue.neon - - property.nonObject.neon - property.notFound.neon - property.phpDocType.neon - staticMethod.notFound.neon diff --git a/utils/phpstan-baseline/property.nonObject.neon b/utils/phpstan-baseline/property.nonObject.neon deleted file mode 100644 index 7268e376d119..000000000000 --- a/utils/phpstan-baseline/property.nonObject.neon +++ /dev/null @@ -1,48 +0,0 @@ -# total 12 errors - -parameters: - ignoreErrors: - - - message: '#^Cannot access property \$insert_id on object\|resource\|false\.$#' - count: 1 - path: ../../system/Test/Mock/MockConnection.php - - - - message: '#^Cannot access property \$name on array\|object\.$#' - count: 1 - path: ../../tests/system/Database/Live/FabricatorLiveTest.php - - - - message: '#^Cannot access property \$currentRow on CodeIgniter\\Database\\ResultInterface\|false\.$#' - count: 1 - path: ../../tests/system/Database/Live/GetTest.php - - - - message: '#^Cannot access property \$name on array\|object\.$#' - count: 4 - path: ../../tests/system/Database/Live/GetTest.php - - - - message: '#^Cannot access property \$resultID on CodeIgniter\\Database\\ResultInterface\|false\.$#' - count: 1 - path: ../../tests/system/Database/Live/GetTest.php - - - - message: '#^Cannot access property \$created_at on array\|object\.$#' - count: 1 - path: ../../tests/system/Test/FabricatorTest.php - - - - message: '#^Cannot access property \$deleted_at on array\|object\.$#' - count: 1 - path: ../../tests/system/Test/FabricatorTest.php - - - - message: '#^Cannot access property \$id on array\|object\.$#' - count: 1 - path: ../../tests/system/Test/FabricatorTest.php - - - - message: '#^Cannot access property \$updated_at on array\|object\.$#' - count: 1 - path: ../../tests/system/Test/FabricatorTest.php