Conditions | 10 |
Paths | 31 |
Total Lines | 25 |
Code Lines | 16 |
Lines | 12 |
Ratio | 48 % |
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 |
||
77 | public function setAppValue($key, $value) { |
||
78 | if ($key === OcrConstants::LANGUAGES_CONFIG_KEY) { |
||
79 | if (! preg_match ( '/^(([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4});)*([a-z]{3,4}|[a-z]{3,4}\-[a-z]{3,4})$/', $value )) { |
||
80 | throw new NotFoundException ( $this->l10n->t ( 'The languages are not specified in the correct format.' ) ); |
||
81 | } |
||
82 | } |
||
83 | if ($key === OcrConstants::REDIS_CONFIG_KEY_HOST) { |
||
84 | if (! preg_match ( '/(^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$)/', $value )) { |
||
85 | throw new NotFoundException ( $this->l10n->t ( 'The redis host is not specified in the correct format.' ) ); |
||
86 | } |
||
87 | } |
||
88 | View Code Duplication | if ($key === OcrConstants::REDIS_CONFIG_KEY_PORT) { |
|
|
|||
89 | $value = intval ( $value ); |
||
90 | if (! ($value > 0 && $value < 65535)) { |
||
91 | throw new NotFoundException ( $this->l10n->t ( 'The redis port number is not specified in the correct format.' ) ); |
||
92 | } |
||
93 | } |
||
94 | View Code Duplication | if ($key === OcrConstants::REDIS_CONFIG_KEY_DB) { |
|
95 | $value = intval ( $value ); |
||
96 | if (! ($value >= 0)) { |
||
97 | throw new NotFoundException ( $this->l10n->t ( 'The redis db is not specified in the correct format.' ) ); |
||
98 | } |
||
99 | } |
||
100 | return $this->config->setAppValue ( $this->appName, $key, $value ); |
||
101 | } |
||
102 | } |
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.