Skip to content

refactor: Improve types for phpstan #9685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ function view_cell(string $library, $params = null, int $ttl = 0, ?string $cache
/**
* Get the class "basename" of the given object / class.
*
* @param object|string $class
* @param class-string|object $class
*
* @return string
*
Expand All @@ -1244,9 +1244,9 @@ function class_basename($class)
/**
* Returns all traits used by a class, its parent classes and trait of their traits.
*
* @param object|string $class
* @param class-string|object $class
*
* @return array
* @return array<class-string, class-string>
*
* @codeCoverageIgnore
*/
Expand All @@ -1270,9 +1270,9 @@ function class_uses_recursive($class)
/**
* Returns all traits used by a trait and its traits.
*
* @param string $trait
* @param class-string $trait
*
* @return array
* @return array<class-string, class-string>
*
* @codeCoverageIgnore
*/
Expand Down
41 changes: 22 additions & 19 deletions system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Database\MigrationRunner;
use CodeIgniter\Database\Seeder;
use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\Header;
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Test\Mock\MockCache;
Expand Down Expand Up @@ -72,6 +73,8 @@ abstract class CIUnitTestCase extends TestCase

/**
* Store of identified traits.
*
* @var array<class-string, class-string>|null
*/
private ?array $traits = null;

Expand Down Expand Up @@ -109,9 +112,9 @@ abstract class CIUnitTestCase extends TestCase

/**
* The seed file(s) used for all tests within this test case.
* Should be fully-namespaced or relative to $basePath
* Should be fully-namespaced or relative to $basePath.
*
* @var class-string<Seeder>|list<class-string<Seeder>>
* @var ''|class-string<Seeder>|list<class-string<Seeder>>
*/
protected $seed = '';

Expand All @@ -127,9 +130,9 @@ abstract class CIUnitTestCase extends TestCase
* The namespace(s) to help us find the migration classes.
* `null` is equivalent to running `spark migrate --all`.
* Note that running "all" runs migrations in date order,
* but specifying namespaces runs them in namespace order (then date)
* but specifying namespaces runs them in namespace order (then date).
*
* @var array|string|null
* @var list<string>|string|null
*/
protected $namespace = 'Tests\Support';

Expand All @@ -156,17 +159,17 @@ abstract class CIUnitTestCase extends TestCase
protected $migrations;

/**
* Seeder instance
* Seeder instance.
*
* @var Seeder
* @var Seeder|null
*/
protected $seeder;

/**
* Stores information needed to remove any
* rows inserted via $this->hasInDatabase();
* rows inserted via $this->hasInDatabase().
*
* @var array
* @var list<array<int|string, mixed>>
*/
protected $insertCache = [];

Expand All @@ -186,27 +189,27 @@ abstract class CIUnitTestCase extends TestCase
* Values to be set in the SESSION global
* before running the test.
*
* @var array
* @var array<int|string, mixed>
*/
protected $session = [];

/**
* Enabled auto clean op buffer after request call
* Enabled auto clean op buffer after request call.
*
* @var bool
*/
protected $clean = true;

/**
* Custom request's headers
* Custom request's headers.
*
* @var array
* @var array<string, Header|list<Header>>
*/
protected $headers = [];

/**
* Allows for formatting the request body to what
* the controller is going to expect
* the controller is going to expect.
*
* @var string
*/
Expand Down Expand Up @@ -276,7 +279,7 @@ protected function tearDown(): void
* Checks for traits with corresponding
* methods for setUp or tearDown.
*
* @param string $stage 'setUp' or 'tearDown'
* @param 'setUp'|'tearDown' $stage
*/
private function callTraitMethods(string $stage): void
{
Expand All @@ -298,7 +301,7 @@ private function callTraitMethods(string $stage): void
// --------------------------------------------------------------------

/**
* Resets shared instanced for all Factories components
* Resets shared instanced for all Factories components.
*
* @return void
*/
Expand All @@ -308,7 +311,7 @@ protected function resetFactories()
}

/**
* Resets shared instanced for all Services
* Resets shared instanced for all Services.
*
* @return void
*/
Expand All @@ -318,7 +321,7 @@ protected function resetServices(bool $initAutoloader = true)
}

/**
* Injects the mock Cache driver to prevent filesystem collisions
* Injects the mock Cache driver to prevent filesystem collisions.
*
* @return void
*/
Expand All @@ -328,7 +331,7 @@ protected function mockCache()
}

/**
* Injects the mock email driver so no emails really send
* Injects the mock email driver so no emails really send.
*
* @return void
*/
Expand All @@ -338,7 +341,7 @@ protected function mockEmail()
}

