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 |
||
| 26 | class BankAccount extends ApiResource |
||
| 27 | { |
||
| 28 | |||
| 29 | const OBJECT_NAME = "bank_account"; |
||
| 30 | |||
| 31 | use ApiOperations\Delete; |
||
| 32 | use ApiOperations\Update; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Possible string representations of the bank verification status. |
||
| 36 | * @link https://stripe.com/docs/api/external_account_bank_accounts/object#account_bank_account_object-status |
||
| 37 | */ |
||
| 38 | const STATUS_NEW = 'new'; |
||
| 39 | const STATUS_VALIDATED = 'validated'; |
||
| 40 | const STATUS_VERIFIED = 'verified'; |
||
| 41 | const STATUS_VERIFICATION_FAILED = 'verification_failed'; |
||
| 42 | const STATUS_ERRORED = 'errored'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return string The instance URL for this resource. It needs to be special |
||
| 46 | * cased because it doesn't fit into the standard resource pattern. |
||
| 47 | */ |
||
| 48 | public function instanceUrl() |
||
| 49 | { |
||
| 50 | if ($this['customer']) { |
||
| 51 | $base = Customer::classUrl(); |
||
| 52 | $parent = $this['customer']; |
||
| 53 | $path = 'sources'; |
||
| 54 | View Code Duplication | } elseif ($this['account']) { |
|
|
|
|||
| 55 | $base = Account::classUrl(); |
||
| 56 | $parent = $this['account']; |
||
| 57 | $path = 'external_accounts'; |
||
| 58 | } else { |
||
| 59 | $msg = "Bank accounts cannot be accessed without a customer ID or account ID."; |
||
| 60 | throw new Error\InvalidRequest($msg, null); |
||
| 61 | } |
||
| 62 | $parentExtn = urlencode(Util\Util::utf8($parent)); |
||
| 63 | $extn = urlencode(Util\Util::utf8($this['id'])); |
||
| 64 | return "$base/$parentExtn/$path/$extn"; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param array|string $_id |
||
| 69 | * @param array|string|null $_opts |
||
| 70 | * |
||
| 71 | * @throws \Stripe\Error\InvalidRequest |
||
| 72 | */ |
||
| 73 | public static function retrieve($_id, $_opts = null) |
||
| 74 | { |
||
| 75 | $msg = "Bank accounts cannot be accessed without a customer ID or account ID. " . |
||
| 76 | "Retrieve a bank account using \$customer->sources->retrieve('bank_account_id') or " . |
||
| 77 | "\$account->external_accounts->retrieve('bank_account_id') instead."; |
||
| 78 | throw new Error\InvalidRequest($msg, null); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $_id |
||
| 83 | * @param array|null $_params |
||
| 84 | * @param array|string|null $_options |
||
| 85 | * |
||
| 86 | * @throws \Stripe\Error\InvalidRequest |
||
| 87 | */ |
||
| 88 | View Code Duplication | public static function update($_id, $_params = null, $_options = null) |
|
| 89 | { |
||
| 90 | $msg = "Bank accounts cannot be accessed without a customer ID or account ID. " . |
||
| 91 | "Call save() on \$customer->sources->retrieve('bank_account_id') or " . |
||
| 92 | "\$account->external_accounts->retrieve('bank_account_id') instead."; |
||
| 93 | throw new Error\InvalidRequest($msg, null); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param array|null $params |
||
| 98 | * @param array|string|null $options |
||
| 99 | * |
||
| 100 | * @return BankAccount The verified bank account. |
||
| 101 | */ |
||
| 102 | public function verify($params = null, $options = null) |
||
| 109 | } |
||
| 110 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.