Php code refactoring