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 |
||
13 | class CleanUpModelsCommand extends Command |
||
14 | { |
||
15 | /** |
||
16 | * The console command name. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $signature = 'clean:models'; |
||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Clean up models.'; |
||
27 | |||
28 | protected $filesystem; |
||
29 | |||
30 | public function __construct(Filesystem $filesystem) |
||
36 | |||
37 | public function handle() |
||
51 | |||
52 | View Code Duplication | protected function getModelsThatShouldBeCleanedUp() : Collection |
|
64 | |||
65 | View Code Duplication | protected function getModelsThatShouldBeForcedCleanedUp() : Collection |
|
77 | |||
78 | protected function cleanUp(Collection $cleanableModels) |
||
79 | { |
||
80 | $cleanableModels->each(function (string $modelClass) { |
||
81 | |||
82 | $numberOfDeletedRecords = $modelClass::cleanUp($modelClass::query())->delete(); |
||
83 | |||
84 | event(new ModelWasCleanedUp($modelClass, $numberOfDeletedRecords)); |
||
85 | |||
86 | $this->info("Deleted {$numberOfDeletedRecords} record(s) from {$modelClass}."); |
||
87 | |||
88 | }); |
||
89 | } |
||
90 | |||
91 | protected function forceCleanUp(Collection $cleanableModels) |
||
92 | { |
||
93 | $cleanableModels->each(function (string $modelClass) { |
||
94 | |||
95 | $numberOfDeletedRecords = $modelClass::forceCleanUp($modelClass::query())->forceDelete(); |
||
96 | |||
97 | event(new ModelWasCleanedUp($modelClass, $numberOfDeletedRecords)); |
||
98 | |||
99 | $this->info("Deleted {$numberOfDeletedRecords} record(s) from {$modelClass}."); |
||
100 | |||
101 | }); |
||
102 | } |
||
103 | |||
104 | protected function getAllModelsFromEachDirectory(array $directories) : Collection |
||
112 | |||
113 | protected function getClassNamesInDirectory(string $directory) : Collection |
||
114 | { |
||
115 | $files = config('model-cleanup.recursive', true) |
||
116 | ? $this->filesystem->allFiles($directory) |
||
117 | : $this->filesystem->files($directory); |
||
118 | |||
119 | return collect($files)->map(function (string $path) { |
||
120 | |||
121 | return $this->getFullyQualifiedClassNameFromFile($path); |
||
122 | |||
123 | })->filter(function (string $className) { |
||
124 | |||
125 | return !empty($className); |
||
126 | |||
127 | }); |
||
128 | } |
||
129 | |||
130 | protected function getFullyQualifiedClassNameFromFile(string $path) : string |
||
153 | } |
||
154 |
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.