Skip to content

Commit 09fd699

Browse files
committed
Enable CacheFactory::getHandler() return type extension
1 parent 70b7edb commit 09fd699

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

docs/type-inference.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,23 @@ This extension provides precise return type for the static `getSharedInstance()`
128128
> additionalServices:
129129
> - Acme\Blog\Config\Services
130130
> ```
131+
132+
### CacheFactoryGetHandlerReturnTypeExtension
133+
134+
**Class:** `CodeIgniter\PHPStan\Type\CacheFactoryGetHandlerReturnTypeExtension`
135+
136+
This extension provides precise return types for the static `CacheFactory::getHandler()` method, resolving
137+
the handler class from the `validHandlers` of the passed `Config\Cache` and the requested handler and backup
138+
handler names.
139+
140+
> [!NOTE]
141+
> **Configuration:**
142+
>
143+
> By default, only the resolved primary handler is used as the return type. To also include the backup handler
144+
> in the inferred return type, enable:
145+
>
146+
> ```yml
147+
> parameters:
148+
> codeigniter:
149+
> addBackupHandlerAsReturnType: true
150+
> ```

extension.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ parameters:
1010
- CodeIgniter\Config\
1111
additionalModelNamespaces: []
1212
additionalServices: []
13+
addBackupHandlerAsReturnType: false
1314
checkArgumentTypeOfConfig: true
1415
checkArgumentTypeOfModel: true
1516
checkArgumentTypeOfFactories: true
@@ -19,6 +20,7 @@ parametersSchema:
1920
additionalConfigNamespaces: listOf(string())
2021
additionalModelNamespaces: listOf(string())
2122
additionalServices: listOf(string())
23+
addBackupHandlerAsReturnType: bool()
2224
checkArgumentTypeOfConfig: bool()
2325
checkArgumentTypeOfModel: bool()
2426
checkArgumentTypeOfFactories: bool()
@@ -86,6 +88,13 @@ services:
8688
tags:
8789
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
8890

91+
-
92+
class: CodeIgniter\PHPStan\Type\CacheFactoryGetHandlerReturnTypeExtension
93+
arguments:
94+
addBackupHandlerAsReturnType: %codeigniter.addBackupHandlerAsReturnType%
95+
tags:
96+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
97+
8998
-
9099
class: CodeIgniter\PHPStan\Rules\Functions\FactoriesFunctionArgumentTypeRule
91100
arguments:

tests/Type/data/cache-factory.php renamed to tests/data/type-inference/cache-factory-return-types.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
* the LICENSE file that was distributed with this source code.
1212
*/
1313

14-
namespace CodeIgniter\PHPStan\Tests\Fixtures\Type;
14+
namespace CodeIgniter\PHPStan\Tests\Type;
1515

1616
use CodeIgniter\Cache\CacheFactory;
17+
use CodeIgniter\Cache\Handlers\ApcuHandler;
1718
use CodeIgniter\Cache\Handlers\BaseHandler;
1819
use CodeIgniter\Cache\Handlers\DummyHandler;
1920
use CodeIgniter\Cache\Handlers\FileHandler;
@@ -29,6 +30,7 @@
2930
assertType(FileHandler::class, CacheFactory::getHandler($cache));
3031
assertType(FileHandler::class, CacheFactory::getHandler($cache, null));
3132

33+
assertType(ApcuHandler::class, CacheFactory::getHandler($cache, 'apcu'));
3234
assertType(DummyHandler::class, CacheFactory::getHandler($cache, 'dummy'));
3335
assertType(FileHandler::class, CacheFactory::getHandler($cache, 'file'));
3436
assertType(MemcachedHandler::class, CacheFactory::getHandler($cache, 'memcached'));

0 commit comments

Comments
 (0)