mago lint で遭遇する各種エラーの抑止 と baselineの利用

に公開

※ この記事は、現時点で最新のバージョン 1.0.0-alpha.8 を対象にしています。
※ Mago のドキュメント Using the Linterなども併せて確認ください

  • mago lintを実行時、以下のようなメッセージに遭遇することがあると思います。
$ mago lint
 WARN Parsing issues in 'vendor/squizlabs/php_codesniffer/tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/CategoryA/.hidden/HiddenDirShouldBeIgnoredSniff.php'. Codebase analysis may be incomplete.
 WARN Parsing issues in 'vendor/squizlabs/php_codesniffer/tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/.hidden/HiddenDirShouldBeIgnoredSniff.php'. Codebase analysis may be incomplete.
 WARN Parsing issues in 'vendor/squizlabs/php_codesniffer/tests/Core/Ruleset/Fixtures/DirectoryExpansion/.hiddenAbove/src/MyStandard/Sniffs/.hidden/HiddenDirShouldBeIgnoredSniff.php'. Codebase analysis may be incomplete.
error[invalid-var-tag]: Could not resolve the type for the @var tag.
   ┌─ test/HydratorObjectPropertyTest.php:28:28
   │
28 │             /** @var object{id:int} */
   │                            ^ Unexpected token `LeftBrace`= The parser encountered a token that was not expected at this position.
   = Help: Review the type syntax near the unexpected token.

warning[php-unit/redundant-instance-of]: Redundant `instanceof` assertion.
   ┌─ test/Strategy/BooleanStrategyTest.php:18:9
   │
18$this->assertInstanceOf(BooleanStrategy::class, new BooleanStrategy(1, 0));
   │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `instanceof` assertion is redundant because `Laminas\Hydrator\Strategy\BooleanStrategy` is always an instance of `Laminas\Hydrator\Strategy\BooleanStrategy`.= Help: Remove this `instanceof` assertion because it is redundant and always true.

warning[php-unit/redundant-instance-of]: Redundant `instanceof` assertion.
   ┌─ test/Strategy/BooleanStrategyTest.php:23:9
   │
23$this->assertInstanceOf(BooleanStrategy::class, new BooleanStrategy('true', 'false'));
   │         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This `instanceof` assertion is redundant because `Laminas\Hydrator\Strategy\BooleanStrategy` is always an instance of `Laminas\Hydrator\Strategy\BooleanStrategy`.= Help: Remove this `instanceof` assertion because it is redundant and always true.

error: found 3 issues: 1 error(s), 2 warning(s)
 = 2 issues contain auto-fix suggestions

stderr WARN Parsing issues in への対応 - excludesでの指定

  • 今回の場合、あきらかに不要なvendorのテストディレクトリに対して、解析時のエラーが発生しているので、mago.toml でのsourceセクションでのexcludes にて対象のディレクトリを指定して除外対象にしましょう
[source]
paths = ["benchmark/", "src/", "test/"]
includes = ["vendor"]
excludes = ["vendor/squizlabs/php_codesniffer/tests"]

mago が解析未対応の error[invalid-var-tag] への対処 - 修正可能な --fixable-only のみの表示

error[invalid-var-tag]: Could not resolve the type for the @var tag.
   ┌─ test/HydratorObjectPropertyTest.php:28:28
   │
28 │             /** @var object{id:int} */
   │                            ^ Unexpected token `LeftBrace`= The parser encountered a token that was not expected at this position.
   = Help: Review the type syntax near the unexpected token.

というようなmago未対応のphpdocタグに遭遇する場合もあるので、この表示をいったんスキップする場合(修正可能であろう warningのみ)表示するは、--fixable-only あるいは -f オプションが利用できます。

mago lint -f

現行の指摘事項(lintエラー)の抑止 - baseline

  • mago も psalmやphpstanと同じくbaselineサポートがあります。
  • baselineは --baseline にてbaseline記述のtomlファイルパスを指定します
  • (先述の)パースエラーもbaselineにて対応・生成されるので、-f オプションは不要です

baseline生成

  • --generate-baseline で作成指定を行います
✗ mago lint --baseline mago-lint-baseline.toml --generate-baseline
 INFO Generating baseline file...
 INFO Baseline file successfully generated at `mago-lint-baseline.toml`

baseline利用でのコマンド実行

  • 生成したbaselineありで実行する場合には、都度 --baseline を指定します。
✗ mago lint --baseline mago-lint-baseline.toml
 INFO Filtered out 3 issues based on the baseline file at `mago-lint-baseline.toml`.
 INFO No issues found.

[補足] 生成される baselineファイル

  • --generate-baseline にて作成されるbaselineファイルのフォーマットは以下の通りとなっています。
[entries."test/HydratorObjectPropertyTest.php"]
hash = "7b906f8ead90e9297e7a173ba274fd42fc6f428e70a45c63fef88e6a5cdb37d6"

[[entries."test/HydratorObjectPropertyTest.php".issues]]
code = "invalid-var-tag"
start_line = 27
start_column = 27
end_line = 27
end_column = 28

[entries."test/Strategy/BooleanStrategyTest.php"]
hash = "5a078ed5df04104d0c29f888b73e7a952da7adf3101b5a721a5b527c9e3df256"

[[entries."test/Strategy/BooleanStrategyTest.php".issues]]
code = "php-unit/redundant-instance-of"
start_line = 17
start_column = 8
end_line = 17
end_column = 82

[[entries."test/Strategy/BooleanStrategyTest.php".issues]]
code = "php-unit/redundant-instance-of"
start_line = 22
start_column = 8
end_line = 22
end_column = 93

Discussion