| Conditions | 9 |
| Paths | 9 |
| Total Lines | 53 |
| Code Lines | 29 |
| 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 |
||
| 87 | private function validateSKU(string $sku) |
||
| 88 | { |
||
| 89 | if (strlen($sku) == 0) { |
||
| 90 | throw new SKUException("A SKU cannot be empty"); |
||
| 91 | } |
||
| 92 | |||
| 93 | // check for white-space |
||
| 94 | $containsWhitespace = preg_match($this->whiteSpacePattern, $sku) == 1; |
||
| 95 | if ($containsWhitespace) { |
||
| 96 | throw new SKUException(sprintf("A SKU cannot contain white space characters: \"%s\"", $sku)); |
||
| 97 | } |
||
| 98 | |||
| 99 | // uppercase |
||
| 100 | $containsUppercaseCharacters = preg_match($this->uppercaseCharactersPattern, $sku) == 1; |
||
| 101 | if ($containsUppercaseCharacters) { |
||
| 102 | throw new SKUException(sprintf("A SKU cannot contain uppercase characters: \"%s\"", $sku)); |
||
| 103 | } |
||
| 104 | |||
| 105 | // check for invalid characters |
||
| 106 | $containsInvalidCharacters = preg_match($this->invalidCharactersPattern, $sku) == 1; |
||
| 107 | if ($containsInvalidCharacters) { |
||
| 108 | throw new SKUException(sprintf("The SKU \"%s\" contains invalid characters. A SKU can only contain the following characters: a-z, 0-9 and -", |
||
| 109 | $sku)); |
||
| 110 | } |
||
| 111 | |||
| 112 | // check prefix |
||
| 113 | $prefixMatches = []; |
||
| 114 | $prefixContainsInvalidCharacters = preg_match($this->invalidPrefixCharacters, $sku, $prefixMatches) == 1; |
||
| 115 | if ($prefixContainsInvalidCharacters) { |
||
| 116 | throw new SKUException(sprintf("A SKU cannot start with the given characters: \"%s\"", |
||
| 117 | implode("", $prefixMatches))); |
||
| 118 | } |
||
| 119 | |||
| 120 | // check suffix |
||
| 121 | $suffixMatches = []; |
||
| 122 | $suffixContainsInvalidCharacters = preg_match($this->invalidSuffixCharacters, $sku, $suffixMatches) == 1; |
||
| 123 | if ($suffixContainsInvalidCharacters) { |
||
| 124 | throw new SKUException(sprintf("A SKU cannot end with the given characters: \"%s\"", |
||
| 125 | implode("", $suffixMatches))); |
||
| 126 | } |
||
| 127 | |||
| 128 | // check minimum length |
||
| 129 | if (strlen($sku) < $this->minLength) { |
||
| 130 | throw new SKUException(sprintf("The given SKU \"%s\" is too short. The minimum length for a SKU is: %s", |
||
| 131 | $sku, $this->minLength)); |
||
| 132 | } |
||
| 133 | |||
| 134 | // check maximum length |
||
| 135 | if (strlen($sku) > $this->maxLength) { |
||
| 136 | throw new SKUException(sprintf("The given SKU \"%s\" is too long (%s character). The maximum length for a SKU is: %s", |
||
| 137 | strlen($sku), $sku, $this->maxLength)); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |