Friday, October 19, 2018

Automated Code review for Apex in Salesforce – Static code analysis


PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth. It supports Java, JavaScript, Salesforce.com Apex and Visualforce, PLSQL, Apache Velocity, XML, XSL.

Installation Steps:

1. For MacOS:

Follow below steps to install PDM.

A. Open the terminal
B. Run cd $HOME
C. Run curl -OL https://github.com/pmd/pmd/releases/download/pmd_releases%2F6.8.0/pmd-bin-6.8.0.zip
D. unzip pmd-bin-6.8.0.zip
E. alias pmd="$HOME/pmd-bin-6.8.0/bin/run.sh pmd"

2. For Windows:

Follow below steps to install PDM.

A. Open the command prompt
B. Download pmd-bin-6.8.0.zip 
C. Extract the zip-archive, e.g. to C:\pmd-bin-6.8.0
D. Add folder C:\pmd-bin-6.8.0\bin to PATH, either
Permanently: Using System Properties dialog > Environment variables > Append to PATH variable
Temporarily, at command line: SET PATH=C:\pmd-bin-6.8.0;%PATH%
E. alias pmd="$HOME/pmd-bin-6.8.0/bin/run.sh pmd"

PMD Configuration for Eclipse :

To install the PMD plugin for Eclipse:

A. Start Eclipse and open a project
B. Select “Help”->“Install New Software”->“Find and Install”
C. Click “Add”
D. Enter “PMD” into the Name field and http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/ into the URL field
E. Click “Ok”
F. Click through the rest of the dialog boxes to install the plugin


Running PMD on Eclipse :

A. To run PMD, right-click on a project node and select “PMD”->“Check code”.
B. To run the duplicate code detector, right-click on a project node and select “PMD”->“Find suspect cut and paste”. The report will be placed in a “reports” directory in a file called “cpd-report.txt”.