raul338/cakephp-phpstan-extensions

为 CakePHP 项目提供辅助 phpstan 分析的服务

安装数: 36,268

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:phpstan-extension

3.0.1 2024-09-21 02:51 UTC

This package is auto-updated.

Last update: 2024-09-21 02:52:38 UTC


README

CI

为 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