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 |
||
12 | class CrawlDelay implements DirectiveInterface, RobotsTxtInterface |
||
13 | { |
||
14 | use Toolbox; |
||
15 | |||
16 | /** |
||
17 | * Directive alternatives |
||
18 | */ |
||
19 | const DIRECTIVE = [ |
||
20 | self::DIRECTIVE_CACHE_DELAY, |
||
21 | self::DIRECTIVE_CRAWL_DELAY, |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * Directive |
||
26 | */ |
||
27 | protected $directive = self::DIRECTIVE_CRAWL_DELAY; |
||
28 | |||
29 | /** |
||
30 | * Delay |
||
31 | * @var float|int |
||
32 | */ |
||
33 | protected $value; |
||
34 | |||
35 | /** |
||
36 | * CrawlDelay constructor. |
||
37 | * @param string $directive |
||
38 | */ |
||
39 | public function __construct($directive = self::DIRECTIVE_CRAWL_DELAY) |
||
43 | |||
44 | /** |
||
45 | * Add |
||
46 | * |
||
47 | * @param float|int|string $line |
||
48 | * @return bool |
||
49 | */ |
||
50 | public function add($line) |
||
66 | |||
67 | /** |
||
68 | * Export rules |
||
69 | * |
||
70 | * @return float[]|int[]|string[] |
||
71 | */ |
||
72 | public function export() |
||
76 | |||
77 | /** |
||
78 | * Render |
||
79 | * |
||
80 | * @return string[] |
||
81 | */ |
||
82 | View Code Duplication | public function render() |
|
89 | } |
||
90 |
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.