Re: How to look for unused methods/functions and variables/constants.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Dec 14, 2007 2:49 PM, Robert Cummings <robert@xxxxxxxxxxxxx> wrote:

> On Fri, 2007-12-14 at 14:09 -0500, Nathan Nobbe wrote:
> > On Dec 12, 2007 4:21 AM, Mathijs van Veluw <
> mathijs.van.veluw@xxxxxxxxxxx>
> > wrote:
> >
> > > Hello there,
> > >
> > > We have a large project with lots of classes.
> > > Now i am wondering if there is a way to let something check all those
> > > files and tell me which methods/functions variables/constants etc..
> > > arn't used anymore.
> > >
> > > Or even which files arn't used anymore.
> > >
> > > Is there already something like this?
> >
> >
> > if you want to do it right, youll have to make use of the php tokenizer.
> > http://us.php.net/manual/en/ref.tokenizer.php
> > using this tool you can analyze the source to create a list of things
> like
> > - all classes defined in the code
> > - all functions defined in the code
> > once you have these lists you simply iterate over each member of them.
> > if there are no references to a specific function or class, it *should*
> be
> > safe to assume it is no longer in use, however it could be new code, not
> > yet in use.
>
> That's easier said than done :) The case of functions is mostly trivial
> however it becomes much more difficult once you get into the realm of
> dynamic function calling using variables. Additionally the case of
> classes and their methods is difficult to begin with. A method may be
> referenced for an object anywhere, but knowing that a given variable
> holds a particular object is much more difficult. In fact it's
> impossible to properly track in PHP since it's a loosely typed language
> and any variable can hold any type of object.


i didnt say it would be easy :)
also, note how i used the terminology, 'candidate for removal'.
i scrubbed some code at a former job once, using the last
technique i recommended.  i produced a list of files that were no longer in
use.
we passed it around to a lot of people, many times.  finally we had the list
filtered down to a point everyone could agree on.  we first removed the
files
in a pre-production environment.  all-said-and-done we were able to remove
roughly 50 files from the source base.  that was a big load off our chest.
regarding the tokenizing bit; well for one, it just sounds cool ;)
but really i do think there is merit in that approach, however i cant speak
for
any level of practical successes as ive never actually done it myself.
the real problem, as you say, is the dynamic class instantiation, and
function
invocation.  just for example...
   function sayRed() {
      echo 'red';
   }

   function sayBlue() {
      echo 'blue';
   }

$aColor = 'Blue';

$dynFuncName = 'say' . $aColor;
$dynFuncName();

good luck tracking down sayRed() never gets called for example.
anyway, in my experience, the people whove been at a company for a long time
will generally know where dead code is.  thats why i suggested implementing
a
practice where said dead code is marked for removal in production support
(or
whenever its stumbled upon).  then during the current round of development,
yank the dead code.  if the application doesnt blow-up as a result leave it
out on
the way to production. also, im confident in saying i think you can get some
decent
mileage out of technique of searching for files that may no longer be
included.
unless of course the filenames are built dynamically ;)
in reality there are many practical approaches short of automation.  why not
start
by asking all the devs to build a list of code they think is no longer
used.  coalesce the
list and start to pair it down as a group.  its time consuming, but
honestly, i think its
practical.

-nathan

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux