phpstan的扩展程序允许分析Drupal代码。
PHPSTAN能够使用作曲家提供的自动加载来发现符号。但是,Drupal并未为模块和主题提供自动加载信息。该项目注册了这些名称空间,以便Phpstan可以自动在Drupal代码库中正确发现符号。
您想赞助吗?
当您使用phpstan/extension-installer时,将自动包括phpstan.neon 。
如果您不想使用phpstan/extension-installer ,请在项目的PHPSTAN配置中包含extension.neon :
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类的支票您可以通过将以下内容添加到ClassExtendsInternalClassRule : phpstan.neon :
parameters :
drupal :
rules :
classExtendsInternalClassRule : false EntityTypeManagerGetStorageDynamicReturnTypeExtension服务有助于映射动态返回类型。这检查了传递的实体类型ID,并尝试返回已知的存储类,除了默认的EntityStorageInterface 。默认映射可以在extension.neon中找到。例如:
parameters:
drupal:
entityMapping:
block:
class: DrupalblockEntityBlock
storage: DrupalCoreConfigEntityConfigEntityStorage
node:
class: DrupalnodeEntityNode
storage: DrupalnodeNodeStorage
taxonomy_term:
class: DrupaltaxonomyEntityTerm
storage: DrupaltaxonomyTermStorage
user:
class: DrupaluserEntityUser
storage: DrupaluserUserStorage
为了增加对自定义实体的支持,您可以在项目的phpstan.neon中添加相同的定义。请参阅以下示例以添加搜索API的映射:
parameters:
drupal:
entityMapping:
search_api_index:
class: Drupalsearch_apiEntityIndex
storage: Drupalsearch_apiEntitySearchApiConfigEntityStorage
search_api_server:
class: Drupalsearch_apiEntityServer
storage: Drupalsearch_apiEntitySearchApiConfigEntityStorage
同样, EntityStorageDynamicReturnTypeExtension服务有助于确定使用实体存储时已加载,创建等实体的类型。例如使用
$ node = Drupal:: entityTypeManager ()-> getStorage ( ' node ' )-> create ([ ' type ' => ' page ' , ' title ' => ' foo ' ]);它有助于了解$node变量的类型是DrupalnodeEntityNode 。
默认映射可以在extension.neon中找到:
parameters :
drupal :
entityMapping :
block :
class : DrupalblockEntityBlock
storage : DrupalCoreConfigEntityConfigEntityStorage
node :
class : DrupalnodeEntityNode
storage : DrupalnodeNodeStorage
taxonomy_term :
class : DrupaltaxonomyEntityTerm
storage : DrupaltaxonomyTermStorage
user :
class : DrupaluserEntityUser
storage : DrupaluserUserStorage为了增加对自定义实体的支持,您可以在项目的phpstan.neon中添加相同的定义。
贡献的模块可以提供自己的映射,当他们使用phpstan/extension-installer时,可以自动在用户代码库中注册。扩展程序安装程序扫描安装了Package的composer.json ,以备extra.phpstan中的值。这将自动捆绑定义的包含包含实体映射配置的定义。
例如,段落模块可以具有以下entity_mapping.neon文件:
parameters :
drupal :
entityMapping :
paragraph :
class : DrupalparagraphsEntityParagraph
paragraphs_type :
class : DrupalparagraphsEntityParagraphsType然后在段落的composer.json entity_mapping.neon
{
"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 "
]
}
}
}