Posted by Abhishek on February 10, 2021
PMD Source Code Analysis CLI is most preferred choice when it comes to doing statis code analysis on Salesforce Apex Code Assets. In fact, it is recommended by Salesforce themselves.
sfdx force:project:create --projectname MyPersonalDevOrg
cd MyPersonalDevOrg
mkdir manifest
code .
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>ApexClass</name>
</types>
<types>
<members>*</members>
<name>ApexTrigger</name>
</types>
<types>
<members>*</members>
<name>ApexPage</name>
</types>
<types>
<members>*</members>
<name>CustomObject</name>
</types>
<version>44.0</version>
</Package>
mkdir buildfiles
sfdx force:mdapi:retrieve -r retrieved -k manifest/package.xml -w 10 -u xxx@xxx.com
unzip retrieved/unpackaged.zip -d retrieved
rm retrieved/unpackaged.zip
mkdir rulesets
mkdir AnalysisResults
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/bestpractices.xml -reportfile ./AnalysisResults/bestpractices.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/codestyle.xml -reportfile ./AnalysisResults/codestyle.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/design.xml -reportfile ./AnalysisResults/design.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/documentation.xml -reportfile ./AnalysisResults/documentation.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/errorprone.xml -reportfile ./AnalysisResults/errorprone.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/multithreading.xml -reportfile ./AnalysisResults/threading.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/performance.xml -reportfile ./AnalysisResults/performance.html
pmd -d ./retrieved/unpackaged -f summaryhtml -R ./rulesets/security.xml -reportfile ./AnalysisResults/security.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/bestpractices.xml -reportfile ./AnalysisResults/bestpractices.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/codestyle.xml -reportfile ./AnalysisResults/codestyle.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/design.xml -reportfile ./AnalysisResults/design.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/documentation.xml -reportfile ./AnalysisResults/documentation.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/errorprone.xml -reportfile ./AnalysisResults/errorprone.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/multithreading.xml -reportfile ./AnalysisResults/threading.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/performance.xml -reportfile ./AnalysisResults/performance.html
pmd -d ./retrieved/unpackaged -f csv -R ./rulesets/security.xml -reportfile ./AnalysisResults/security.html
Hurray ! We just analyzed all of our apex code assets in a matter of few minutes. The next step is to go over each and every finding in the CSV and take corrective action on your code asset. Please do refer my blog on Recommendations to fixing issues in Apex code to see how well we can fix the issues highlightd by PMD report.