mglaman / phpstan-drupal
Drupal extension and rules for PHPStan
Fund package maintenance!
mglaman
Open Collective
Tidelift
Installs: 18 143 377
Dependents: 86
Suggesters: 0
Security: 0
Stars: 196
Watchers: 9
Forks: 76
Open Issues: 103
Type:phpstan-extension
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.3.1
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.3.1
- 1.3.0
- 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-error-identifiers
- dev-mglaman-patch-1
- 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-11-01 12:34:12 UTC
README
Extension for PHPStan to allow analysis of Drupal code.
PHPStan is able to discover symbols by using autoloading provided by Composer. However, Drupal does not provide autoloading information for modules and themes. This project registers those namespaces so that PHPStan can properly discover symbols in your Drupal code base automatically.
Sponsors
Usage
When you are using phpstan/extension-installer
, phpstan.neon
will be automatically included.
Manual installation
If you don't want to use phpstan/extension-installer
, include extension.neon
in your project's PHPStan config:
includes:
- vendor/mglaman/phpstan-drupal/extension.neon
To include Drupal specific analysis rules, include this file:
includes:
- vendor/mglaman/phpstan-drupal/rules.neon
Getting help
Ask for assistance in the discussions or #phpstan channel on Drupal Slack.
Excluding tests from analysis
To exclude tests from analysis, add the following parameter
parameters:
excludePaths:
- *Test.php
- *TestBase.php
Deprecation testing
This project depends on phpstan/phpstan-deprecation-rules
which adds deprecation rules. We provide Drupal-specific
deprecated scope resolvers.
To only handle deprecation testing, use a phpstan.neon
like this:
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
To disable deprecation rules while using phpstan/extension-installer
, you can do the following:
{ "extra": { "phpstan/extension-installer": { "ignore": [ "phpstan/phpstan-deprecation-rules" ] } } }
See the extension-installer
documentation for more information: https://github.com/phpstan/extension-installer#ignoring-a-particular-extension
Adapting to your project
Customizing rules
Disabling checks for extending @internal
classes
You can disable the ClassExtendsInternalClassRule
rule by adding the following to your phpstan.neon
:
parameters: drupal: rules: classExtendsInternalClassRule: false
Entity storage mappings.
The EntityTypeManagerGetStorageDynamicReturnTypeExtension
service helps map dynamic return types. This inspects the
passed entity type ID and tries to return a known storage class, besides the default EntityStorageInterface
. The
default mapping can be found in extension.neon
. For example:
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
To add support for custom entities, you may add the same definition in your project's phpstan.neon
. See the following
example for adding a mapping for 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
Similarly, the EntityStorageDynamicReturnTypeExtension
service helps to determine the type of the entity which is
loaded, created etc.. when using an entity storage.
For instance when using
$node = \Drupal::entityTypeManager()->getStorage('node')->create(['type' => 'page', 'title' => 'foo']);
It helps with knowing the type of the $node
variable is Drupal\node\Entity\Node
.
The default mapping can be found in 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
To add support for custom entities, you may add the same definition in your project's phpstan.neon
likewise.
Providing entity type mappings for a contrib module
Contributed modules can provide their own mapping that can be automatically registered with a user's code base when
they use the phpstan/extension-installer
. The extension installer scans installed package's composer.json
for a
value in extra.phpstan
. This will automatically bundle the defined include that contains an entity mapping
configuration.
For example, the Paragraphs module could have the following entity_mapping.neon
file:
parameters: drupal: entityMapping: paragraph: class: Drupal\paragraphs\Entity\Paragraph paragraphs_type: class: Drupal\paragraphs\Entity\ParagraphsType
Then in the composer.json
for Paragraphs, the entity_mapping.neon
would be provided as a PHPStan include
{ "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" ] } } }