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 AutomatedEditsHelper |
||
13 | { |
||
14 | |||
15 | /** @var ContainerInterface */ |
||
16 | private $container; |
||
17 | |||
18 | /** @var string[] The list of tools that are considered reverting. */ |
||
19 | public $revertTools = [ |
||
20 | 'Generic rollback', |
||
21 | 'Undo', |
||
22 | 'Pending changes revert', |
||
23 | 'Huggle', |
||
24 | 'STiki', |
||
25 | 'Igloo', |
||
26 | 'WikiPatroller', |
||
27 | 'Twinkle revert', |
||
28 | 'Bot revert' |
||
29 | ]; |
||
30 | |||
31 | /** @var string[] The list of tool names and their regexes. */ |
||
32 | protected $tools; |
||
33 | |||
34 | public function __construct(ContainerInterface $container) |
||
38 | |||
39 | /** |
||
40 | * Was the edit (semi-)automated, based on the edit summary? |
||
41 | * @param string $summary Edit summary |
||
42 | * @return boolean Yes or no |
||
43 | */ |
||
44 | View Code Duplication | public function isAutomated($summary) |
|
54 | |||
55 | /** |
||
56 | * Was the edit a revert, based on the edit summary? |
||
57 | * @param string $summary Edit summary |
||
58 | * @return boolean Yes or no |
||
59 | */ |
||
60 | View Code Duplication | public function isRevert($summary) |
|
72 | |||
73 | /** |
||
74 | * Get the name of the tool that matched the given edit summary |
||
75 | * @param string $summary Edit summary |
||
76 | * @return string|boolean Name of tool, or false if nothing was found |
||
77 | */ |
||
78 | View Code Duplication | public function getTool($summary) |
|
88 | |||
89 | /** |
||
90 | * Get the list of automated tools |
||
91 | * @return array Associative array of 'tool name' => 'regex' |
||
92 | */ |
||
93 | public function getTools() |
||
104 | |||
105 | /** |
||
106 | * Get a summary of automated edits made by the given user in their last 1000 edits. |
||
107 | * Will cache the result for 10 minutes. |
||
108 | * @param integer $userId The user ID. |
||
109 | * @return integer[] Array of edit counts, keyed by all tool names from |
||
110 | * app/config/semi_automated.yml |
||
111 | */ |
||
112 | public function getEditsSummary($userId) |
||
152 | } |
||
153 |
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.