1

I have a legacy API in PHP that I need to refactor. The request must include an operation (eg. 'get_some_data') which is determined in a massive if/else sequence. All refactoring recommendations I find online consist of converting procedural to OOP, but in this application there is not really a logical grouping of the operations.

Is there some best practises (or good tips) you have for refactoring a project like this? The final goal is full automated test coverage.

user3654410
  • 464
  • 3
  • 13
  • Please provide more details about your project, like snippet of code so we could find what to refactor. – Karim Harazin May 04 '17 at 07:18
  • I would start by replacing repeated code with function calls, grouping them into classes. That would also give complete knowledge of the code, which will help with reorganising it. – inarilo May 04 '17 at 07:19
  • I can't really upload the code here, as it's proprietary, but it goes something like this: if($_POST[operation]==5){ mysql_query("INSERT INTO TABLE users VALUES (" . $_POST[username] . "); echo 1; } else if($_POST[operation]==6) { /* ... */ } and so forth. Horrible code, but I have determined the actions of the numeric codes. – user3654410 May 04 '17 at 07:49
  • Typo: "INSERT INTO users", but you get the gist. – user3654410 May 04 '17 at 07:57

0 Answers0