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 |
||
| 14 | class DNGitFetch extends DataObject { |
||
|
|
|||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private static $db = array( |
||
| 20 | "ResqueToken" => "Varchar(255)", |
||
| 21 | // Observe that this is not the same as Resque status, since ResqueStatus is not persistent |
||
| 22 | // It's used for finding successful deployments and displaying that in history views in the frontend |
||
| 23 | "Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')", |
||
| 24 | ); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $has_one = array( |
||
| 30 | "Project" => "DNProject", |
||
| 31 | "Deployer" => "Member" |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param int $int |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | View Code Duplication | public static function map_resque_status($int) { |
|
| 39 | $remap = array( |
||
| 40 | Resque_Job_Status::STATUS_WAITING => "Queued", |
||
| 41 | Resque_Job_Status::STATUS_RUNNING => "Running", |
||
| 42 | Resque_Job_Status::STATUS_FAILED => "Failed", |
||
| 43 | Resque_Job_Status::STATUS_COMPLETE => "Complete", |
||
| 44 | false => "Invalid", |
||
| 45 | ); |
||
| 46 | return $remap[$int]; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Queue a fetch job |
||
| 51 | * @param bool $forceClone Force repository to be re-cloned |
||
| 52 | */ |
||
| 53 | public function start($forceClone = false) { |
||
| 54 | $project = $this->Project(); |
||
| 55 | $log = $this->log(); |
||
| 56 | |||
| 57 | if(!$this->DeployerID) { |
||
| 58 | $this->DeployerID = Member::currentUserID(); |
||
| 59 | } |
||
| 60 | |||
| 61 | View Code Duplication | if($this->DeployerID) { |
|
| 62 | $deployer = $this->Deployer(); |
||
| 63 | $message = sprintf( |
||
| 64 | 'Update repository job for %s initiated by %s (%s)', |
||
| 65 | $project->Name, |
||
| 66 | $deployer->getName(), |
||
| 67 | $deployer->Email |
||
| 68 | ); |
||
| 69 | $log->write($message); |
||
| 70 | } |
||
| 71 | |||
| 72 | // write first, so we have the ID. We have to write again |
||
| 73 | // later once we have the resque token. |
||
| 74 | $this->write(); |
||
| 75 | |||
| 76 | $args = array( |
||
| 77 | 'projectID' => $project->ID, |
||
| 78 | 'logfile' => $this->logfile(), |
||
| 79 | 'fetchID' => $this->ID, |
||
| 80 | 'forceClone' => $forceClone |
||
| 81 | ); |
||
| 82 | |||
| 83 | $token = Resque::enqueue('git', 'FetchJob', $args, true); |
||
| 84 | $this->ResqueToken = $token; |
||
| 85 | $this->write(); |
||
| 86 | |||
| 87 | $message = sprintf('Fetch queued as job %s', $token); |
||
| 88 | $log->write($message); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Member|null $member |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | public function canView($member = null) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Return a path to the log file. |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | protected function logfile() { |
||
| 104 | return sprintf( |
||
| 105 | '%s.fetch.%s.log', |
||
| 106 | $this->Project()->Name, |
||
| 107 | $this->ID |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return \DeploynautLogFile |
||
| 113 | */ |
||
| 114 | public function log() { |
||
| 115 | return new DeploynautLogFile($this->logfile()); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function LogContent() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the status of the resque job |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function ResqueStatus() { |
||
| 147 | |||
| 148 | } |
||
| 149 |
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.