2007/12/21, Paul Biggar <[EMAIL PROTECTED]>:
>
> Hi Martin,
>
> On Dec 21, 2007 8:30 PM, Martin Alterisio <[EMAIL PROTECTED]> wrote:
> > I've been pondering about how to algorithmically/mechanically prepare a
> non
> > namespaced code for namespaces appliance. This would be a first step
> which
> > will just prepare the code for namespaces in a safe way, but not to
> profit
> > from aliases created by use. The latest will have to be implemented
> manually
> > (IMHO).
>
> phc (www.phpcompiler.org) would be suitable for this purpose. It has
> good support for nearly all of the features you need.


Thank you, that looks like it's the tool for the job.

> 4) find all function calls (not method calls) which are not keywords
> (array,
> > isset, unset, list, etc) and prefix them with ::
>
> list, array etc wouldnt be confused with functions. Functions are just
> methods without target. You'd need a short list of functions such as
> empty, unset etc to avoid.


I didn't quite understand your point here. If you can elaborate a little
more.

Take into consideration that the reason here for the distinction between
function calls which are actually keywords are due to the fact that the
following wouldn't be valid:

if (::isset($var)) {

Because isset is actually a keyword.

And to ensure that the code is ported safely to code which uses namespaces,
the function calls which point to globals or internal functions, should be
prefixed with ::

> 6) find static method calls with variables used as class name, and mark
> them
> > for user handled refactoring
>
> I'm not sure why this couldnt be done automatically, but finding
> static method calls is also easy. You could add comments to mark these
> fairly easily.


Maybe some workaround can be found to str_replace the namespace separator,
but I think this wouldn't be optimal and possibly harm code readability and
maintainability. Consider a factory method:

<?php
class Factory {
  public static function create($what, $arguments) {
    switch ($what) {
    case 'bananas':
      $class = 'Food_Fruit_Banana';
      break;
      ...
    }
    return $class::create($arguments);
  }
}
?>

An automated refactoring would be tempted to do:

<?php
class Factory {
  public static function create($what, $arguments) {
    switch ($what) {
    case 'bananas':
      $class = 'Food_Fruit_Banana';
      break;
      ...
    }
    $temp = str_replace('_', '::', $class);
    return $temp::create($arguments);
  }
}
?>

Which I think is not the proper way to refactorize this code.
This would be, IMHO, the right way to refactorize this code:

<?php
class Factory {
  public static function create($what, $arguments) {
    switch ($what) {
    case 'bananas':
      $class = 'Food::Fruit::Banana';
      break;
      ...
    }
    return $class::create($arguments);
  }
}
?>

Considering the variants and possible uses of this syntax, I'll say it's
better to leave this job to the coder rather than to an automated job.

Thank you for your response.

Best Regards,

Martin Alterisio

Reply via email to