Zend Code Analyzer in TextMate

Blog calendar

RSS feed from Michal Frackowiak's blog

subscribe to the RSS feed

— or —

get my blog posts via email

Arotaritei VladArotaritei Vlad
TRT- Vipul SharmaTRT- Vipul Sharma
Matt GentileMatt Gentile
Sven StettnerSven Stettner
Joshua DarbyJoshua Darby
Watch: site | category | page

Blog tags

View my profile on LinekdIn




Piotr Gabryjeluk's blog (Gabrys on Wikidot)
Lukasz Tarka's blog (Squark on Wikidot)

Recent posts by my friends

{"module":"feed\/FeedModule","params":{"src":"http:\/\/squark.wikidot.com\/feed\/pages\/category\/blog\/;http:\/\/piotr.gabryjeluk.pl\/feed\/pages\/category\/dev\/t\/Piotr+Gabryjeluk+dev+blog","limit":"10","module_body":"[[div style=\"font-size: 87%; margin: -0.7em 0;\"]]\n%%linked_title%%\n[[\/div]]"}}

Me in other networks:

facebook, last.fm, del.icio.us, Flickr, Flaker

« Back to the Blog

18 Dec 2009 23:26

Zend Code Analyzer (part of Zend Studio) is a powerful tool that analyzes PHP scripts and detects errors and warnings possibly generated by your code. I have been using Zend Studio for a while, and I miss this feature a lot when working with TextMate. Fortunately, thanks to great extensibility of TM, one can include code analysis in the PHP bundle within just a few minutes.

After completing the steps below, you will be able to press a combination of keys and ZendCodeAnalyzer will analyze your PHP script and render its output like on the screen below:


And here is how.

1. Download Zend Studio (demo)

Go to http://www.zend.com/en/products/studio/downloads-prev and download a 30-day trial of Zend Studio 7.0.


You can skip this option if you already have Zend Studio, but since you are using TextMate — I bet you don't. You might need to create an account at Zend, but this is all free. Note that the newest Zend Studio 7.1 does not have a standalone ZendCodeAnalyzer binary, and cannot be (yet) used in TextMate.

2. Copy the binary file

One way to get ZendCodeAnalyzer is to install Zend Studio and look for the file in the installation path. But I do not like to waste my time installing stuff I will not need. Besides, we are interested just in one single executable file.

Open (mount) the downloaded dmg image. If you had just downloaded this, it should be mounted. Now paste those commands:

mkdir  /tmp/zca
cd /tmp/zca
unzip /Volumes/ZendStudio\ 7.0.*/ZendStudio.app/Contents/Resources/Java/Disk1/InstData/Resource1.zip \
unzip  '$IA_PROJECT_DIR$/resources/Mac/plugins_zg_ia_sf.jar' '*ZendCodeAnalyzer'
sudo cp `find . -name ZendCodeAnalyzer` /usr/local/bin/zca
sudo chmod a+x /usr/local/bin/zca
rm -rf /tmp/zca

By doing so you end up having a zca executable in /usr/local/bin. Verify whether it works:

zca --help

4. Edit TextMate PHP bundle

Now you need to add a new command to the PHP bundle.

  • Select Bundles » Bundle Editor » Edit Commands…
  • Add a new command — I called mine Analyze Code
  • Paste the following code:
#!/usr/bin/env ruby
require ENV['TM_SUPPORT_PATH'] + '/lib/textmate'

result = `/usr/local/bin/zca #{ENV['TM_FILEPATH']} 2>&1`
puts result.gsub /^Analyzing.*?\n/m, ''
if result =~ /line (\d+)/
    TextMate.go_to :line => $1 
    puts "No problems found"
  • Set other options as on the screenshot:

(choose any other keyboard shortcut if you wish)

  • Edit PHP Bundle menu and move the Analyze Code below Validate Code (pure esthetics)

5. Try it!

Let us try the analyzer on a simple code:

function foo($bar) {
    $a = 3;
    return "123" . $c;
echo foo("bar");

The code itself is broken in many places, although it does not have any syntax errors. Now try the code analyzer and you should see something like in the first image up there.

The command automatically moves the cursor to the first line that contains a problem.

Still missing

ZendCodeAnalyzer has been replaced by Semantic Analysis in Zend Studio 7.1 and completely integrated into Eclipse framework. It does not provide a standalone version. Perhaps in the future such a version will be available from Zend, but not now.

ZendCodeAnalyzer has some tuning options, mainly: --enable <warning-name>, --disable <warning-name>, --recursive for including required files and —include-path. Perhaps it would be interesting to play with those to provide more in-depth code analysis.

Show as Tool Tip might not be the best way to display output of analyzer, but I could not figure out anything smarter.

Adding code analysis to TextMate is IMHO a game changer, it can potentially save you many headaches caused by various coding mistakes one can make. Although the above installation uses only basic functions of ZCA, it proved to be a very useful addition to the PHP bundle.

If you also find it useful — please add your comment! I will highly appreciate any suggestions and improvements!

rating: 0, tags: php textmate

rating: 0+x


Add a New Comment