Last time, in the article “A few words about debugging – Part I”, we talked about the best PHP configuration for Magento debugging. Today we’ll discuss the extensions that will help you when the code goes astray.

2) Extensions

There are many tools which are helpful in debugging and profiling the PHP code:

Xdebug

http://www.xdebug.org/

A must-have for me. Usually Xdebug doesn’t need any additional configuration. It enables stack-trace display for every error, protects against the unwanted recursion and improves the “var_dump” function out of the box. What’s more, Xdebug is highly configurable, has numerous useful features, a profiler, and the option to integrate a debugger.

Example:

<?php

function a() { 
	b('hello'); 
} 

function b($text) { 
	print c($text); 
} 
 
function c($arr) { 
	if (!is_array($arr)) { 
		throw new Exception('Argument is not an array'); 
	} 
	return array_shift($arr); 
} 

a();
?>

Without Xdebug With Xdebug
 image1-300x93 image2-300x177

What’s really important is that profiling with Xdebug is a breeze. Right after the launch it creates a log of every server query, which can be later reviewed and analyzed with KcacheGrind/QCacheGrind. This can be used to check which elements/functions use the most of the server resources.

Kint

http://raveren.github.io/kint/

Kint is a PHP library which simplifies PHP debugging. It shares some additional functions and methods for presenting the variables, objects and traces in a more accessible way. The installation process is easy and no additional server permissions are required: all you need to do is to add an extra file to the script.

Debugging the global variables with Kint:

image3-300x235

 

Stack-trace from our previous example:

image4-300x265

 

whoops

http://filp.github.io/whoops/

whoops is yet another library which simply works out of the box. Just add it to a project for this neat error and exception handling:

image5-300x156

 

Even though whoops doesn’t offer any fancy features, it lets you take a look at the code from each stack-trace element. Often it’s not even necessary to open the editor to see what causes an error.

XHProf

http://pecl.php.net/package/xhprof

XHProf, a PHP extension created by Facebook, can be used to profile the code. The results are visible in the browser. XHProf is a good alternative to Xdebug and KcacheGrind combination.

In Part III we’ll share with you a handful of best coding practices. Stay tuned!