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 |
||
22 | class SetupCommand extends AbstractCommand |
||
23 | { |
||
24 | /** |
||
25 | * The console command name. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $name = 'aimeos:setup'; |
||
30 | |||
31 | /** |
||
32 | * The console command description. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $description = 'Initialize or update the Aimeos database tables'; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Execute the console command. |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function handle() |
||
45 | { |
||
46 | $ctx = $this->getLaravel()->make( '\Aimeos\Shop\Base\Context' )->get( false, 'command' ); |
||
47 | $ctx->setEditor( 'aimeos:setup' ); |
||
48 | |||
49 | $config = $ctx->getConfig(); |
||
50 | $site = $this->argument( 'site' ); |
||
51 | $template = $this->argument( 'tplsite' ); |
||
52 | |||
53 | $config->set( 'setup/site', $site ); |
||
54 | $dbconfig = $this->getDbConfig( $config ); |
||
55 | $this->setOptions( $config ); |
||
56 | |||
57 | $taskPaths = $this->getLaravel()->make( '\Aimeos\Shop\Base\Aimeos' )->get()->getSetupPaths( $template ); |
||
58 | $manager = new \Aimeos\MW\Setup\Manager\Multiple( $ctx->getDatabaseManager(), $dbconfig, $taskPaths, $ctx ); |
||
59 | |||
60 | $this->info( sprintf( 'Initializing or updating the Aimeos database tables for site "%1$s"', $site ) ); |
||
61 | |||
62 | if( ( $task = $this->option( 'task' ) ) && is_array( $task ) ) { |
||
63 | $task = reset( $task ); |
||
64 | } |
||
65 | |||
66 | switch( $this->option( 'action' ) ) |
||
67 | { |
||
68 | case 'migrate': |
||
69 | $manager->migrate( $task ); |
||
70 | break; |
||
71 | case 'rollback': |
||
72 | $manager->rollback( $task ); |
||
73 | break; |
||
74 | case 'clean': |
||
75 | $manager->clean( $task ); |
||
76 | break; |
||
77 | default: |
||
78 | throw new \Exception( sprintf( 'Invalid setup action "%1$s"', $this->option( 'action' ) ) ); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | |||
83 | /** |
||
84 | * Get the console command arguments. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | View Code Duplication | protected function getArguments() |
|
|
|||
89 | { |
||
90 | return array( |
||
91 | array( 'site', InputArgument::OPTIONAL, 'Site for updating database entries', 'default' ), |
||
92 | array( 'tplsite', InputArgument::OPTIONAL, 'Site used as template for creating the new one', 'default' ), |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Get the console command options. |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | protected function getOptions() |
||
103 | { |
||
104 | return array( |
||
105 | array( 'option', null, InputOption::VALUE_REQUIRED, 'Setup configuration, name and value are separated by ":" like "setup/default/demo:1"', array() ), |
||
106 | array( 'action', null, InputOption::VALUE_REQUIRED, 'Action name that should be executed, i.e. "migrate", "rollback", "clean"', 'migrate' ), |
||
107 | array( 'task', null, InputOption::VALUE_REQUIRED, 'Name of the setup task that should be executed', null ), |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Returns the database configuration from the config object. |
||
114 | * |
||
115 | * @param \Aimeos\MW\Config\Iface $conf Config object |
||
116 | * @return array Multi-dimensional associative list of database configuration parameters |
||
117 | */ |
||
118 | protected function getDbConfig( \Aimeos\MW\Config\Iface $conf ) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Extracts the configuration options from the input object and updates the configuration values in the config object. |
||
135 | * |
||
136 | * @param \Aimeos\MW\Config\Iface $conf Configuration object |
||
137 | * @param array Associative list of database configurations |
||
138 | * @throws \RuntimeException If the format of the options is invalid |
||
139 | */ |
||
140 | protected function setOptions( \Aimeos\MW\Config\Iface $conf ) |
||
148 | } |
||
149 |
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.