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; |
||
5 | class UsherInstaller extends ProviderInstaller implements SetupScript |
||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | protected $driver = 'Usher'; |
||
11 | |||
12 | /** |
||
13 | * Check if the user driver is correctly registered. |
||
14 | * @return bool |
||
15 | */ |
||
16 | public function checkIsInstalled() |
||
21 | |||
22 | /** |
||
23 | * Not called |
||
24 | * @return mixed |
||
25 | */ |
||
26 | public function composer() |
||
30 | |||
31 | /** |
||
32 | * @return mixed |
||
33 | */ |
||
34 | public function publish() |
||
35 | { |
||
36 | View Code Duplication | if ($this->command->option('verbose')) { |
|
|
|||
37 | $this->command->call('vendor:publish', ['--provider' => 'Maatwebsite\Usher\UsherServiceProvider']); |
||
38 | |||
39 | return $this->command->call('vendor:publish', ['--provider' => 'Mitch\LaravelDoctrine\LaravelDoctrineServiceProvider']); |
||
40 | } |
||
41 | |||
42 | $this->command->callSilent('vendor:publish', ['--provider' => 'Maatwebsite\Usher\UsherServiceProvider']); |
||
43 | |||
44 | return $this->command->callSilent('vendor:publish', ['--provider' => 'Mitch\LaravelDoctrine\LaravelDoctrineServiceProvider']); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return mixed |
||
49 | */ |
||
50 | public function migrate() |
||
51 | { |
||
52 | if ($this->command->option('verbose')) { |
||
53 | return $this->command->call('doctrine:schema:update'); |
||
54 | } |
||
55 | |||
56 | return $this->command->callSilent('doctrine:schema:update'); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function configure() |
||
63 | { |
||
64 | $path = base_path("config/usher.php"); |
||
65 | |||
66 | $config = $this->finder->get($path); |
||
67 | |||
68 | $config = str_replace('Maatwebsite\Usher\Domain\Users\UsherUser', "Modules\\User\\Entities\\{$this->driver}\\User", $config); |
||
69 | $config = str_replace('Maatwebsite\Usher\Domain\Roles\UsherRole', "Modules\\User\\Entities\\{$this->driver}\\Role", $config); |
||
70 | |||
71 | $this->finder->put($path, $config); |
||
72 | |||
73 | // Doctrine config |
||
74 | $path = base_path("config/doctrine.php"); |
||
75 | |||
76 | $config = $this->finder->get($path); |
||
77 | |||
78 | $config = str_replace('// Paths to entities here...', 'base_path(\'Modules/User/Entities/Usher\')', $config); |
||
79 | |||
80 | $this->finder->put($path, $config); |
||
81 | |||
82 | $this->bindUserRepositoryOnTheFly('Usher'); |
||
83 | |||
84 | $this->application->register('Maatwebsite\Usher\UsherServiceProvider'); |
||
85 | $this->application->register('Mitch\LaravelDoctrine\LaravelDoctrineServiceProvider'); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return mixed |
||
90 | */ |
||
91 | View Code Duplication | public function seed() |
|
92 | { |
||
93 | if ($this->command->option('verbose')) { |
||
94 | return $this->command->call('db:seed', ['--class' => 'Modules\User\Database\Seeders\UsherTableSeeder']); |
||
95 | } |
||
96 | |||
97 | return $this->command->callSilent('db:seed', ['--class' => 'Modules\User\Database\Seeders\UsherTableSeeder']); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param $password |
||
102 | * @return mixed |
||
103 | */ |
||
104 | public function getHashedPassword($password) |
||
108 | } |
||
109 |
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.