# PHPStan configuration for Gutenberg # # To override this configuration, create a phpstan.neon which includes this file and adjusts its parameters as # needed, rather than copying it, so that the copy does not go stale. See docs/contributors/code/coding-guidelines.md # for examples. # # https://phpstan.org/config-reference includes: # The base configuration file for using PHPStan with the Gutenberg codebase. - tools/phpstan/base.neon # Preexisting errors that should be ignored. Regenerate with `composer phpstan:baselines`, or remove the entries # for fixed errors with `composer phpstan:fix`. # https://phpstan.org/user-guide/baseline - tools/phpstan/baseline.neon # Rules from phpstan-strict-rules which Gutenberg already complies with. The others are not enabled until the code # complies with them, but they can be enabled in a local phpstan.neon to work toward that. See # docs/contributors/code/coding-guidelines.md. # # These are registered individually, rather than by including phpstan-strict-rules' rules.neon with only these # enabled, because that file also makes several PHPStan parameters stricter even when its rules are disabled. That is # also why composer.json tells phpstan/extension-installer to ignore the package, since it would otherwise include # rules.neon automatically. # https://github.com/phpstan/phpstan-strict-rules rules: # strictRules.disallowedLooseComparison is registered under `services` below, since it takes an argument. # strictRules.numericOperandsInArithmeticOperators is registered under `services` below, since its rules need a helper. # strictRules.disallowedBacktick - PHPStan\Rules\DisallowedConstructs\DisallowedBacktickRule # strictRules.disallowedImplicitArrayCreation - PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule # strictRules.disallowedShortTernary - PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule # strictRules.closureUsesThis - PHPStan\Rules\Functions\ClosureUsesThisRule # strictRules.matchingInheritedMethodNames - PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule # strictRules.switchConditionsMatchingType - PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule # strictRules.strictFunctionCalls, which for in_array(), array_search(), and array_keys() is also enforced by the # WordPress.PHP.StrictInArray sniff. - PHPStan\Rules\StrictCalls\StrictFunctionCallsRule # strictRules.illegalConstructorMethodCall - PHPStan\Rules\Methods\IllegalConstructorMethodCallRule - PHPStan\Rules\Methods\IllegalConstructorStaticCallRule # strictRules.dynamicCallOnStaticMethod - PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule - PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsCallableRule services: # Ignores strictRules.dynamicCallOnStaticMethod errors for $this->assert*() calls in tests, since PHPUnit's # assertions are static but conventionally called on $this, as in WordPress core's tests. The phpstan-phpunit # extension registers this only with bleedingEdge. It matters only to a local phpstan.neon analyzing phpunit/. - class: PHPStan\Type\PHPUnit\DynamicCallToAssertionIgnoreExtension autowired: false tags: - phpstan.ignoreErrorExtension # strictRules.disallowedLooseComparison, with the argument rules.neon passes it. - class: PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule arguments: includeOperandTypesInErrorMessage: %featureToggles.bleedingEdge% tags: - phpstan.rules.rule # strictRules.numericOperandsInArithmeticOperators # Its rules need an OperatorRuleHelper service. The helper is registered with autowired: false and passed explicitly # to the twelve rules so that a local phpstan.neon including rules.neon, which also registers one, does not fail with # "Multiple services of type PHPStan\Rules\Operators\OperatorRuleHelper found". The unary minus and plus rules are # registered too, although rules.neon enables them only with bleedingEdge, since the code complies with them. operatorRuleHelper: class: PHPStan\Rules\Operators\OperatorRuleHelper autowired: false - class: PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandInArithmeticUnaryMinusRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandInArithmeticUnaryPlusRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandsInArithmeticModuloRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule - class: PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule arguments: helper: @operatorRuleHelper tags: - phpstan.rules.rule parameters: # https://phpstan.org/user-guide/rule-levels level: 5 reportUnmatchedIgnoredErrors: true # Errors that are intentionally ignored, as opposed to the baseline, which records errors still to be fixed. # # Each entry names the file its error occurs in with `path`, unless it is a rule for every file, which is marked # `reportUnmatched: false` instead, so that it is not reported when no file breaks it. An entry # that names no file applies to every file, so phpstan-strict.neon.dist, which analyzes only some of them, would # report it as unmatched when its error occurs only in the others. An error occurring in several files gets an entry # for each, since PHPStan reports an entry naming several files with `paths` as unmatched even when it analyzes none # of them. ignoreErrors: # Level 0: # Return values from action callbacks are discarded by do_action(), but returning one is harmless and lets # callers and tests know whether the callback did anything. Core has many such callbacks, like _wp_cron() and # wp_set_up_cross_origin_isolation(). - message: '#^Action callback returns .+ but should not return anything\.$#' identifier: return.void reportUnmatched: false # wp_get_color_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_color_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/calendar/index.php # wp_get_shadow_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_shadow_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/navigation-submenu/index.php - message: '#^Function wp_get_shadow_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/post-featured-image/index.php - message: '#^Function wp_get_shadow_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/search/index.php # wp_get_spacing_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_spacing_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/comment-author-avatar/index.php - message: '#^Function wp_get_spacing_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/icon/index.php - message: '#^Function wp_get_spacing_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/post-navigation-link/index.php # wp_get_background_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_background_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/button/index.php - message: '#^Function wp_get_background_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/icon/index.php - message: '#^Function wp_get_background_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/search/index.php # wp_get_border_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/14094 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_border_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/avatar/index.php - message: '#^Function wp_get_border_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/icon/index.php - message: '#^Function wp_get_border_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/post-featured-image/index.php - message: '#^Function wp_get_border_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/site-logo/index.php # wp_get_block_supports_classes_and_styles() is new in WordPress Core: https://github.com/WordPress/wordpress-develop/pull/15485 # Remove this entry once the WordPress stubs are generated from a Core revision that includes it. - message: '#^Function wp_get_block_supports_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/avatar/index.php - message: '#^Function wp_get_block_supports_classes_and_styles not found\.$#' identifier: function.notFound path: packages/block-library/src/post-navigation-link/index.php # Level 3: # WP_REST_Request implements ArrayAccess, and the WordPress stubs declare its offsetSet() with a method-level # template (`@template TOffset of key-of`, `@param T[TOffset] $value`). PHPStan does not resolve that # template for an offset write like `$request['context'] = 'edit'`, so every such write is rejected, valid # ones included, while the equivalent `$request->set_param( 'context', 'edit' )` call is accepted. # # This is a false positive which should be fixed in PHPStan: https://github.com/phpstan/phpstan-src/pull/6903 # Once a PHPStan release with that fix is required, remove these entries. - message: '#^Cannot assign offset .+ to WP_REST_Request(?:<.+>)?\.$#' identifier: offsetAssign.dimType reportUnmatched: false - message: '#^WP_REST_Request(?:<.+>)? does not accept .+\.$#' identifier: offsetAssign.valueType reportUnmatched: false # Level 4: # The Navigation block checks for WP_Theme_JSON_Gutenberg::get_viewport_media_queries() before falling back to # WP_Theme_JSON. PHPStan sees the Gutenberg class as always defined, but the block library also runs in # WordPress Core, where it is not, so the check is needed there. - message: '#^Call to function method_exists\(\) with ''WP_Theme_JSON…'' and ''get_viewport_media…'' will always evaluate to true\.$#' identifier: function.alreadyNarrowedType path: packages/block-library/src/navigation/index.php # strictRules.dynamicCallOnStaticMethod: # PHPUnit's markTestSkipped() is static, but like its assertions, which DynamicCallToAssertionIgnoreExtension # covers above, it is conventionally called on $this, as in WordPress core's tests. The tests are analyzed only # by a local phpstan.neon, so this entry goes unmatched otherwise. - message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert::markTestSkipped\(\)\.$#' identifier: staticMethod.dynamicCall path: phpunit/* reportUnmatched: false