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 |
||
| 7 | trait RecurringProfiles |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Create recurring subscription on custom frequency basis. |
||
| 11 | * The billing frequency is in months, i.e: if you want a monthly subscription, then your billing frequency would be 1. |
||
| 12 | * |
||
| 13 | * @param string $token |
||
| 14 | * @param float $amount |
||
| 15 | * @param string $description |
||
| 16 | * @param int $billingFrequency |
||
| 17 | * @param int $trialDays |
||
| 18 | * |
||
| 19 | * @return array |
||
| 20 | */ |
||
| 21 | public function createCustomSubscription($token, $amount, $description, $billingFrequency, $trialDays = null) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Create recurring subscription on monthly basis. |
||
| 44 | * |
||
| 45 | * @param string $token |
||
| 46 | * @param float $amount |
||
| 47 | * @param string $description |
||
| 48 | * |
||
| 49 | * @return array |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function createMonthlySubscription($token, $amount, $description) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Create recurring subscription on yearly basis. |
||
| 67 | * |
||
| 68 | * @param string $token |
||
| 69 | * @param float $amount |
||
| 70 | * @param string $description |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function createYearlySubscription($token, $amount, $description) |
|
| 87 | } |
||
| 88 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: