Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 10 | class SalesForceTest extends BuildTask |
||
| 11 | { |
||
| 12 | protected $title = 'Test Sales Force API'; |
||
| 13 | |||
| 14 | protected $description = 'Test Sales Cloud'; |
||
| 15 | |||
| 16 | public function run($request) |
||
| 19 | } |
||
| 20 | |||
| 21 | protected function findContact() |
||
| 22 | { |
||
| 23 | print '<h2>Request</h2>'; |
||
| 24 | print "<pre>MySalesForcePartnerAPI::retrieve_contact('[email protected]')</pre>"; |
||
| 25 | $contact = MySalesForcePartnerAPI::retrieve_contact('[email protected]'); |
||
|
|
|||
| 26 | |||
| 27 | print '<h2>Result</h2>'; |
||
| 28 | print '<pre>'; |
||
| 29 | print htmlentities(print_r($contact, true)); |
||
| 30 | print '</pre>'; |
||
| 31 | } |
||
| 32 | |||
| 33 | protected function createContact() |
||
| 34 | { |
||
| 35 | $response = MySalesForcePartnerAPI::create_contact( |
||
| 36 | [ |
||
| 37 | 'FirstName' => 'John', |
||
| 38 | 'LastName' => 'Smith', |
||
| 39 | 'Phone' => '(510) 555-5555', |
||
| 40 | 'BirthDate' => '1957-01-25', |
||
| 41 | 'Email' => '[email protected]', |
||
| 42 | ] |
||
| 43 | ); |
||
| 44 | |||
| 45 | $this->showResults($response); |
||
| 46 | } |
||
| 47 | |||
| 48 | protected function updateContact() |
||
| 49 | { |
||
| 50 | $response = MySalesForcePartnerAPI::update_contact( |
||
| 51 | [ |
||
| 52 | 'FirstName' => 'Joan', |
||
| 53 | 'LastName' => 'Smith', |
||
| 54 | 'Phone' => '(510) 555-5555', |
||
| 55 | 'BirthDate' => '1957-01-25', |
||
| 56 | 'Email' => '[email protected]', |
||
| 57 | ] |
||
| 58 | ); |
||
| 59 | |||
| 60 | $this->showResults($response); |
||
| 61 | } |
||
| 62 | |||
| 63 | protected function showResults($response) |
||
| 64 | { |
||
| 65 | $this->showRequest(); |
||
| 66 | $this->showResponse($response); |
||
| 67 | } |
||
| 68 | |||
| 69 | protected function showRequest() |
||
| 70 | { |
||
| 71 | $connection = MySalesForcePartnerAPIConnectionOnly::singleton(); |
||
| 72 | $this->output(' |
||
| 73 | <h2>Request</h2> |
||
| 74 | <pre> |
||
| 75 | '); |
||
| 76 | $this->output($connection->debug(), true); |
||
| 77 | $this->output(' |
||
| 78 | </pre> |
||
| 79 | '); |
||
| 80 | } |
||
| 81 | |||
| 82 | protected function showResponse($response) |
||
| 83 | { |
||
| 84 | $this->output(' |
||
| 85 | <h2>Response</h2> |
||
| 86 | <pre> |
||
| 87 | '); |
||
| 88 | $this->output($response); |
||
| 89 | $this->output(' |
||
| 90 | </pre> |
||
| 91 | '); |
||
| 92 | } |
||
| 93 | |||
| 94 | protected function output($html, $escape = false) |
||
| 95 | { |
||
| 96 | if (! is_string($html)) { |
||
| 97 | $html = print_r($html, 1); |
||
| 98 | } |
||
| 99 | if ($this->isCli()) { |
||
| 100 | echo "\n"; |
||
| 101 | } elseif ($escape) { |
||
| 102 | $html = htmlentities($html); |
||
| 103 | } |
||
| 104 | |||
| 105 | echo $html; |
||
| 106 | } |
||
| 107 | |||
| 108 | protected function isCli() |
||
| 111 | } |
||
| 112 | } |
||
| 113 |