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 |
||
8 | class MatchAllResult extends RegexResult |
||
9 | { |
||
10 | /** @var string */ |
||
11 | protected $pattern; |
||
12 | |||
13 | /** @var string */ |
||
14 | protected $subject; |
||
15 | |||
16 | /** @var bool */ |
||
17 | protected $hasMatch; |
||
18 | |||
19 | /** @var array */ |
||
20 | protected $matches; |
||
21 | |||
22 | View Code Duplication | public function __construct(string $pattern, string $subject, bool $result, array $matches) |
|
29 | |||
30 | /** |
||
31 | * @param string $pattern |
||
32 | * @param string $subject |
||
33 | * |
||
34 | * @return static |
||
35 | * |
||
36 | * @throws \Spatie\Regex\RegexFailed |
||
37 | */ |
||
38 | View Code Duplication | public static function for(string $pattern, string $subject) |
|
54 | |||
55 | public function hasMatch(): bool |
||
59 | |||
60 | /** |
||
61 | * @return \Spatie\Regex\MatchResult[] |
||
62 | */ |
||
63 | public function results(): array |
||
69 | } |
||
70 |
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.