raul338 / cakephp-phpstan-extensions
为 CakePHP 项目提供辅助 phpstan 分析的服务
3.0.1
2024-09-21 02:51 UTC
Requires
- php: ^7.4|~8.0
- phpstan/phpstan: ^1.10
Requires (Dev)
- cakephp/cakephp: ^4.0
- cakephp/cakephp-codesniffer: ^4.0
- dealerdirect/phpcodesniffer-composer-installer: ^1.0.0
- friendsofcake/crud: ^6.2
- phpunit/phpunit: ^9.6
README
为 CakePHP 项目提供辅助 phpstan 分析的服务
安装
composer require --dev raul338/cakephp-phpstan-extensions
如果安装了 phpstan/extension-installer,则此扩展将自动加载
composer require --dev phpstan/extension-installer
或者如果您不使用 phpstan/extension-installer,请在 phpstan.neon 中包含
includes:
- vendor/raul338/cakephp-phpstan-extensions/src/cakephp-extensions.neon
这如何帮助我
此扩展包含规则,用于分析以下代码片段,无需使用 var 注释
动态查找器
$query = $this->Users->findAllByUsername('joebob');
检测来自行为的方法
如果方法名称在 implementedMethods 中未被修改,则此功能才会工作。否则,分析可能不正确,或者您可能需要使用 dockblocks 装饰您的代码
/** * @mixin \Cake\ORM\Behavior\TimestampBehavior */ public class UsersTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->addBehavior('Timestamp'); } } // somewhere else - phpstan will know its the Timestamp touch method $this->Users->touch($user);
FriendsOfCake/crud 动作 & 监听器
此功能用于与 FriendsOfCake/crud 一起使用
public function add() { // phpstan will know action() is a AddAction instead of BaseAction $this->Crud->action()->saveOptions([]); // phpstan will know it is a \Crud\Listener\RelatedModelsListener $this->Crud->listener('relatedModels')->relatedModels(true); }
它只会在您使用默认操作之一时工作
- 添加
- 删除
- 编辑
- 索引
- 查看
以及西班牙语名称
- agregar
- editar
- borrar
- ver
如果您使用带有 crud 的操作,您将需要这样做
public function custom() { $this->Crud->mapAction('custom', 'Crud.Index'); /** @var \Crud\Action\IndexAction */ $action = $this->Crud->action(); }
FriendsOfCake/crud 事件主题
告诉 phpstan,如果事件在控制器内部,主题可能是一个 CrudSubject
示例
$this->Crud->on('beforePaginate', function (Event $event) { $query = $event->getSubject()->query; $query->where([ /** ... */]); });
许可证
MIT