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 |
||
| 3 | class APIProject extends APINoun { |
||
|
|
|||
| 4 | |||
| 5 | /** |
||
| 6 | * @var array |
||
| 7 | */ |
||
| 8 | private static $allowed_actions = array( |
||
| 9 | 'index', |
||
| 10 | 'fetch' |
||
| 11 | ); |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param \SS_HTTPRequest $request |
||
| 15 | * @return \SS_HTTPResponse |
||
| 16 | */ |
||
| 17 | public function index(\SS_HTTPRequest $request) { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param \SS_HTTPRequest $request |
||
| 47 | * @return \SS_HTTPResponse |
||
| 48 | */ |
||
| 49 | View Code Duplication | public function fetch(\SS_HTTPRequest $request) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param int $ID |
||
| 65 | * @return SS_HTTPResponse |
||
| 66 | */ |
||
| 67 | View Code Duplication | protected function getFetch($ID) { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @return SS_HTTPResponse |
||
| 82 | */ |
||
| 83 | View Code Duplication | protected function createFetch() { |
|
| 84 | /** @var DNGitFetch $fetch */ |
||
| 85 | $fetch = DNGitFetch::create(); |
||
| 86 | $fetch->ProjectID = $this->record->ID; |
||
| 87 | $fetch->write(); |
||
| 88 | $fetch->start(); |
||
| 89 | |||
| 90 | $location = Director::absoluteBaseURL() . $this->Link() . '/fetch/' . $fetch->ID; |
||
| 91 | $output = array( |
||
| 92 | 'message' => 'Fetch queued as job ' . $fetch->ResqueToken, |
||
| 93 | 'href' => $location, |
||
| 94 | ); |
||
| 95 | |||
| 96 | $response = $this->getAPIResponse($output); |
||
| 97 | $response->setStatusCode(201); |
||
| 98 | $response->addHeader('Location', $location); |
||
| 99 | return $response; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | public function Link() { |
||
| 111 | } |
||
| 112 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.