# 静的解析の実行

前のセクションで追加したrubocopを使い静的解析を実行します。

## 初めての静的解析

`tic_tac_toe`フォルダ直下で以下のコマンドを実行し

メインロジックを実装しているファイルを静的解析にかけます。

```
rubocop
```

実行してみるとめちゃくちゃ怒られます(50の警告が出ました)。

```
tic_tac_toe.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
# 説明: ゲーム板の状態をコンソールに出力する
^
tic_tac_toe.rb:4:15: C: [Correctable] Style/MethodDefParentheses: Use def with parentheses when there are parameters.
def put_board board
              ^^^^^
tic_tac_toe.rb:5:8: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
  puts ""
       ^^
tic_tac_toe.rb:8:13: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      print " N " if 0 == e
...(長過ぎるので割愛)

1 file inspected, 50 offenses detected, 39 offenses corrected, 7 more offenses can be corrected with `rubocop -A`
```

プロジェクトに初めて静的解析ツールを入れると大体の場合はこうなります。

## 設定ファイルの使用

次は、プロジェクト用に作成した設定ファイルを使ってrubocopを実行してみます。

code直下に`.rubocop.yml`を作成して以下の内容を貼り付けてください。

```
AllCops:
  NewCops: enable

EnforcedStyle: double_quotes

Style/YodaCondition:
  EnforcedStyle: require_for_all_comparison_operators

Style/NumericPredicate:
  Enabled: false

Style/NegatedIf:
  Enabled: false

Metrics/AbcSize:
  Max: 70

Metrics/CyclomaticComplexity:
  Max: 25

Metrics/PerceivedComplexity:
  Max: 25

Metrics/MethodLength:
  Max: 15
```

Metricsに関してはwin?メソッドが反応してしまうので、一旦大きめの値に設定しています。

設定ファイルを配置した状態で再びrubocopを実行します。

```
tic_tac_toe.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
# 説明: ゲーム板の状態をコンソールに出力する
^
tic_tac_toe.rb:4:15: C: [Correctable] Style/MethodDefParentheses: Use def with parentheses when there are parameters.
def put_board board
              ^^^^^
tic_tac_toe.rb:22:10: C: [Correctable] Style/MethodDefParentheses: Use def with parentheses when there are parameters.
...(長過ぎるので割愛)

1 file inspected, 27 offenses detected, 26 offenses auto-correctable
```

設定ファイルを書いて条件を緩めたりコーディングスタイルを限定したので

警告は少なくなりましたが、まだ27警告も残っています。

## 自動修正を実行

以下のコマンドを使用することで実は自動修正してくれます。

体力を消耗する前に自動実行で出来るだけ警告を消します。

```
rubocop -a
```

もう一度rubocopコマンドを叩いて、警告の数を確認します。

```
tic_tac_toe.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
# 説明: ゲーム板の状態をコンソールに出力する
^
tic_tac_toe.rb:55:3: C: [Correctable] Style/YodaCondition: Reverse the order of the operands player == 1.
  player == 1 ? 2 : 1
  ^^^^^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses auto-correctable
```

2つだけになりました！これなら何とかなりそうです。

## 手動修正

残りの3エラーは手動で修正していきます。

### Style/FrozenStringLiteralComment

`tic_tac_toe.rb`の一番上に下記のコメントを追加して修正しました

```
# frozen_string_literal: true
```

### Style/YodaCondition

一部Yoda記法になっていないコードがあったので、そこを修正しました

全部直し終わったのでもう一度rubocopを実行してみます

```
Inspecting 1 file
.

1 file inspected, no offenses detected
```

ようやく警告がなくなりました！

ここまでのコード実装を下記のリポジトリに置いておきました。

詰まったら参考にしてみてください。

{% embed url="<https://github.com/Kashiwara0205/ruby-tic-tac-toe/tree/master/ch3>" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kashiwara.gitbook.io/rubydesurufurusukuratchibe/di-san-zhang-jing-de-jie-xi/no.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
