Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing refactoring-friendly PHP code

As far as I know, and have gathered from other SO posts, there are no proper tools for refactoring PHP code yet, so when it comes to refactoring, it's probably good old search-and-replace for most of us, with a good amount of praying that we didn't overlook something.

I would like to know whether there are any coding recommendations on how to write code friendly for manual refactoring. Never to construct variable names from strings, would be one thing that comes to mind because a construct like that is impossible to grep:

$object->{"field_".$fieldname}

I could imagine there are several such do's and don'ts. Maybe somebody knows good resources / articles on the issue. It wouldn't have to be PHP specific, either.

like image 595
Pekka Avatar asked Nov 16 '09 02:11

Pekka


2 Answers

Unit tests always help me identify places where I've broken code due to a refactor. Unit tests in dynamic languages (PHP, Ruby, Python, etc.) provide assistance where static typing in other languages (Java, C#) would typically allow you to more safely refactor.

like image 152
John Paulett Avatar answered Oct 14 '22 06:10

John Paulett


Avoid magic as much as possible: variable variables, eval, masking errors with @ and storing code in the database will come back to bite you.

like image 24
Ken Avatar answered Oct 14 '22 06:10

Ken