lipemat/phpstan-wordpress

用于与 WordPress 一起工作的 Phpstan 扩展

资助包维护!
lipemat

安装次数: 10,545

依赖项: 1

建议者: 0

安全: 0

星标: 7

关注者: 3

分支: 0

开放问题: 0

类型:phpstan-extension

3.4.1 2024-08-03 01:40 UTC

README

package version php version Packagist

使用方法

通过 composer 安装

composer require lipemat/phpstan-wordpress

包含的占位符

  1. 半官方的 phpstan-wordpress 占位符。
  2. 自定义占位符
    1. wp.php WordPress 的一些附加占位符

可选占位符

  1. WP-CLI 占位符.
  2. WP-CLI 工具占位符.
  3. CMB2 占位符
  4. Genesis 占位符
  5. VIP 占位符 WP VIP 环境的一些占位符。

可以选择性地将这些添加到您的 phpstan.neonphpstan.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.neonphpstan.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
  
  1. 防止使用 compact 函数。
  2. 要求所有类要么是抽象的,要么是最终的。
  3. 要求每个非空文件中都有 declare(strict_types=1) 语句。
  4. 防止在类构造函数中使用默认值。
  5. 防止在最终类中声明 protected 方法,而是使用 private
  6. 防止使用 switch 语句,而是使用 match
  7. 要求抽象类中的任何具体方法必须是 privatefinal
  8. 防止子类跳过父参数类型。
  9. 防止调用未知类的方法。
  10. 除非期望布尔值,否则优先返回 null。
  11. 禁止使用 ArrayAccess 来访问类数据。
  12. 使用 instance of 而不是 isset 进行对象验证。

分布式插件或主题

一些规则假设您正在处理一个不会分发到社区的私有项目。如果您的项目将要分发,您可以在 lipemat 参数中添加 nonDistributed 参数。

parameters:
    lipemat:
      nonDistributed: false

nonDistributed 参数设置为 false 将禁用以下规则

  1. 要求所有类要么是抽象的,要么是最终的。
  2. 要求每个非空文件中都有 declare(strict_types=1) 语句。
  3. 要求抽象类中的任何具体方法必须是 privatefinal

阻止任何继承

noExtends 参数添加到 lipemat 参数中,将阻止拥有或扩展任何未列出的抽象类。

parameters:
    lipemat:
      allowedToBeExtended: 
        - Lipe\Project\SomeAbstractClass
        - Lipe\Project\SomeOtherAbstractClass
      noExtends: true

您可以选择省略 allowedToBeExtended 参数,以防止扩展任何抽象类。