Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding large php code, what techniques to use?

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?

like image 661
CuriousMind Avatar asked Apr 06 '11 21:04

CuriousMind


2 Answers

First, in this kind of situation, I try to get an overview of the application : some kind of global idea of :

  • What the application (not the code !) does
  • How the code is globally organized : where are the models, the templates, the controllers, ...
  • How each type of component is structured -- once you know how a Model class works, others will typically work the same way.


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 :

  • The inclued extension (quoting) : Allows you trace through and dump the hierarchy of file inclusions and class inheritance at runtime
  • Xdebug + KCacheGrind will allow you to generate call-graphs ; XHProf should do the same kind of thing.
  • Using your IDE (Eclipse PDT, Zend Studio, phpStorm, netbeans, ...), ctrl+click on a class/method should bring you to its declaration.


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.

like image 174
Pascal MARTIN Avatar answered Nov 09 '22 04:11

Pascal MARTIN


You need an IDE. I use netbeans for PHP and it works great. This will allow you to find out where the homeview/home classes are by right clicking and selecting a "find where defined" option or something similar.

You can get a list. This is called the stack. Setting up a debugger like xdebug with the IDE will allow you to do this.

like image 37
k to the z Avatar answered Nov 09 '22 05:11

k to the z