Refactoring php code