mglaman/phpstan-drupal

Drupal 扩展和规则,用于 PHPStan

安装次数: 171,167,782

依赖者: 83

建议者: 0

安全性: 0

星标: 193

关注者: 9

分支: 75

开放问题: 99

类型:phpstan-extension

1.2.12 2024-08-07 21:15 UTC

README

Tests CircleCI

PHPStan 扩展,允许分析 Drupal 代码。

PHPStan 可以通过 Composer 提供的自动加载来 发现符号。然而,Drupal 不为模块和主题提供自动加载信息。此项目注册了这些命名空间,以便 PHPStan 可以自动发现您的 Drupal 代码库中的符号。

赞助商

undpaul Optasy Fame Helsinki

您想赞助吗?

用法

当您使用 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"
      ]
    }
  }
}