ส่วนขยายสำหรับ phpstan เพื่ออนุญาตการวิเคราะห์รหัส Drupal
PHPSTAN สามารถค้นพบสัญลักษณ์โดยใช้การโหลดอัตโนมัติโดยนักแต่งเพลง อย่างไรก็ตาม 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
ขอความช่วยเหลือในการอภิปรายหรือ #PHPSTAN ช่องทาง Drupal Slack
หากต้องการยกเว้นการทดสอบจากการวิเคราะห์เพิ่มพารามิเตอร์ต่อไปนี้
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 ตัวติดตั้งส่วนขยายสแกนสแกน composer.json ของแพ็คเกจที่ติดตั้งสำหรับค่าใน extra.phpstan สิ่งนี้จะรวมกลุ่มที่กำหนดไว้โดยอัตโนมัติซึ่งมีการกำหนดค่าการแมปเอนทิตี
ตัวอย่างเช่นโมดูลย่อหน้าอาจมีไฟล์ entity_mapping.neon ต่อไปนี้ไฟล์:
parameters :
drupal :
entityMapping :
paragraph :
class : DrupalparagraphsEntityParagraph
paragraphs_type :
class : DrupalparagraphsEntityParagraphsType จากนั้นใน entity_mapping.neon composer.json
{
"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 "
]
}
}
}