/**
* Injects the mock session driver into Services
* Injects the mock session driver into Services.
*
* @return void
*/
Expand Down
12 changes: 10 additions & 2 deletions system/Test/DatabaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
namespace CodeIgniter\Test;

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\MigrationRunner;
use CodeIgniter\Database\Seeder;
use CodeIgniter\Test\Constraints\SeeInDatabase;
use Config\Database;
use Config\Migrations;
use Config\Services;
use PHPUnit\Framework\Attributes\AfterClass;

/**
Expand All @@ -27,6 +29,12 @@
* Provides functionality for refreshing/seeding
* the database during testing.
*
* @property BaseConnection $db
* @property list<array<int|string, mixed>> $insertCache
* @property Seeder|null $seeder
* @property MigrationRunner|null $migrations
* @property list<string>|string|null $namespace
*
* @mixin CIUnitTestCase
*/
trait DatabaseTestTrait
Expand Down Expand Up @@ -88,7 +96,7 @@ public function loadDependencies()
$config = new Migrations();
$config->enabled = true;

$this->migrations = Services::migrations($config, $this->db, false);
$this->migrations = service('migrations', $config, $this->db, false);
$this->migrations->setSilent(false);
}

Expand Down
21 changes: 18 additions & 3 deletions system/Test/FeatureTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@

namespace CodeIgniter\Test;

use Closure;
use CodeIgniter\Events\Events;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\Header;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\Method;
use CodeIgniter\HTTP\Request;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\URI;
use CodeIgniter\Router\RouteCollection;
use Config\App;
use Config\Services;
use Exception;
Expand All @@ -30,6 +34,12 @@
*
* Provides additional utilities for doing full HTTP testing
* against your application in trait format.
*
* @property array<int|string, mixed> $session
* @property array<string, Header|list<Header>> $headers
* @property RouteCollection|null $routes
*
* @mixin CIUnitTestCase
*/
trait FeatureTestTrait
{
Expand All @@ -42,7 +52,12 @@ trait FeatureTestTrait
* ['GET', 'home', 'Home::index'],
* ]
*
* @param array|null $routes Array to set routes
* @param array<int, array{
* 0: string,
* 1: string,
* 2: ((Closure(mixed...): (ResponseInterface|string|void)))|string,
* 3?: array<string, mixed>
* }>|null $routes Array to set routes
*
* @return $this
*/
Expand Down Expand Up @@ -84,7 +99,7 @@ protected function withRoutes(?array $routes = null)
/**
* Sets any values that should exist during this session.
*
* @param array|null $values Array of values, or null to use the current $_SESSION
* @param array<int|string, mixed>|null $values Array of values, or null to use the current $_SESSION
*
* @return $this
*/
Expand All @@ -103,7 +118,7 @@ public function withSession(?array $values = null)
* 'Authorization' => 'Token'
* ])
*
* @param array $headers Array of headers
* @param array<string, Header|list<Header>> $headers Array of headers
*
* @return $this
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class DatabaseTestCaseMigrationOnce1Test extends CIUnitTestCase
* Note that running "all" runs migrations in date order,
* but specifying namespaces runs them in namespace order (then date)
*
* @var array|string|null
* @var list<string>|string|null
*/
protected $namespace = [
'Tests\Support\MigrationTestMigrations',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ final class DatabaseTestCaseMigrationOnce2Test extends CIUnitTestCase
* Note that running "all" runs migrations in date order,
* but specifying namespaces runs them in namespace order (then date)
*
* @var array|string|null
* @var list<string>|string|null
*/
protected $namespace = [
'Tests\Support\MigrationTestMigrations',
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Database/DatabaseTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class DatabaseTestCaseTest extends CIUnitTestCase
* The seed file(s) used for all tests within this test case.
* Should be fully-namespaced or relative to $basePath
*
* @var array|string
* @var ''|class-string<Seeder>|list<class-string<Seeder>>
*/
protected $seed = [
CITestSeeder::class,
Expand All @@ -53,7 +53,7 @@ final class DatabaseTestCaseTest extends CIUnitTestCase
* Note that running "all" runs migrations in date order,
* but specifying namespaces runs them in namespace order (then date)
*
* @var array|string|null
* @var list<string>|string|null
*/
protected $namespace = [
'Tests\Support',
Expand Down
3 changes: 2 additions & 1 deletion tests/system/Database/Live/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Database\Live;

use CodeIgniter\Database\Seeder;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Database;
Expand All @@ -30,7 +31,7 @@ final class MetadataTest extends CIUnitTestCase
/**
* The seed file used for all tests within this test case.
*
* @var string
* @var ''|class-string<Seeder>|list<class-string<Seeder>>
*/
protected $seed = CITestSeeder::class;

Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 2836 errors
# total 2807 errors
includes:
- argument.type.neon
- assign.propertyType.neon
Expand Down
Loading
Loading