Static Analysis of PHP and JavaScript

17th May, 2012 @ Pollenize HQ

About me

Simon Males

Platform Engineer at Pollenizer

- sime.net.au / @simonmales

Purpose

  • Clean up code
  • Adhere to some sort of standard
  • Style tips

Inspiration

Book: The Grumpy Programmer's Guide To Building Testable PHP Applications by Chris Hartjes

Blog post: Static Analysis tools for PHP by Mark Story

JavaScript

jshint (via node.js)

First install node.js: nodejs.org

Install jshint via npm:


$ sudo npm install -g jshint
					

Bad JS!


$("#charlie").navPlugin({
    'itemWidth': 116,
    'itemHeight': 42,
});

if ($('#responseModal').length == 0)
{
    alert('true');
}


$.each(data, function(i, value) {
    bookingTimes += '<option value="' + data[i]["value"] + '">' + data[i]["name"] + '</option>';
});

var disableFields = true
var enableFields = false;

if ($(this).attr("value") != "")
{
    emptyField = false;
}

if ($(this).attr("value") == "")
{
    emptyFields += 1;
}

					

Fix Bad JS


$ jshint bad.js
					

PHP

PHP_CodeSniffer

Supports JavaScript and CSS (not covered)

Install via CakePHP Code Channel


$ sudo pear channel-discover pear.cakephp.org
$ sudo pear install cakephp/CakePHP_CodeSniffer
					

Standards?


$ phpcs -i
The installed coding standards are CakePHP, MySource, PEAR, PHPCS, Squiz and Zend
					

Set Standard

By default: PEAR

Set standard on the fly:


$ phpcs --standard=CakePHP example.php
					

Update default standard


$ sudo phpcs --config-set default_standard CakePHP
					

Show configuration:


$ phpcs --config-show
					

Bad PHP #1

CakePHP MakePlans Plugin

https://github.com/Pollenizer-Hive/CakePHP-MakePlans-Plugin


$ sudo phpcs --config-set default_standard CakePHP
$ phpcs MakePlansSource.php
					

Bad PHP #2


<?php
$cow_status = true;
					

Bad PHP #3


<?php
Class Person {
	public $name;
}
					

Bad PHP #4


<?php
$names = array ('Simon', 'Males');
foreach($names as $name)
{
	echo $name;
}
					

Bad PHP #5

CakePHP Specific


<?php

/**
 * success - Redirected upon a successful booking created/update
 *
 * @param int $id
 * @return void
 */
public function success($id) {
	$booking = $this->Booking->findById($id);
	if (empty($booking)) {
		throw new NotFoundException(__('Invalid booking'));
	}
}

					

Bad PHP #6


<?php
if(true)
{
    $this->redirect(array('key'=>'value'));
}
					

PHPMD

PHP Mess Detector

phpmd.org

PHPMD can be seen as an user friendly and easy to configure frontend for the raw metrics measured by PHP Depend.


$ sudo pear channel-discover pear.phpmd.org
$ sudo pear channel-discover pear.pdepend.org
$ sudo pear install --alldeps phpmd/PHP_PMD
					

Bad PHP #8


<?php
function mess($id) {
	return $id;
}
					

$ phpmd bad_8.php text codesize,unusedcode,naming

Bad PHP #9


<?php
function index() {
	$name = 'Simon';
	$this->set(compact('name'));
}

$ phpmd bad_9.php text codesize,unusedcode,naming

Other interesting PHPMD output


The class BookingsController has 16 methods.
Consider refactoring BookingsController to keep number of methods under 10.

The class BookingsController has an overall complexity of 52 which is very high.
The configured complexity threshold is 50

THE END

BY Simon Males / @pollenizer