1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SonarQubeでPHPの解析を始めるとき最初に読む資料

Last updated at Posted at 2023-06-21

1.概念、設計思想、用語を理解する

concepts(公式ドキュメント)

公式ドキュメント内の主要な用語、概念のリスト。(約10分)

conceptsに対する補足、注釈

  • 資料中、Analyzerに該当する製品がSonarScanner。
  • 資料中、Serverに該当する製品がSonarQube。

SonarQubeの静的解析を構成する概念について

主要概念の関係、管理画面を見るときに理解の助けとなる要約的な資料。(約10分)

JTF2021 D12 『SonarQube をより有効活用する』

SonarQubeが生まれた背景、どう活かしていくか、についての発表(約20分)

まとめ

  • Rule、Issue、QualityProfileの関係について理解する
  • Issueの種類と重要度を把握する
  • 開発者全員が品質意識を持ち、開発プロセスの中に品質チェックを組み込む

2.実際に使ってみる

Try out SonarQube(公式ドキュメント)

Try out SonarQubeに関する補足

  • 手元にDocker環境がある場合、Dockerが楽。
  • この資料で説明されているのはSonarQube(ConceptsにおけるServer)のみである点に注意。
  • conceptsの項目で説明されている通り、解析はAnalyzerにあたるSonarScannerが行うので、解析を行う環境に別途インストールが必要(後述)
  • 実際の運用では共用サーバーにSonarQubeを建てる方がよい
  • CommunityEditionでは、解析対象のブランチが1つしか選べない

SonarScanner(公式ドキュメント)

3.プロジェクトにあわせて設定のカスタマイズを行う

Analysis parameters(公式ドキュメント)

解析パラメータについて。特に、資料中のSettingHierarchyはよく理解しておくこと。

補足

  • 解析パラメータは4箇所で設定できるが、このうちProject analysis parametersが扱いやすそう。
    ※sonar-project.properties というファイルの形でリポジトリ管理できるため
  • 特定のIssueを無効化するなどの微調整を行う際にも利用する(後述)

PHP test coverage(SonarQube公式ドキュメント)

PHPのテストカバレッジについて。
SonarQubeはカバレッジレポート生成機能を持たないため、サードパーティ製のパッケージ(ほとんどの場合PHPUnit?)が生成したカバレッジレポートをインポートして、SonarQube上で利用する。

The XML Configuration File(PHPUnit公式ドキュメント)

  • phpunitが出力するカバレッジレポートをSonarQubeにインポートする際、phpunit側の設定としてxml出力を定義しておくと楽。
phpunit.xml
    <!-- 例 -->
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
        <report>
            <clover outputFile="clover.xml"/>
        </report>
    </coverage>

Importing third-party issues(公式ドキュメント)

Issueのインポートについて。PHPStanが出力する形式に標準対応している。

Comma-delimited list of paths to PHPStan reports. Reports should be generated in the PHPStan JSON Output Format (use the PHPStan analyse command with the option --error-format=json).

Importing issues from SARIF reports(公式ドキュメント)

  • 既にSonarQube以外のツールを使って解析をやっている場合、それらの解析結果をSonarQube上のIssueとして取り込みたい場合があるかもしれない。SARIF形式での出力をサポートしているツール、パッケージであればSonarQubeにインポート可能。(※phpstanの結果インポートはSonarCube側でインポートがサポートされている)
  • 2023-06-20調査した範囲では、対応状況は以下。
    PHPMD 〇
    PHPCS ☓
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?