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 |
||
6 | class InvalidArgumentException |
||
7 | extends \InvalidArgumentException |
||
8 | implements \Psr\SimpleCache\InvalidArgumentException |
||
9 | { |
||
10 | 2 | View Code Duplication | public static function fromKeyAndInvalidTTL(string $key, $ttl) : self |
|
|||
11 | { |
||
12 | 2 | return new self(sprintf( |
|
13 | 2 | 'TTL for "%s" should be defined by an integer or a DateInterval, but %s is given.', |
|
14 | $key, |
||
15 | 2 | is_object($ttl) ? get_class($ttl) : gettype($ttl) |
|
16 | )); |
||
17 | } |
||
18 | |||
19 | 1 | public static function fromInvalidKeyCharacters(string $invalidKey) : self |
|
20 | { |
||
21 | 1 | return new self(sprintf( |
|
22 | 1 | 'Key "%s" is in an invalid format - must not contain characters: {}()/\@:', |
|
23 | $invalidKey |
||
24 | )); |
||
25 | } |
||
26 | |||
27 | 1 | View Code Duplication | public static function fromInvalidType($invalidKey) : self |
34 | |||
35 | 1 | public static function fromEmptyKey() : self |
|
39 | |||
40 | 1 | View Code Duplication | public static function fromNonIterableKeys($invalidKeys) : self |
47 | |||
48 | 1 | View Code Duplication | public static function fromNonIterableValues($invalidValues) : self |
55 | } |
||
56 |
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.