All type inference features of this extension are summarised below:
Class: CodeIgniter\PHPStan\Type\ServicesReturnTypeExtension
This extension provides precise return types for the service() and single_service() functions.
Note
Configuration:
You can instruct PHPStan to consider your own services factory classes.
Please note that it should be a valid class extending CodeIgniter\Config\BaseService!
parameters:
codeigniter:
additionalServices:
- Acme\Blog\Config\ServicesClass: CodeIgniter\PHPStan\Type\FactoriesFunctionReturnTypeExtension
This extension provides precise return types for the config() and model() functions, resolving the
class from the passed name or class string.
Note
Configuration:
You can instruct PHPStan to consider additional namespaces when resolving the passed name to a class.
parameters:
codeigniter:
additionalConfigNamespaces:
- Acme\Blog\Config\
additionalModelNamespaces:
- Acme\Blog\Models\Class: CodeIgniter\PHPStan\Type\FakeFunctionReturnTypeExtension
This extension provides the precise return type for the fake() function, typing it as a single fabricated
record of the given model's return type (an entity, a shaped array, or a stdClass). The model may be passed
as a class string, a model name, or a model instance.
Class: CodeIgniter\PHPStan\Type\UrlHelperFunctionReturnTypeExtension
This extension resolves the string|URI url helpers current_url() and previous_url() from their
constant $returnObject flag: a constant true returns a CodeIgniter\HTTP\URI, while false or an
absent flag returns a string. A non-constant flag leaves the declared union in place.
Class: CodeIgniter\PHPStan\Type\ReflectionHelperMethodInvokerReturnTypeExtension
This extension provides precise return type for the method call to
ReflectionHelper::getPrivateMethodInvoker() (i.e., $this->getPrivateMethodInvoker()).
This is enabled by default for tests extending CodeIgniter\Test\CIUnitTestCase.
Note
Configuration:
If you are using ReflectionHelper outside of testing, you can still enjoy the precise return types by adding a
service for the class using this trait. In your phpstan.neon (or phpstan.dist.neon), add the following to
the services schema:
-
class: CodeIgniter\PHPStan\Type\ReflectionHelperMethodInvokerReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
arguments:
class: <Fully qualified class name of class using ReflectionHelper>
Class: CodeIgniter\PHPStan\Type\SuperglobalsMethodDynamicReturnTypeExtension
This extension provides precise return types for the following methods of CodeIgniter\Superglobals:
server()get()post()cookie()request()getGlobalArray()
Class: CodeIgniter\PHPStan\Type\ModelFindReturnTypeExtension
This extension provides precise return types for the find(), findAll(), first(), and findColumn()
methods of CodeIgniter\Model subclasses.
A fetched row is typed from the model's $returnType:
- an entity instance (whose properties are typed by the entity extension below, with the producing model's
$castslayered on so anasObject(SomeEntity::class)fetch reflects that model's casts, not only the entity's own model's), - a shaped array built from the table's columns and the model's
$casts, or - a
stdClasswith those same fields.
The row type also honors an asArray()/asObject() override and a select() field list earlier in the call
chain. A select() supports column, table.column, as aliases, and table.*, resolving qualified
references (including joined tables) against the introspected schema. A non-constant or unparseable select()
falls back to a generic array.
Each method then wraps that row type:
find(): a single row ornullfor a scalar id, a list of rows for an array of ids or no argument.findAll(): a list of rows.first(): a single row ornull.findColumn(): a list of the selected column's values, ornull.
Class: CodeIgniter\PHPStan\Type\ResultMethodReturnTypeExtension
This extension sharpens the row-set accessors of CodeIgniter\Database\ResultInterface, which the
framework declares as a bare array or a loose object union at the interface level:
getResultArray():list<array<string, mixed>>getResultObject():list<stdClass>getResult($type): a list keyed off the constant$type, namelylist<array<string, mixed>>for'array',list<stdClass>for'object'(the default), andlist<T>for aclass-string.getRowArray():array<string, mixed>|nullgetRowObject():stdClass|nullgetCustomResultObject($className):list<T>when$classNameis a constantclass-string, or a list of unknown objects otherwise.
Class: CodeIgniter\PHPStan\Type\RequestMethodReturnTypeExtension
This extension sharpens two CodeIgniter\HTTP\Request accessors the framework declares as bare mixed
or a loose JSON union:
getServer($index): a null or absent$indexreturns the wholearray<string, mixed>. A constant key in the server map (argv,argc,REQUEST_TIME,REQUEST_TIME_FLOAT) resolves to its mapped type, and any other constant key tostring, each withnullfor an absent key. A filter argument or a non-constant key leaves the declaredmixed.getJSON($assoc): a constanttruedecodes objects as associative arrays, sostdClassis dropped from the declaredarray|bool|float|int|stdClass|null. Afalse, absent, or non-constant$assocleaves the union in place.
Class: CodeIgniter\PHPStan\Type\IncomingRequestMethodReturnTypeExtension
This extension sharpens the CodeIgniter\HTTP\IncomingRequest input accessors built on
RequestTrait::fetchGlobal() (getGet, getPost, getCookie, getPostGet, getGetPost, and
getRawInputVar), all declared as the loose union array|bool|float|int|object|string|null. The
$index argument shape drives the result:
- a null or absent index returns the whole global as
array<string, mixed>, - an array of keys returns
array<string, mixed>, - a constant string index returns
array<mixed>|string|null(the value can be a nested array, or null when the key is absent).getRawInputVardefers here, as its dotted-path leaf does not narrow usefully.
A filter argument (the second parameter) or a non-constant index leaves the declared union in place.
getVar() is not handled: it falls back to the JSON input stream, so its result cannot be narrowed
soundly.
Class: CodeIgniter\PHPStan\Type\CLIRequestMethodReturnTypeExtension
On the CLI these same accessors (getGet, getPost, getCookie, getPostGet, getGetPost) never read
a real superglobal. They return an empty array for a null or array index and null for a string index,
narrowing the declared array|null accordingly.
Class: CodeIgniter\PHPStan\Type\ReflectionHelperMethodInvokerStaticReturnTypeExtension
This extension provides precise return type for the static method call to
ReflectionHelper::getPrivateMethodInvoker() (i.e., self::getPrivateMethodInvoker()).
This is enabled by default for tests extending CodeIgniter\Test\CIUnitTestCase.
Note
Configuration:
If you are using ReflectionHelper outside of testing, you can still enjoy the precise return types by adding a
service for the class using this trait. In your phpstan.neon (or phpstan.dist.neon), add the following to
the services schema:
-
class: CodeIgniter\PHPStan\Type\ReflectionHelperMethodInvokerStaticReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
arguments:
class: <Fully qualified class name of class using ReflectionHelper>
Class: CodeIgniter\PHPStan\Type\ServicesGetSharedInstanceReturnTypeExtension
This extension provides precise return type for the static getSharedInstance() method of services class.
Note
Configuration:
You can instruct PHPStan to consider your own services factory classes.
Please note that it should be a valid class extending CodeIgniter\Config\BaseService!
parameters:
codeigniter:
additionalServices:
- Acme\Blog\Config\ServicesClass: CodeIgniter\PHPStan\Type\CacheFactoryGetHandlerReturnTypeExtension
This extension provides precise return types for the static CacheFactory::getHandler() method, resolving
the handler class from the validHandlers of the passed Config\Cache and the requested handler and backup
handler names.
Note
Configuration:
By default, only the resolved primary handler is used as the return type. To also include the backup handler in the inferred return type, enable:
parameters:
codeigniter:
addBackupHandlerAsReturnType: trueClass: CodeIgniter\PHPStan\Type\FactoriesStaticMethodReturnTypeExtension
This extension provides precise return types for the static factory entry points Factories::config(),
Factories::models(), and Factories::get(), the static counterparts of the config() and model()
functions. A class-string argument resolves to that class for any component, while a string alias resolves
through the config/model namespaces (including the additionalConfigNamespaces and
additionalModelNamespaces settings of the FactoriesFunctionReturnTypeExtension).
Class: CodeIgniter\PHPStan\Reflection\EntityPropertiesClassReflectionExtension
This extension types the virtual properties of CodeIgniter\Entity\Entity subclasses. For each property it
applies, in order, the entity's $dates, the entity's $casts, then the $casts of the model whose
$returnType is the entity (CodeIgniter applies those before hydrating the entity), and finally the type of
the backing database column. Custom $castHandlers are resolved by reflecting their get() method, and the
backing column is found through the model's table. Properties that match none of these resolve to mixed.
Note
Configuration:
The Model and Entity column types are derived from a live schema, built by running your migrations against a
throwaway SQLite database (this requires the sqlite3 PHP extension). By default every registered namespace
is scanned (your app plus installed packages). To restrict the schema to a single namespace, set:
parameters:
codeigniter:
schemaNamespace: Acme\Blog