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 "
]
}
}
}