reinfi / laminas-typed-params
A Laminas Framework module for typed params plugin to solve static code analysis.
Installs: 11 300
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- laminas/laminas-mvc: ^3.3
- webmozart/assert: ^1.10
Requires (Dev)
- phpunit/phpunit: ^9.5
README
Laminas controller plugin for params to solve static code analysis.
=======
Installation
- Install with Composer:
composer require reinfi/laminas-typed-params
. - Enable the module via config in
appliation.config.php
undermodules
key:
return [ 'modules' => [ 'Reinfi\TypedParams', // other modules ], ];
Configuration
To enable static code analysis to find your controller plugin you need to add it to your controller class.
use Laminas\Mvc\Controller\AbstractActionController; use Reinfi\TypedParams\Plugin\TypedParams; /** * @method TypedParams typedParams() */ class MyController extends AbstractActionController { public function indexAction() { $id = $this->typedParams()->fromRoute('id')->asNonEmptyString(); } }
In this example static code analysis will now know that id is always a non empty string.
Differences
As of now you can not have conditional return types in the @method
Annotation, i.e.
/** * @method ($param is string ? TypedValue : TypedParams) typedParams(string $param, $default = null) */
For that reason the invocation of the plugin always returns the plugin itself and you need to use the public methods.
In difference to the laminas mvc controller plugin params
which returns route parameters if invoked with a parameter.
public function __invoke($param = null, $default = null) { if ($param === null) { return $this; } return $this->fromRoute($param, $default); }