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 |
||
21 | class BaseInitCommand extends Command |
||
22 | { |
||
23 | /** |
||
24 | * The name and signature of the console command. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $signature = 'starter:base'; |
||
29 | |||
30 | /** |
||
31 | * The console command description. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $description = 'Create starter base files'; |
||
36 | |||
37 | /** |
||
38 | * The Filesystem instance. |
||
39 | * |
||
40 | * @var Filesystem |
||
41 | */ |
||
42 | protected $filesystem; |
||
43 | |||
44 | /** |
||
45 | * Filename of stub-file. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $stubs = [ |
||
50 | 'api.controller.laravel' => 'Api/LaravelBaseController', |
||
51 | 'api.controller.lumen' => 'Api/LumenBaseController', |
||
52 | 'http.controller.laravel' => 'Http/LaravelBaseController', |
||
53 | 'http.controller.lumen' => 'Http/LumenBaseController', |
||
54 | 'model' => 'Models/BaseModel', |
||
55 | 'presenter' => 'Presenters/BasePresenter', |
||
56 | 'repositories.eloquent' => 'Repositories/Eloquent/BaseRepository', |
||
57 | 'repositories.interfaces' => 'Repositories/Interfaces/BaseRepositoryInterface', |
||
58 | 'transformer' => 'Transformers/BaseTransformer', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * BaseInitCommand constructor. |
||
63 | * |
||
64 | * @param Filesystem $filesystem |
||
65 | */ |
||
66 | public function __construct(Filesystem $filesystem) |
||
72 | |||
73 | public function handle() |
||
89 | |||
90 | public function generateApiController() |
||
91 | { |
||
92 | $stub = is_lumen() ? $this->stubs['api.controller.lumen'] : $this->stubs['api.controller.laravel']; |
||
93 | $stub = $this->getStub($stub); |
||
94 | |||
95 | $controllerGenerator = new ControllerGenerator(['name' => 'Base']); |
||
96 | $namespace = $controllerGenerator->getNamespace(); |
||
97 | |||
98 | $path = $controllerGenerator->getBasePath().'/'.$controllerGenerator->getConfigGeneratorClassPath($controllerGenerator->getPathConfigNode(), |
||
99 | true).'/BaseController.php'; |
||
100 | |||
101 | $this->generateFile($path, $stub, $namespace); |
||
102 | } |
||
103 | |||
104 | public function generateHttpController() |
||
113 | |||
114 | View Code Duplication | public function generateModel() |
|
|
|||
115 | { |
||
116 | $stub = $this->stubs['model']; |
||
117 | $stub = $this->getStub($stub); |
||
118 | $generator = new ModelGenerator(['name' => 'BaseModel']); |
||
119 | $namespace = $generator->getNamespace(); |
||
120 | $path = $generator->getPath(); |
||
121 | |||
122 | $this->generateFile($path, $stub, $namespace); |
||
123 | } |
||
124 | |||
125 | View Code Duplication | public function generatePresenters() |
|
126 | { |
||
127 | $stub = $this->stubs['presenter']; |
||
128 | $stub = $this->getStub($stub); |
||
129 | $generator = new PresenterGenerator(['name' => 'Base']); |
||
130 | $namespace = $generator->getNamespace(); |
||
131 | $path = $generator->getPath(); |
||
132 | |||
133 | $this->generateFile($path, $stub, $namespace); |
||
134 | } |
||
135 | |||
136 | View Code Duplication | public function generateRepositoriesEloquent() |
|
137 | { |
||
138 | $stub = $this->stubs['repositories.eloquent']; |
||
139 | $stub = $this->getStub($stub); |
||
140 | $generator = new RepositoryEloquentGenerator(['name' => 'Base']); |
||
141 | $namespace = $generator->getNamespace(); |
||
142 | $path = $generator->getBasePath().'/'.$generator->getConfigGeneratorClassPath($generator->getPathConfigNode(), |
||
143 | true).'/BaseRepository.php'; |
||
144 | |||
145 | $this->generateFile($path, $stub, $namespace); |
||
146 | } |
||
147 | |||
148 | View Code Duplication | public function generateRepositoriesInterfaces() |
|
149 | { |
||
150 | $stub = $this->stubs['repositories.interfaces']; |
||
151 | $stub = $this->getStub($stub); |
||
152 | $generator = new RepositoryInterfaceGenerator(['name' => 'Base']); |
||
153 | $namespace = $generator->getNamespace(); |
||
154 | $path = $generator->getBasePath().'/'.$generator->getConfigGeneratorClassPath($generator->getPathConfigNode(), |
||
155 | true).'/BaseRepositoryInterface.php'; |
||
156 | |||
157 | $this->generateFile($path, $stub, $namespace); |
||
158 | } |
||
159 | |||
160 | View Code Duplication | public function generateTransformer() |
|
161 | { |
||
162 | $stub = $this->stubs['transformer']; |
||
163 | $stub = $this->getStub($stub); |
||
164 | $generator = new TransformerGenerator(['name' => 'Base']); |
||
165 | $namespace = $generator->getNamespace(); |
||
166 | $path = $generator->getPath(); |
||
167 | |||
168 | $this->generateFile($path, $stub, $namespace); |
||
169 | } |
||
170 | |||
171 | public function generateFile($path, $stub, $namespace) |
||
172 | { |
||
173 | if (! $this->filesystem->exists($path) || $this->confirm($path.' already exists! Continue?')) { |
||
174 | $content = str_replace('$NAMESPACE$', $namespace, $stub); |
||
175 | |||
176 | View Code Duplication | if (! $this->filesystem->isDirectory($dir = dirname($path))) { |
|
177 | $this->filesystem->makeDirectory($dir, 0777, true, true); |
||
178 | } |
||
179 | |||
180 | $this->filesystem->put($path, $content); |
||
181 | $this->line('---------------'); |
||
182 | $this->info($path.' generated'); |
||
183 | $this->line('---------------'); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | public function getPath($stub) |
||
199 | |||
200 | public function getStub($stub) |
||
204 | } |
||
205 |
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.