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 |
||
| 8 | class ServiceProviderRegister extends Plugin |
||
| 9 | { |
||
| 10 | 3 | public function process() |
|
| 11 | { |
||
| 12 | 3 | $path = Arr::get($this->config, 'path'); |
|
| 13 | |||
| 14 | 3 | if (empty($path) === true) { |
|
| 15 | return; |
||
| 16 | } |
||
| 17 | |||
| 18 | 3 | $content = $this->files->get($path); |
|
| 19 | |||
| 20 | 3 | if (strpos($content, '$this->registerRepositories') === false) { |
|
| 21 | 3 | $content = preg_replace_callback('/public function register\(.+\n\s+{/', function ($m) { |
|
| 22 | 3 | return $m[0]."\n". |
|
| 23 | 3 | str_repeat(' ', 8). |
|
| 24 | 3 | '$this->registerRepositories();'; |
|
| 25 | 3 | }, $content); |
|
| 26 | 3 | } |
|
| 27 | |||
| 28 | 3 | if (strpos($content, 'private function registerRepositories()') === false) { |
|
| 29 | 3 | $content = substr($content, 0, strrpos($content, '}')). |
|
| 30 | 3 | "\n".str_repeat(' ', 4). |
|
| 31 | 3 | 'private function registerRepositories()'. |
|
| 32 | 3 | "\n".str_repeat(' ', 4).'{'. |
|
| 33 | 3 | "\n".str_repeat(' ', 4).'}'. |
|
| 34 | 3 | "\n}\n"; |
|
| 35 | 3 | } |
|
| 36 | |||
| 37 | 3 | $qualifiedName = $this->attributes['qualified_class']; |
|
| 38 | 3 | $class = $this->attributes['class']; |
|
| 39 | |||
| 40 | 3 | View Code Duplication | if ($qualifiedName && strpos($content, sprintf('use %s;', $qualifiedName)) === false) { |
|
|
|||
| 41 | 3 | $content = preg_replace_callback( |
|
| 42 | 3 | '/namespace.+/', |
|
| 43 | 3 | [$this, 'replaceServieProviderCallback'], |
|
| 44 | $content |
||
| 45 | 3 | ); |
|
| 46 | 3 | } |
|
| 47 | |||
| 48 | 3 | View Code Duplication | if ($class && strpos($content, $this->singletonString($class)) === false) { |
| 49 | 3 | $content = preg_replace_callback( |
|
| 50 | 3 | '/protected function registerRepositories.+\n\s+{/', |
|
| 51 | 3 | [$this, 'replaceServieProviderCallback'], |
|
| 52 | $content |
||
| 53 | 3 | ); |
|
| 54 | 3 | } |
|
| 55 | |||
| 56 | 3 | $fixedContent = $this->useSortFixer->fix($content); |
|
| 57 | |||
| 58 | 3 | $this->files->put( |
|
| 59 | 3 | $path, |
|
| 60 | 3 | $fixedContent ? $fixedContent : $content |
|
| 61 | 3 | ); |
|
| 62 | 3 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * replaceServieProviderCallback. |
||
| 66 | * |
||
| 67 | * @param array $match |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | 3 | private function replaceServieProviderCallback($match) |
|
| 84 | |||
| 85 | 3 | private function singletonString($class) |
|
| 89 | } |
||
| 90 |
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.