cakedc/cakephp-phpstan

CakePHP 插件扩展用于 PHPStan。

安装量: 306 313

依赖关系: 17

建议者: 0

安全性: 0

星标: 30

关注者: 15

分支: 5

开放问题: 2

类型:phpstan-extension

3.1.2 2024-02-27 16:33 UTC

This package is auto-updated.

Last update: 2024-08-28 19:27:11 UTC


README

Build Status Downloads Latest Version License

提供服务和规则,以优化 CakePHP 应用程序的 PHPStan 分析,包括解析类型(表、辅助函数、行为等)的多个规则。

安装

要使用此扩展,通过 Composer 引入

composer require --dev cakedc/cakephp-phpstan

如果您还安装了 phpstan/extension-installer,那么您就准备好了!

手动安装

如果您不想使用 phpstan/extension-installer,请将 extension.neon 包含在项目 PHPStan 配置中

includes:
    - vendor/cakedc/cakephp-phpstan/extension.neon

通用类加载/检索扩展

包含的功能

  1. Cake\ORM\Locator\LocatorInterface::get() 提供正确的返回类型
  2. Cake\Controller\Controller::loadComponent() 提供正确的返回类型
  3. Cake\Controller\Controller::fetchTable() 提供正确的返回类型
  4. Cake\Controller\Component::fetchTable() 提供正确的返回类型
  5. Cake\Command\Command::fetchTable() 提供正确的返回类型
  6. Cake\Mailer\Mailer::fetchTable() 提供正确的返回类型
  7. Cake\View\Cell::fetchTable() 提供正确的返回类型
  8. Cake\Console\ConsoleIo::helper() 提供正确的返回类型

表格类返回类型扩展

TableEntityDynamicReturnTypeExtension

  1. 基于您的表格类名称,为 Cake\ORM\Table::get 提供正确的返回类型
  2. 基于您的表格类名称,为 Cake\ORM\Table::newEntity 提供正确的返回类型
  3. 基于您的表格类名称,为 Cake\ORM\Table::newEntities 提供正确的返回类型
  4. 基于您的表格类名称,为 Cake\ORM\Table::newEmptyEntity 提供正确的返回类型
  5. 基于您的表格类名称,为 Cake\ORM\Table::findOrCreate 提供正确的返回类型
示例
  //Now PHPStan know that \App\Models\Table\NotesTable::get returns \App\Model\Entity\Note
  $note = $this->Notes->get(1);
  $note->note = 'My new note';//No error

  //Now PHPStan know that \App\Models\Table\NotesTable::newEntity returns \App\Model\Entity\Note
  $note = $this->Notes->newEntity($data);
  $note->note = 'My new note new entity';//No error

  //Now PHPStan know that \App\Models\Table\NotesTable::newEmptyEntity returns \App\Model\Entity\Note
  $note = $this->Notes->newEmptyEntity($data);
  $note->note = 'My new note new empty entity';//No error

   //Now PHPStan know that \App\Models\Table\NotesTable::findOrCreate returns \App\Model\Entity\Note
  $note = $this->Notes->findOrCreate($data);
  $note->note = 'My entity found or created';//No error

  //Now PHPStan know that \App\Models\Table\NotesTable::newEntities returns \App\Model\Entity\Note[]
  $notes = $this->Notes->newEntities($data);
  foreach ($notes as $note) {
    $note->note = 'My new note';//No error
  }

TableFirstArgIsTheReturnTypeExtension

  1. 基于传递的第一个参数,为 Cake\ORM\Table::patchEntity 提供正确的返回类型
  2. 基于传递的第一个参数,为 Cake\ORM\Table::patchEntities 提供正确的返回类型
  3. 基于传递的第一个参数,为 Cake\ORM\Table::save 提供正确的返回类型
  4. 基于传递的第一个参数,为 Cake\ORM\Table::saveOrFail 提供正确的返回类型
  5. 基于传递的第一个参数,为 Cake\ORM\Table::saveMany 提供正确的返回类型
  6. 基于传递的第一个参数,为 Cake\ORM\Table::saveManyOrFail 提供正确的返回类型
  7. 基于传递的第一个参数,为 Cake\ORM\Table::deleteMany 提供正确的返回类型
  8. 基于传递的第一个参数,为 Cake\ORM\Table::deleteManyOrFail 提供正确的返回类型
示例
  //Now PHPStan know that \App\Models\Table\NotesTable::get returns \App\Model\Entity\Note
  $note = $this->Notes->get(1);
  $notes = $this->Notes->newEntities($data);

  //Since PHPStan knows the type of $note, these methods call use the same type as return type:
  $note = $this->Notes->patchEntity($note, $data);
  $text = $note->note;//No error.

  $note = $this->Notes->save($note);
  $text = $note->note;//No error.

  $note = $this->Notes->saveOrFail($note);
  $text = $note->note;//No error.
  //Since PHPStan knows the type of $notes, these methods call use the same type as return type:
  $notes = $this->Notes->patchEntities($notes);
  $notes = $this->Notes->saveMany($notes);
  $notes = $this->Notes->saveManyOrFail($notes);
  $notes = $this->Notes->deleteMany($notes);
  $notes = $this->Notes->deleteManyOrFail($notes);

规则

此库提供的所有规则都包含在 rules.neon 中,默认启用

AddAssociationExistsTableClassRule

此规则检查在调用 Table::belongsTo、Table::hasMany、Table::belongsToMany、Table::hasOne 和 AssociationCollection::load 时目标关联是否有有效的表格类。

AddAssociationMatchOptionsTypesRule

此规则检查关联选项是否为有效选项类型,基于每个类期望的内容。这涵盖了调用 Table::belongsTo、Table::hasMany、Table::belongsToMany、Table::hasOne 和 AssociationCollection::load。

AddBehaviorExistsClassRule

此规则检查在调用Table::addBehavior和BehaviorRegistry::load时,目标行为是否有有效的表类。

OrmSelectQueryFindMatchOptionsTypesRule

此规则检查传递给Table::find和SelectQuery的选项(参数)是否为有效的查找选项类型。

TableGetMatchOptionsTypesRule

此规则检查传递给Table::get的选项(参数)是否为有效的查找选项类型。

如何禁用规则

每个规则在cakeDC 'namespace'中都有一个参数来启用或禁用,它与规则的名称相同,首字母小写。例如,要禁用AddAssociationExistsTableClassRule规则,你应该有

parameters:
	cakeDC:
	 	addAssociationExistsTableClassRule: false

PHPDoc 扩展

TableAssociationTypeNodeResolverExtension

修复交集关联的phpDoc以正确指定泛型对象类型,例如

\Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable更改为\Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable>

技巧

为了使您的开发更轻松,请确保在您的表类中使用@mixin@method注解。`@mixin`注解将帮助phpstan知道您正在使用行为中的方法,而`@method`注解将允许它知道像Table::get()Table::newEntity()这样的方法的正确返回类型。

您可以使用插件IdeHelper轻松更新注解。

支持

对于错误和功能请求,请使用此存储库的问题部分。

还提供商业支持,联系我们获取更多信息。

贡献

如果您想为此插件贡献新功能、增强或错误修复,请阅读我们的贡献指南以获取详细说明。