Has anyone tried refactoring a "real world" application/library to use
namespaces?

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).

I've come up with the following, for an organization model of a class per
file.
Please review.

1) find the class definition and reduce the class name to namespace/class
2) add namespace declaration on top of file
3) replace class name in definition with short name
4) find all function calls (not method calls) which are not keywords (array,
isset, unset, list, etc) and prefix them with ::
5) find all class names (static method calls (no self:: nor parent::), new,
catch, type hints, instanceof, extends, implements) and:
5a) replace separator used (possibly: _) with namespace separator (::)
5b) IF the class has no namespace (ie global namespace) prefix the class
name with ::
5c) IF the namespace of the class starts with the same namespace found in
(1), remove it or prefix it with namespace::
      (although this is not necessary)
6) find static method calls with variables used as class name, and mark them
for user handled refactoring
7) find internal functions that use callables (call_user_func, array_map,
array_filter, etc) and mark them for user handled refactoring

Later on, a methodical way of adding use statements for reducing the class
names used, can be implemented. Although this might be more complicate than
it seems if the method has to be careful to avoid creating possible name
clashes.

If anyone has a comment on this, I'll be more than grateful if you share.

Best Regards,

Martin Alterisio

Reply via email to