lipemat / phpstan-wordpress
用于与 WordPress 一起工作的 Phpstan 扩展
Requires
- php: >=7.4
- php-stubs/wp-cli-stubs: ^2.6.0
- phpstan/extension-installer: ^1.1.0
- phpstan/phpstan: ^1.10.38
- szepeviktor/phpstan-wordpress: ^1.1.2
Requires (Dev)
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-strict-rules: ^1.5
- roave/security-advisories: dev-latest
- dev-master
- 3.4.1
- 3.4.0
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.0
- 2.11.1
- 2.11.0
- 2.10.0
- 2.9.0
- 2.8.1
- 2.8.0
- 2.7.1
- 2.7.0
- 2.6.0
- 2.5.0
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.1
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.1
- 2.0.0
- 1.9.0
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.0
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- v1.0.0-beta-4
- v1.0.0-beta-3
- v1.0.0-beta-2
- v1.0.0-beta-1
- dev-develop
- dev-feature/get_approved_comments
This package is auto-updated.
Last update: 2024-09-03 01:50:59 UTC
README
使用方法
通过 composer 安装
composer require lipemat/phpstan-wordpress
包含的占位符
- 半官方的 phpstan-wordpress 占位符。
- 自定义占位符
wp.php
WordPress 的一些附加占位符
可选占位符
- WP-CLI 占位符.
- WP-CLI 工具占位符.
- CMB2 占位符
- Genesis 占位符
- VIP 占位符 WP VIP 环境的一些占位符。
可以选择性地将这些添加到您的 phpstan.neon
或 phpstan.neon.dist
,如下所示
当将库作为全局安装使用时
scanFiles: - %rootDir%/../../../stubs/cmb2/cmb2-3.10.php - %rootDir%/../../../stubs/genesis/genesis-3.4.php - %rootDir%/../../../stubs/wp-cli/php-cli-tools-0.11.php - %rootDir%/../../../stubs/vip.php - %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php - %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-commands-stubs.php - %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-i18n-stubs.php
当将库作为 composer 依赖项使用时
scanFiles: - %rootDir%/../../lipemat/phpstan-wordpress/stubs/cmb2/cmb2-3.10.php - %rootDir%/../../lipemat/phpstan-wordpress/stubs/genesis/genesis-3.4.php - %rootDir%/../../lipemat/phpstan-wordpress/stubs/wp-cli/php-cli-tools-0.11.php - %rootDir%/../../lipemat/phpstan-wordpress/stubs/vip.php - %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php - %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-commands-stubs.php - %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-i18n-stubs.php
或者,您可以将 %rootDir%/../../
替换为您 vendor
目录的相对路径。
示例 wp-content/plugins/core/vendor/lipemat/phpstan-wordpress/stubs/cmb2/cmb2-3.10.php
实用类型
\AtLeast<T, U>
标记一组数组形状键为必需,同时将其他键设置为可选。
/** * @phpstan-var \AtLeast<array{a?: string, b?: string}, 'a'> $array * // results: array{a: string, b?: string} */
\Exclude<T, K>
从数组形状中排除指定的键。
/** * @phpstan-var \Exclude<array{a: string, b: string}, 'a'> $array * // results: array{b: string} */
\Partial<T>
标记数组形状中的所有键或指定的键为可选。
\Partial<T>
: 标记所有键为可选。\Partial<T, K>
: 仅标记指定的键为可选。
/** * @phpstan-var \Optional<array{a: string, b: string}> $array * // results: array{a?: string, b?: string} * * @phpstan-var \Optional<array{a: string, b: string}, 'b'> $array * // results: array{a: string, b?: string} */
\Pick<T, K>
仅从数组形状中选择指定的键。
/** * @phpstan-var \Pick<array{a: string, b: string}, 'a'> $array * // results: array{a: string} */
\Required<T>
标记数组形状中的所有键或指定的键为必需。
\Required<T>
: 标记所有键为必需。\Required<T, K>
: 仅标记指定的键为必需。
/** * @phpstan-var \Required<array{a?: string, b?: string}> $array * // results: array{a: string, b: string} * * @phpstan-var \Required<array{a?: string, b?: string}, 'b'> $array * // results: array{a?: string, b: string} */
\Sarcastic<T>
将类型标记为不可预测的随机值。
此实用程序在日常项目中非常实用。
/** * @phpstan-var \Sarcastic<string> $string * // results: anyone's guess */
\Union<T, U, ...X>
将两个或多个数组形状组合,就像使用 array_merge
将第二个数组覆盖第一个数组一样。
/** * @phpstan-var \Union<array{a: string}, array{b: string}> $array * // results: array{a: string, b: string} */
可选包含规则
随着我们朝着使用组合而不是继承的世界迈进,我们需要对我们的代码编写方式更加严格。这些可选规则不能让我们完全达到目标,但它们是朝着正确方向迈出的一步,同时仍然适用于 WordPress 项目。
在您的 phpstan.neon
或 phpstan.neon.dist
中启用,如下所示
includes: # If you are using this library as a global install - %rootDir%/../../../rules.neon # If you are using this library as a composer dependency - %rootDir%/../../lipemat/phpstan-wordpress/rules.neon
- 防止使用
compact
函数。 - 要求所有类要么是抽象的,要么是最终的。
- 要求每个非空文件中都有
declare(strict_types=1)
语句。 - 防止在类构造函数中使用默认值。
- 防止在最终类中声明
protected
方法,而是使用private
。 - 防止使用
switch
语句,而是使用match
。 - 要求抽象类中的任何具体方法必须是
private
或final
。 - 防止子类跳过父参数类型。
- 防止调用未知类的方法。
- 除非期望布尔值,否则优先返回 null。
- 禁止使用
ArrayAccess
来访问类数据。 - 使用
instance of
而不是isset
进行对象验证。
分布式插件或主题
一些规则假设您正在处理一个不会分发到社区的私有项目。如果您的项目将要分发,您可以在 lipemat
参数中添加 nonDistributed
参数。
parameters: lipemat: nonDistributed: false
将 nonDistributed
参数设置为 false
将禁用以下规则
- 要求所有类要么是抽象的,要么是最终的。
- 要求每个非空文件中都有
declare(strict_types=1)
语句。 - 要求抽象类中的任何具体方法必须是
private
或final
。
阻止任何继承
将 noExtends
参数添加到 lipemat
参数中,将阻止拥有或扩展任何未列出的抽象类。
parameters: lipemat: allowedToBeExtended: - Lipe\Project\SomeAbstractClass - Lipe\Project\SomeOtherAbstractClass noExtends: true
您可以选择省略 allowedToBeExtended
参数,以防止扩展任何抽象类。