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 namespace Modules\Core\Console\Installers\Scripts\UserProviders; |
||
9 | abstract class ProviderInstaller implements SetupScript |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $driver; |
||
15 | |||
16 | /** |
||
17 | * @var Command |
||
18 | */ |
||
19 | protected $command; |
||
20 | |||
21 | /** |
||
22 | * @var Filesystem |
||
23 | */ |
||
24 | protected $finder; |
||
25 | |||
26 | /** |
||
27 | * @var Composer |
||
28 | */ |
||
29 | protected $composer; |
||
30 | |||
31 | /** |
||
32 | * @var Application |
||
33 | */ |
||
34 | protected $application; |
||
35 | |||
36 | /** |
||
37 | * @param Filesystem $finder |
||
38 | * @param Composer $composer |
||
39 | * @param Application $application |
||
40 | */ |
||
41 | public function __construct(Filesystem $finder, Composer $composer, Application $application) |
||
48 | |||
49 | /** |
||
50 | * Fire the install script |
||
51 | * @param Command $command |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function fire(Command $command) |
||
55 | { |
||
56 | $this->command = $command; |
||
57 | |||
58 | // Publish asgard configs |
||
59 | View Code Duplication | if ($this->command->option('verbose')) { |
|
|
|||
60 | $this->command->call('vendor:publish', ['--provider' => 'Modules\Core\Providers\CoreServiceProvider']); |
||
61 | } else { |
||
62 | $this->command->callSilent('vendor:publish', ['--provider' => 'Modules\Core\Providers\CoreServiceProvider']); |
||
63 | } |
||
64 | |||
65 | if (! $this->checkIsInstalled()) { |
||
66 | return $this->command->error('No user driver was installed. Please check the presence of a Service Provider'); |
||
67 | } |
||
68 | |||
69 | $this->publish(); |
||
70 | $this->configure(); |
||
71 | $this->migrate(); |
||
72 | $this->seed(); |
||
73 | |||
74 | $this->createFirstUser(); |
||
75 | |||
76 | if ($this->command->option('verbose')) { |
||
77 | $command->info($this->driver . ' succesfully configured'); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @return mixed |
||
83 | */ |
||
84 | abstract public function composer(); |
||
85 | |||
86 | /** |
||
87 | * Check if the user driver is correctly registered. |
||
88 | * @return bool |
||
89 | */ |
||
90 | abstract public function checkIsInstalled(); |
||
91 | |||
92 | /** |
||
93 | * @return mixed |
||
94 | */ |
||
95 | abstract public function publish(); |
||
96 | |||
97 | /** |
||
98 | * @return mixed |
||
99 | */ |
||
100 | abstract public function migrate(); |
||
101 | |||
102 | /** |
||
103 | * @return mixed |
||
104 | */ |
||
105 | abstract public function seed(); |
||
106 | |||
107 | /** |
||
108 | * @return mixed |
||
109 | */ |
||
110 | abstract public function configure(); |
||
111 | |||
112 | /** |
||
113 | * @param $password |
||
114 | * @return mixed |
||
115 | */ |
||
116 | abstract public function getHashedPassword($password); |
||
117 | |||
118 | /** |
||
119 | * @param $search |
||
120 | * @param $Driver |
||
121 | */ |
||
122 | protected function replaceCartalystUserModelConfiguration($search, $Driver) |
||
134 | |||
135 | /** |
||
136 | * Set the correct repository binding on the fly for the current request |
||
137 | * |
||
138 | * @param $driver |
||
139 | */ |
||
140 | protected function bindUserRepositoryOnTheFly($driver) |
||
155 | |||
156 | /** |
||
157 | * Create a first admin user |
||
158 | */ |
||
159 | protected function createFirstUser() |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | private function askForFirstName() |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | private function askForLastName() |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | private function askForEmail() |
||
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | private function askForPassword() |
||
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | private function askForFirstPassword() |
||
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | private function askForPasswordConfirmation() |
||
269 | } |
||
270 |
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.