yireo/magento2-extensionchecker

扫描 Magento 模块的代码

支持包维护!
yireo
www.paypal.me/yireo

安装次数: 110,221

依赖者: 2

建议者: 0

安全性: 0

星级: 88

关注者: 12

分支: 15

开放问题: 9

类型:magento2-module

2.4.4 2024-08-29 12:01 UTC

README

此扩展验证其他扩展的代码,并与静态代码分析工具(如PHPCS)相辅相成。

示例用法

bin/magento yireo_extensionchecker:scan --module Yireo_ExampleAdminhtml

运行此命令可能会得到以下输出

Dependency "Magento_Backend" not found module.xml
Dependency "Magento_Ui" not found module.xml
Dependency "magento/module-backend" not found composer.json. Current version is 101.0.2
Dependency "magento/module-ui" not found composer.json. Current version is 101.1.2
Dependency "psr/log" not found composer.json. Current version is 1.1.0

输出提示您向 composer.json 中添加什么。例如,应添加 composer 要求 magento/module-ui,并且这可能具有版本约束 ^101.1 以匹配语义版本。理论上,这也可以是 ^101.0 或甚至是 ^100.0|^101.0,但对于这一点,需要进行深入代码分析(由您进行)。

注意,您还可以通过逗号分隔来通过 --module 标志传递多个模块

bin/magento yireo_extensionchecker:scan --module Yireo_Example1,Yireo_Example2,Yireo_Example3

列出依赖项(例如:由此 ExtensionChecker 检测到的依赖项)可以使用以下命令完成

bin/magento yireo_extensionchecker:list-dependencies --module Yireo_Example
bin/magento yireo_extensionchecker:list-dependencies --module Yireo_Example --format=json | jq

安装

将模块作为开发环境中的 composer 要求进行安装

composer require --dev yireo/magento2-extensionchecker
bin/magento module:enable Yireo_ExtensionChecker

注意,如果您想扫描一个模块,此模块也需要启用。我们个人在 CI/CD 链中使用此扩展,以确保始终报告零问题。

废弃的依赖

检查类依赖(通过构造函数注入)是否过时,以查看它们是否适用于使用的 Magento 版本。您可以通过在命令中添加标志 --hide-deprecated 来跳过此行为

bin/magento yireo_extensionchecker:scan --module Yireo_Example --hide-deprecated=1

未声明的依赖

通过构造函数注入的类依赖会追溯到相应的模块(或框架或其他),应在 composer.json 文件和 module.xml 文件中反映出来。对于每个 composer 依赖项,还会报告当前版本。

此外,通过标记 PHP 源代码,它还会检测是否在 composer.json 文件中应反映特定的 PHP 扩展(例如,ext-json),当使用特定扩展的 PHP 函数时(例如,json_encode)。

@todo: 硬编码代理

代理是一个 DI 技巧,应该在模块的 di.xml 文件中进行配置,而不是在 PHP 中硬编码。扩展可以报告这一点。

@todo: 检查其他签名方法

如果除了构造函数之外的其他方法包含导入命名空间的类型提示,则这些命名空间会导致与模块的进一步依赖关系。例如,如果特定方法返回类型为 Magento/ModuleX/SomeInterface 的对象,则 Magento_ModuleX 需要报告为依赖项。

@todo: 扫描 @since

扫描类依赖项中的 @since 并双检查此最低版本是否与 composer 要求匹配。

提示:检查多个模块

您可以使用类似以下命令快速检查多个模块

bin/magento yireo_extensionchecker:scan --module $(bin/magento module:status --enabled | grep -e Yireo_ | awk '{printf "%s%s",sep,$0; sep=","} END{print""}') --hide-needless 1 --hide-deprecated 1