mglaman / phpstan-drupal
Drupal 扩展和规则,用于 PHPStan
Requires
- php: ^8.1
- phpstan/phpstan: ^1.10.56
- phpstan/phpstan-deprecation-rules: ^1.1.4
- symfony/finder: ^4.2 || ^5.0 || ^6.0 || ^7.0
- symfony/yaml: ^4.2|| ^5.0 || ^6.0 || ^7.0
- webflo/drupal-finder: ^1.2
Requires (Dev)
- behat/mink: ^1.8
- composer/installers: ^1.9
- drupal/core-recommended: ^10
- drush/drush: ^10.0 || ^11 || ^12 || ^13@beta
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^8.5 || ^9 || ^10 || ^11
- slevomat/coding-standard: ^7.1
- squizlabs/php_codesniffer: ^3.3
- symfony/phpunit-bridge: ^4.4 || ^5.4 || ^6.0 || ^7.0
Suggests
- jangregor/phpstan-prophecy: Provides a prophecy/prophecy extension for phpstan/phpstan.
- phpstan/phpstan-deprecation-rules: For catching deprecations, especially in Drupal core.
- phpstan/phpstan-phpunit: PHPUnit extensions and rules for PHPStan.
- 608.x-dev
- 1.2.12
- 1.2.11
- 1.2.10
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.x-dev
- 1.1.37
- 1.1.36
- 1.1.35
- 1.1.34
- 1.1.33
- 1.1.32
- 1.1.31
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.26
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- dev-main / 1.0.x-dev
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.12.15
- 0.12.14
- 0.12.13
- 0.12.12
- 0.12.11
- 0.12.10
- 0.12.9
- 0.12.8
- 0.12.7
- 0.12.6
- 0.12.5
- 0.12.4
- 0.12.3
- 0.12.2
- 0.12.1
- 0.12.0
- 0.11.14
- 0.11.13
- 0.11.12
- 0.11.11
- 0.11.10
- 0.11.9
- 0.11.8
- 0.11.7
- 0.11.6
- 0.11.5
- 0.11.4
- 0.11.3
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.2
- dev-webflo-accept-deprecations
- dev-fame-sponsorship
- dev-gh734
- dev-bug-693
- dev-fix-RenderCallbackRule
- dev-CallingAddDecoratorOnNull
- dev-release-posts
- dev-gh601-why-needed
- dev-deprecation-scopes
- dev-conditional-return-types-and-generics
- dev-service-id-type
- dev-gh343
This package is auto-updated.
Last update: 2024-09-07 21:34:46 UTC
README
PHPStan 扩展,允许分析 Drupal 代码。
PHPStan 可以通过 Composer 提供的自动加载来 发现符号。然而,Drupal 不为模块和主题提供自动加载信息。此项目注册了这些命名空间,以便 PHPStan 可以自动发现您的 Drupal 代码库中的符号。
赞助商
用法
当您使用 phpstan/extension-installer
时,将自动包含 phpstan.neon
。
手动安装
如果您不想使用 phpstan/extension-installer
,请将 extension.neon
包含在您的项目 PHPStan 配置中。
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
要包含特定于 Drupal 的分析规则,请包含此文件
includes:
- vendor/mglaman/phpstan-drupal/rules.neon
寻求帮助
在 讨论 或 Drupal Slack 的 #phpstan 频道中寻求帮助。
排除测试分析
要排除测试分析,请添加以下参数
parameters:
excludePaths:
- *Test.php
- *TestBase.php
弃用测试
此项目依赖于 phpstan/phpstan-deprecation-rules
,它添加了弃用规则。我们提供特定于 Drupal 的弃用范围解析器。
仅处理弃用测试时,使用类似下面的 phpstan.neon
parameters:
customRulesetUsed: true
reportUnmatchedIgnoredErrors: false
# Ignore phpstan-drupal extension's rules.
ignoreErrors:
- '#\Drupal calls should be avoided in classes, use dependency injection instead#'
- '#Plugin definitions cannot be altered.#'
- '#Missing cache backend declaration for performance.#'
- '#Plugin manager has cache backend specified but does not declare cache tags.#'
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
要禁用使用 phpstan/extension-installer
时的弃用规则,您可以执行以下操作
{ "extra": { "phpstan/extension-installer": { "ignore": [ "phpstan/phpstan-deprecation-rules" ] } } }
有关更多信息,请参阅 extension-installer
文档:https://github.com/phpstan/extension-installer#ignoring-a-particular-extension
适配您的项目
自定义规则
禁用扩展 @internal
类的检查
您可以通过将以下内容添加到您的 phpstan.neon
来禁用 ClassExtendsInternalClassRule
规则
parameters: drupal: rules: classExtendsInternalClassRule: false
指定您的 Drupal 项目的根目录
默认情况下,PHPStan Drupal 扩展将尝试根据 PHPStan 检查的工作目录来确定您的 Drupal 项目的根目录。如果不起作用,您可以使用 drupal.drupal_root
参数显式定义 Drupal 项目的根目录。
parameters:
drupal:
drupal_root: /path/to/drupal
您还可以使用容器参数。例如,您可以将它设置为当前工作目录。
parameters:
drupal:
drupal_root: %currentWorkingDirectory%
实体存储映射。
服务 EntityTypeManagerGetStorageDynamicReturnTypeExtension
帮助映射动态返回类型。它检查传递的实体类型 ID,并尝试返回一个已知存储类,除了默认的 EntityStorageInterface
之外。默认映射可以在 extension.neon
中找到。例如
parameters:
drupal:
entityMapping:
block:
class: Drupal\block\Entity\Block
storage: Drupal\Core\Config\Entity\ConfigEntityStorage
node:
class: Drupal\node\Entity\Node
storage: Drupal\node\NodeStorage
taxonomy_term:
class: Drupal\taxonomy\Entity\Term
storage: Drupal\taxonomy\TermStorage
user:
class: Drupal\user\Entity\User
storage: Drupal\user\UserStorage
要添加对自定义实体的支持,您可以在项目的 phpstan.neon
中添加相同的定义。以下示例说明了添加对 Search API 的映射
parameters:
drupal:
entityMapping:
search_api_index:
class: Drupal\search_api\Entity\Index
storage: Drupal\search_api\Entity\SearchApiConfigEntityStorage
search_api_server:
class: Drupal\search_api\Entity\Server
storage: Drupal\search_api\Entity\SearchApiConfigEntityStorage
类似地,服务 EntityStorageDynamicReturnTypeExtension
帮助确定在使用实体存储时加载、创建等的实体类型。例如,当使用
$node = \Drupal::entityTypeManager()->getStorage('node')->create(['type' => 'page', 'title' => 'foo']);
它有助于知道 $node
变量的类型是 Drupal\node\Entity\Node
。
默认映射可以在 extension.neon
中找到
parameters: drupal: entityMapping: block: class: Drupal\block\Entity\Block storage: Drupal\Core\Config\Entity\ConfigEntityStorage node: class: Drupal\node\Entity\Node storage: Drupal\node\NodeStorage taxonomy_term: class: Drupal\taxonomy\Entity\Term storage: Drupal\taxonomy\TermStorage user: class: Drupal\user\Entity\User storage: Drupal\user\UserStorage
要添加对自定义实体的支持,您可以在项目的 phpstan.neon
中添加相同的定义,同样。
为贡献模块提供实体类型映射
贡献模块可以提供自己的映射,当它们使用 phpstan/extension-installer
时,这些映射可以自动注册到用户的代码库中。扩展安装程序会扫描已安装软件包的 composer.json
文件,查找 extra.phpstan
中的值。这将自动捆绑包含实体映射配置的已定义包含文件。
例如,段落模块可以有以下 entity_mapping.neon
文件
parameters: drupal: entityMapping: paragraph: class: Drupal\paragraphs\Entity\Paragraph paragraphs_type: class: Drupal\paragraphs\Entity\ParagraphsType
然后在段落的 composer.json
中,将 entity_mapping.neon
提供为 PHPStan 包含
{ "name": "drupal/paragraphs", "description": "Enables the creation of Paragraphs entities.", "type": "drupal-module", "license": "GPL-2.0-or-later", "require": { "drupal/entity_reference_revisions": "~1.3" }, "extra": { "phpstan": { "includes": [ "entity_mapping.neon" ] } } }