LoginSignup
5
5

More than 1 year has passed since last update.

[Apex PMD] VSCodeでApexの静的解析を行う

Posted at

Apex PMDとは

Apex PMDはApexのソースコードを静的解析し、問題のある箇所を検知するVSCodeの拡張機能です。
問題箇所の早期発見が容易になります。

ApexPMD_Result.png

Apex PMD 導入方法

マーケットプレイスから下記の拡張機能をインストールします。

インストールしたら、静的解析を行いたいApexファイルを開き、コマンドパレットでApex Static Analysis: On Fileを実行するだけで静的解析されます。
プロジェクト全体で静的解析したい場合はApex Static Analysis: On Workspaceを実行します。
表示されたエラーをクリアしたい場合はApex Static Analysis: Clear Problemsを実行します。

ApexPMD_CommandParret.png

コマンドパレットからの実行の他、ファイルを保存したときにも実行されます。この設定は設定画面から変更ができます。
「Run On File Change」 をチェックすることでファイル変更時点で静的解析されます。

ApexPMD_Setting.png

独自のルールセットを作成する

静的解析の検知対象はルールセットで定義されています。
エラー、警告などプライオリティを変更したい場合、不要なルールを無効にしたい場合などは独自のルールセットを使用することができます。
ルールセットは公式ドキュメントのルールリファレンス1に従って、XMLファイルで記述します。
作成したルールセットは設定画面で追加できます。複数ファイル使用することもできます。

ApexPMD_CustomRules2.png

下記はルールセットの例になります。

best-practice.xml
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Best Practices"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
    <description>一般的に受け入れられているベストプラクティスを実施するルール。</description>
    <exclude-pattern>.*/.sfdx/.*</exclude-pattern>

<!--
    priority 1: error
    priority 3: warn
    priority 5: hint
-->

    <!-- アサートメソッドはメッセージを設定してください。 -->
    <rule ref="category/apex/bestpractices.xml/ApexAssertionsShouldIncludeMessage">
        <priority>5</priority>
    </rule>

    <!-- テストメソッドには少なくとも1つのアサーションを含める必要があります。 -->
    <rule
        ref="category/apex/bestpractices.xml/ApexUnitTestClassShouldHaveAsserts"
        message="Apex unit test classes should have at least one System.assert() or assertEquals() or AssertNotEquals() call">
        <priority>3</priority>
    </rule>

    <!-- testMethodキーワードは非推奨です。@IsTestアノテーションを使用してください。 -->
    <rule ref="category/apex/bestpractices.xml/ApexUnitTestMethodShouldHaveIsTestAnnotation" >
        <priority>1</priority>
    </rule>

    <!-- @IsTest(seeAllData=true)は使用できません。 -->
    <rule
        ref="category/apex/bestpractices.xml/ApexUnitTestShouldNotUseSeeAllDataTrue"
        message="@isTest(seeAllData=true) should not be used in Apex unit tests because it opens up the existing database data for unexpected modification by tests">
        <priority>1</priority>
    </rule>

    <!-- RestResource、WebService以外のメソッドはglobalにできません。 -->
    <rule
        ref="category/apex/bestpractices.xml/AvoidGlobalModifier"
        message="Avoid using global modifier">
        <priority>1</priority>
    </rule>

    <!-- トリガにロジックは記述せず、トリガハンドラを使用してください。 -->
    <rule
        ref="category/apex/bestpractices.xml/AvoidLogicInTrigger"
        message="Avoid logic in triggers">
        <priority>3</priority>
    </rule>

    <!-- デバッグにログレベルを設定してください。 -->
    <rule ref="category/apex/bestpractices.xml/DebugsShouldUseLoggingLevel">
        <priority>3</priority>
        <properties>
            <!-- LoggingLevel.DEBUGを検出する場合はtrue -->
            <property name="strictMode" value="false" />
        </properties>
    </rule>

    <!-- 使用されていない変数は宣言しないでください。 -->
    <rule ref="category/apex/bestpractices.xml/UnusedLocalVariable">
        <priority>1</priority>
    </rule>
</ruleset>

ルールを自作する

カスタムルールを自作してルールセットに追加することも可能のようです。
https://pmd.github.io/latest/pmd_userdocs_extending_writing_pmd_rules.html

参考

5
5
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
5
5