I have been handed over a large undocumented code of a application written in php as the original coder went AWOL. My task is to add new features but I can’t do that without understanding the code.I started poking around. honestly, I am overwhelmed by the amount of source code. I have found:
- Its well written based upon MVC architecture, DB persistence, Templating & OOP
- modular, there is concept of URL based routing,basic templating
- Uses custom written php framework which has no documentation.And there no source control history(oops!)
- there over 500 files, with each file containing hundreds of line of code. And every file has 3-4 require_once statements which include tons of other files, so its kinda hard to tell which function/class/method is coming from where
Now I am looking for some techniques that I use to understand this code. for example, consider the following code snippet:
class SiteController extends Common {
private $shared;
private $view;
protected function init(){
$this->loadShared();
$this->loadView();
}
private function loadShared(){
$this->shared = new Home();
}
private function loadView(){
$this->view = new HomeView();
}
I want to know
- where HomeView() & Home() are defined? Where does $this->shared & this->view come from? I checked the rest of the file, there is no method named shared or view. so obviously, they coming from one of hundreds of classes being included using require_once() But which one? how can I find out?
- Can I get a list of all the functions or methods that are being executed? If yes, then how?
- this class SiteController overrides a base Common class. But I unable to find out where is this Common class is located. How to tell?
Further, Please share some techniques that that be used to understand existing code written in php?
First, in this kind of situation, I try to get an overview of the application : some kind of global idea of :
Once you have that global idea, a possibility to start understanding how the code works, if you have some time before you, is to use a PHP Debugger.
About that, Xdebug + Eclipse PDT is a possibility — but pretty much all modern IDEs support that.
It’ll allow you to go through the generation of a page step by step, line by line, understanding what is called, when, from where, …
Of course, you will not do that for the whole application !
But as your application uses a Framework, there are high chances that all parts of the application work kind of the same way — which means that really understanding one component should help understanding the other more easily.
As a couple of tools to understand what calls what and how and where, you might want to take a look at :
Also note that an application is not only code : it often find very useful to reverse-engineer the database, to generate a diagram of all tables.
If you are lucky, there are foreign keys in your database — and you’ll have links between tables, this way ; which will help you understand how they relate to each other.