Integrate tools for static PHP code analyses into NetBeans 7.x

Published:

Table of contents

  1. Introduction
  2. Prerequisites
  3. Installation of the NetBeans IDE plugin phpcsmd
  4. Configuration of the NetBeans IDE plugin phpcsmd
  5. Usage of the NetBeans IDE plugin phpcsmd

    1. Create a new filter
    2. Scan for violations
    3. Scan with PHP_Depend
  6. Conclusion

Introduction

At things stand today, there exist three plugins for NetBeans IDE that are related to static code analyses for the PHP scripting language.

phpcsmd is the most advanced of the three available plugins since it integrates four external tools and supports the latest NetBeans IDE version 7.2.

In this article I demonstrate the installation, configuration and usage of the NetBeans IDE plugin phpcsmd. Both version 7.1.2 and version 7.2 (build 201204181547) of NetBeans IDE are used in this tutorial.

Prerequisites

Install PHP_CodeSniffer, PHPMD, PHP_Depend and PHPCPD. The preferred way is the installation with the PEAR installer (You have to run these commands as root on *nix, respectively Administrator on Windows systems).

pear config-set auto_discover 1
pear install --alldeps pear/PHP_CodeSniffer
pear install --alldeps phpmd/PHP_PMD
pear install --alldeps pdepend/PHP_Depend
pear install --alldeps phpunit/phpcpd
pear config-set auto_discover 0

Installation of the NetBeans IDE plugin phpcsmd

  1. Start NetBeans IDE 7.x.
NetBeans IDE Splash Screen
  1. Select Tools > Plugins from the menu bar. The Plugins dialog appears.
  2. Select the Settings tab.
Plugins: Settings
  1. Make sure that Plugin Portal is listed in the Update Center. If it is already in the list, proceed with step 6.
  2. Add Plugin Portal to the Update Center.

    1. Click on the button Add. The Update Center Customizer dialog appears.
    2. Make sure that the Check for updates automatically checkbox is enabled.
    3. Enter Plugin Portal in the Name input text field and http://plugins.netbeans.org/nbpluginportal/updates/7.1/catalog.xml.gz in the URL input text field.
Update Center Customizer
Plugins: Settings (after the addition of the Plugin Portal)
  1. Select the Available Plugins tab.
  2. Enter phpcsmd into the Search input text field. Click on the Reload Catalog button if it does not appear in the list.
  3. Enable the Install checkbox in the first column of the list.
Plugins: Available Plugins
  1. Click on the Install button. The Welcome to the NetBeans IDE Plugin Installer dialog appears.
NetBeans IDE Plugin Installer: Welcome Screen
  1. Click on the Next > button. The License Agreement dialog appears.
NetBeans IDE Plugin Installer: License Agreement
  1. Enable the I accept the terms in all of the license agreements. checkbox. Click on the Install button. The installation progress starts.
  2. The Verify Certificate dialog appears. Click on the Continue button. The installation progress continues.
NetBeans IDE Plugin Installer: Verify Certificate
  1. The Restart NetBeans IDE to complete installation. dialog appears. Enable the Restart IDE Now checkbox. Click on the Finish button. NetBeans IDE is restarted.
NetBeans IDE Plugin Installer: Completed Installation

After this procedure the NetBeans IDE plugin phpcsmd is installed and can be configured.

Configuration of the NetBeans IDE plugin phpcsmd

  1. Select Tools > Options from the menu bar.
  2. Select the item PHP in the Options dialog. After that, select the tab PHPCSMD.
  3. Fill in the following data:

    • General
      • Enable the checkbox try threading analysis.
      • Enable the checkbox update on save.
      • Enable the checkbox show notification icon.
      • Enable the checkbox PHPCS.
    • PHPCS
      • Enable the checkbox PHPCS.
      • Enter the filepath of the PHP_CodeSniffer executable (phpcs.bat under Microsoft Windows operating systems) in the Script location text input field.
      • Enter the coding standard into the –standard= text input field. PHP_CodeSniffer version 1.3.3 has the following coding standards installed: MySource, PEAR, PHPCS, Squiz and Zend.
      • Enable the checkbox show Warnings.
      • Enable the checkbox Extras.
      • Enable the checkbox Unreachable Code (Squiz standard must be installed).
    • PHPMD
      • Enable the checkbox PHPMD.
      • Enter the filepath of the PHPMD executable (phpmd.bat under Microsoft Windows operating systems) in the Script location text input field.
      • Enable the checkbox strict.
    • PHPCPD
      • Enable the checkbox PHPCPD.
      • Enter the filepath of the PHPCPD executable (phpcpd.bat under Microsoft Windows operating systems) in the Script location text input field.
    • PDepend
      • Enable the checkbox PDepend.
      • Enter the filepath of the PHP_Depend executable (pdepend.bat under Microsoft Windows operating systems) in the Script location text input field.
      • Enable the checkbox Use tabs to organize PdependReport.
      • Enable the checkbox Create JDepend statistics.
  4. Click on in the OK button.
Options: PHPCSMD

After this procedure the NetBeans IDE plugin phpcsmd is configured and can be used.

Usage of the NetBeans IDE plugin phpcsmd

Create a new filter

  1. Select Window > Action Items (Window > Tasks for NetBeans IDE versions < 7.2) from the menu bar.
  2. Click on the Filter button and select Edit… from the list.

    1. Click on the New button.
    2. Input PHP into the Name text input field.
    3. Activate the checkboxes Issues, PHP Violations and TODO in the list of the Types tab.
    4. Click on the OK button.
Task List Filter

Scan for violations

Every file is checked after it is saved. All coding violations for the current file appear within the Window > Action Items (Window > Tasks) window.

Tasks window with phpcsmd enabled

In addition, there is one new annotation for each violation within the Editor window. If there are multiple annotations for one line, one can click on them to cycle through them.

Editor window  with phpcsmd enabled

Another nice feature is called violation scan report. Such a report can be generated by right-clicking on a directory within the Project window and select Scan for violations from the context menu (or select Tools > Scan scan for violations from the menu bar). A new window appears. Enable the full checkbox and click on the Rescan button. A list is populated which contains all files of the selected directory and displays the number of violations detected by each static code analyses tool.

Violation scan report

Last but not least, there is a new icon in the status bar which displays the violation count for the current file.

Status Bar with phpcsmd enabled

Notice: Selecting Window > ScanReport from the menu bar does not seem to trigger any action.

Scan with PHP_Depend

  1. Right-click on a directory or file within the Project window and select Scan with Pdepend from the context menu (or select Tools > Scan with Pdepend from the menu bar). A new window appears.
  2. Click on the Scan button in the new window. Six new tabs appear within the previously created window.
    • Metrics
PHP_Depend scan report: Metrics
  • Counts
PHP_Depend scan report: Counts
  • Classes
PHP_Depend scan report: Classes
  • Different Metrics
PHP_Depend scan report: Different Metrics
  • JDepend Graph
  • Other
PHP_Depend scan report: Other

Notice: Selecting Window > PdependReport from the menu bar does not seem to trigger any action.

Conclusion

Even if it is in an early state, phpcsmd is by now the best NetBeans IDE plugin which is related to static code analyses in the PHP scripting language. All other plugins I tried so far, threw unhandled Exceptions after using them for a while. In addition, phpcsmd supports more external tools for static code analyses.

I hope you enjoyed this article. Leave me a comment or send me a mail for corrections or additions related to this topic.