| Conditions | 14 |
| Paths | 70 |
| Total Lines | 48 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 21 | public function validate() |
||
| 22 | { |
||
| 23 | $parameters = func_get_args(); |
||
| 24 | |||
| 25 | if ( count($parameters) == 0 ) |
||
| 26 | { |
||
| 27 | $parameters = ['number', 'cvv', 'expiryMonth', 'expiryYear']; |
||
| 28 | } |
||
| 29 | |||
| 30 | foreach ($parameters as $key) |
||
| 31 | { |
||
| 32 | $value = $this->parameters->get($key); |
||
| 33 | |||
| 34 | if ( empty($value) ) |
||
| 35 | { |
||
| 36 | throw new InvalidCreditCardException("The $key parameter is required"); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | if ( isset($parameters['expiryMonth']) && isset($parameters['expiryYear']) ) |
||
| 41 | { |
||
| 42 | if ( $this->getExpiryDate('Ym') < gmdate('Ym') ) |
||
| 43 | { |
||
| 44 | throw new InvalidCreditCardException('Card has expired'); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | if ( isset($parameters['number']) ) |
||
| 49 | { |
||
| 50 | if ( !Helper::validateLuhn( $this->getNumber() ) ) |
||
| 51 | { |
||
| 52 | throw new InvalidCreditCardException('Card number is invalid'); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ( !is_null( $this->getNumber() ) && !preg_match( '/^\d{12,19}$/i', $this->getNumber() ) ) |
||
| 56 | { |
||
| 57 | throw new InvalidCreditCardException('Card number should have 12 to 19 digits'); |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | if ( isset($parameters['cvv']) ) |
||
| 62 | { |
||
| 63 | if ( !is_null( $this->getCvv() ) && !preg_match( '/^\d{3,4}$/i', $this->getCvv() ) ) |
||
| 64 | { |
||
| 65 | throw new InvalidCreditCardException('Card CVV should have 3 to 4 digits'); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 |