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

Comments

3/29/2010 1:21:30 AM #

smchristensen

Obviously tab spacing and replacing tabs with spaces is a topic about as divisive as religion or politics.  Here's my reasoning for not converting tabs to spaces.

1. Smaller script size (probably not an issue if you're using an obfuscator or minifier on  your scripts.
2. Allows for developer flexibility.  If I want to set my tab spacing to 4 spaces per tab vs. 2 for readability purposes I can.  I'm not forced into using what I find less readable.

My $.02

smchristensen United States |

3/30/2010 11:51:28 PM #

Chris Strosser

@smchristensen - Agreed. Call me old-fashioned, but I also prefer tabs.

Chris Strosser United States |

3/31/2010 2:28:39 AM #

pingback

Pingback from developercast.com

Klaus Graefensteiner’s Blog: Enforcing PHP coding standards with Eclipse PDT | Development Blog With Code Updates : Developercast.com

developercast.com |

4/1/2010 6:28:02 AM #

Burak Erdem

Totally agree with the tab spacing. If file size and readability is important, then tabs should be displayed as TABS Smile

Burak Erdem Turkey |

Comments are closed

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 and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2014, 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 2014 by Klaus Graefensteiner

Rendertime:
Page rendered at 11/27/2014 9:29:04 PM (PST Pacific Standard Time UTC DST -7)