nicksdot/phpstan-prettier-output

自定义PHPStan格式化器以实现更美观的输出。

0.2.0 2024-09-17 14:37 UTC

This package is auto-updated.

Last update: 2024-09-17 14:45:48 UTC


README

重要

这是一个概念验证。事情可能粗糙、笨拙且未整理,但它是有效的。

安装

$ composer require --dev nicksdot/phpstan-prettier-output

设置格式化器

services:
+	errorFormatter.customTable:
+		class: NickSdot\PrettierStanOutput\CustomTableFormatter
+	errorFormatter.customJson:
+		class: NickSdot\PrettierStanOutput\CustomJsonFormatter
+	errorFormatter.customCheckstyle:
+		class: NickSdot\PrettierStanOutput\CustomCheckstyleFormatter

IDE输出

如果您还希望在IDE中看到更美观的消息,则必须覆盖默认的CheckstyleErrorFormatter。为此,在您的phpstan.non中覆盖该服务。

services:
	errorFormatter.customTable:
		class: NickSdot\PrettierStanOutput\CustomTableFormatter
	errorFormatter.customJson:
		class: NickSdot\PrettierStanOutput\CustomJsonFormatter
	errorFormatter.customCheckstyle:
		class: NickSdot\PrettierStanOutput\CustomCheckstyleFormatter
+	errorFormatter.checkstyle:
+		class: NickSdot\PrettierStanOutput\CustomCheckstyleFormatter

愿望清单

为了使此包不那么笨拙,我们需要以下内容。

PHPStorm PHPStan插件

  • 在设置中允许自定义格式化器
  • 允许在工具提示输出中使用换行符。
  • 调整PhpStanMessageProcessor.java的处理和消费,以便检查工具提示和检查消息窗口使用不同的消息。类似于HTML检查中的工作方式。

插件仓库:https://github.com/JetBrains/phpstorm-phpstan-plugin

PHPStan

目前,我们依赖于正则表达式来重新格式化报告的消息(例如,添加换行符)。如果PHPStan能够添加对格式化消息的支持,那将是很好的,但这不是必需的。

可能的方案

  • 跟踪所有非sprintf消息,存放在RuleErrorBuilder::message()的静态属性中。
  • 将格式化和未格式化的消息都作为一级功能。
  • 将构建消息所需的价值添加到PHPStan\Command\AnalysisResult中。

考虑事项

"提示"的使用

Ondrej 建议仅使用"提示"输出。然而,这似乎不足以以下原因。

  • 很多时候没有"提示"。
  • 每个错误可以有多个提示。
  • 它们通常是简单的外部引用,如以下示例。
12   app/Models/Option.php:12                                                      
PHPDoc type array of property App\Models\Option::$fillable is not the same as PHPDoc type list<string> of overridden property Illuminate\Database\Eloquent\Model::$fillable.                                                                                 

💡 You can fix 3rd party PHPDoc types with stub files:               
💡 https://phpstan.org/user-guide/stub-files    

给定此示例,Ondrej提出的解决方案将导致以下结果,这并没有变得更易读,并且与之前相同的认知负荷。

使用此存储库中的方法,输出将得到。

12   app/Models/Option.php:12 
PHPDoc type of property App\Models\Option::$fillable is not the same as PHPDoc type of overridden property Illuminate\Database\Eloquent\Model::$fillable. 

⁉️ array
⁉️ list<string>

💡 You can fix 3rd party PHPDoc types with stub files:               
💡 https://phpstan.org/user-guide/stub-files    

因此,目前的结论是,提示不是正确的方法。

鸣谢

所有格式化器都是基于PHPStan内置错误格式化器

贡献

许可证

MIT