🐧

mago format 、mago.toml での [format] 設定による調整

に公開

※ この記事は、現時点で最新のバージョン 1.0.0-alpha.6 を対象にしています。

Mago のフォーマットコマンドは、以下の通りmago init 実行後すぐに始めることができます。

mago init で設定ファイル mago.toml 用意

mago init
 INFO Found `composer.json` file at /home/you/dev/laminas-hydrator/composer.json
✔ Use `composer.json` to configure Mago? · yes
✔ Write configuration to `mago.toml`? · yes
 INFO Writing configuration to /home/you/dev/laminas-hydrator/mago.toml
 INFO Configuration file created successfully!

mago format でフォーマット実行

  • 特に設定値は加えずmago format(あるいはmago fmt)でフォーマットが行えます。
  • 実際の変更を行わないドライランを、--dry-run あるいは -d をオプションに設定して確認も行えます。
mago format
 INFO Formatted 102 source files successfully.
  • ディレクトリを個別に指定してでの実行もできます。
mago fmt src/Exception
 INFO Formatted 1 source files successfully.
  • デフォルトのコーディングスタンダードは、PER Coding Style 2.0 に沿うように設定されています。

mago format のコマンドオプション

  • format の実行オプションは mago help format で確認できます。
 mago help format
Format PHP code using Mago's formatter

Usage: mago format [OPTIONS] [PATH]...

Arguments:
  [PATH]...  Format specific files or directories, overriding the source configuration

Options:
  -d, --dry-run      Check if the source files are already formatted without making changes
  -i, --stdin-input  Read input from STDIN, format it, and write to STDOUT
  -h, --help         Print help

mago.toml の初期値

  • mago init 後には以下のような内容で mago.toml が作成されているはずです。
# Mago configuration file
# For more information, see https://mago.carthage.software/#/getting-started/configuration
php_version = "8.1.0"

[source]
paths = ["benchmark/", "src/", "test/"]
includes = ["vendor"]
excludes = []

[format]
print_width = 120
tab_width = 4
use_tabs = false


[linter]
default_plugins = true
plugins = ["php-unit"]

フォーマット値の調整

[format]
print_width = 120
tab_width = 4
use_tabs = false

[format]
print_width = 120
tab_width = 4
use_tabs = false

# 型宣言のスタイル設定。 `?string` というものにしたい場合は Question を指定。デフォルトは、NullPipe ( null|string )
null_type_hint = "Question"

# `function foo() : string` と return型での: の前にスペースを入れるか?デフォルトは false
space_before_colon_in_return_type = true

# `function foo(): string` と return型での : の後にスペースを入れるか? デフォルトは true
space_after_colon_in_return_type = true

# `! $foo` と !記号のあとにスペースを入れるか? デフォルトは false
space_after_logical_not_unary_prefix_operator = true

Discussion