Enforcing PHP coding standards with Eclipse PDT

by Klaus Graefensteiner 28. March 2010 05:49

Introduction

I recently came across a nice collection of coding standards that the developers at Gallery2.org are using. I especially liked the summary of settings for Eclipse PDT. I applied them to my Eclipse PDT development environment, took some screenshots and put this article together for my personal reference:

Eclipse PDT editor settings

Here is the list of common editor settings that promote readable source code:

Note: The following screenshots and settings apply to Eclipse Galileo (Build id: 20090920-1017).

General text editor settings

Go to Window/Preferences/General/Editors/Text Editors and verify the following settings:

  1. Displayed tab width = 2
  2. Insert spaces for tabs should be checked
  3. Show print margin should be checked
  4. Print margin column = 100

PDT Preferences 1

Figure 1: General Text Editor settings

PHP style format settings

Go to Window/Preferences/PHP/Code Style/Formatter and verify the following settings:

  1. Tab policy = Spaces
  2. Indentation Size = 2

PDT Preferences 2

Figure 2: PHP Code Style Formatter settings

Source control integration settings

Go to Window/Preferences/Team/Ignored Resources and make sure that ".cache", ".project", ".buildpath" and ".settings" are ignored.

PDT Preferences 3

Figure 3: Files excluded from source control

File Save settings

Go to Window/Preferences/PHP/Editor/Save Actions and verify that:

  1. Remove trailing whitespace should be checked
  2. Choose All lines is selected

PDT Preferences 4

Figure 4: Clean-up actions when saving a file

Ausblick

Besides these editor settings there is another very powerful feature that makes it easy to follow coding standards without having to think about it. And this features is called Code Templates, which are similar to Microsoft Visual Studio code snippets. Here is a list of PHP templates that are predefined in PDT. You can modify these to meet your coding standard and you can create additional ones.

Go to Window/Preferences/PHP/Templates and you see a complete list of them:

image

Figure 5: Code templates help also with coding standards without the need to remember all the details

Tags: , , , , ,

Php | Tips & Tricks

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as developer in Test at Rockwell Automation and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Administration

About

Powered by:
BlogEngine.Net
Version: 1.5.0.7

License:
Creative Commons License

Copyright:
© Copyright 2009, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2009 by Klaus Graefensteiner

Rendertime:
Page rendered at 9/9/2010 5:01:12 AM (PST Pacific Standard Time UTC DST -7)