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 |
||
14 | class ScriptCommand extends AbstractMagentoCommand |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $scriptVars = array(); |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $_scriptFilename = ''; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $_stopOnError = false; |
||
30 | |||
31 | View Code Duplication | protected function configure() |
|
|
|||
32 | { |
||
33 | $this |
||
34 | ->setName('script') |
||
35 | ->addArgument('filename', InputArgument::OPTIONAL, 'Script file') |
||
36 | ->addOption('define', 'd', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Defines a variable') |
||
37 | ->addOption('stop-on-error', null, InputOption::VALUE_NONE, 'Stops execution of script on error') |
||
38 | ->setDescription('Runs multiple n98-magerun commands') |
||
39 | ; |
||
40 | |||
41 | $help = <<<HELP |
||
42 | Example: |
||
43 | |||
44 | # Set multiple config |
||
45 | config:set "web/cookie/cookie_domain" example.com |
||
46 | |||
47 | # Set with multiline values with "\n" |
||
48 | config:set "general/store_information/address" "First line\nSecond line\nThird line" |
||
49 | |||
50 | # This is a comment |
||
51 | cache:flush |
||
52 | |||
53 | |||
54 | Optionally you can work with unix pipes. |
||
55 | |||
56 | \$ echo "cache:flush" | n98-magerun-dev script |
||
57 | |||
58 | \$ n98-magerun.phar script < filename |
||
59 | |||
60 | It is even possible to create executable scripts: |
||
61 | |||
62 | Create file `test.magerun` and make it executable (`chmod +x test.magerun`): |
||
63 | |||
64 | #!/usr/bin/env n98-magerun.phar script |
||
65 | |||
66 | config:set "web/cookie/cookie_domain" example.com |
||
67 | cache:flush |
||
68 | |||
69 | # Run a shell script with "!" as first char |
||
70 | ! ls -l |
||
71 | |||
72 | # Register your own variable (only key = value currently supported) |
||
73 | \${my.var}=bar |
||
74 | |||
75 | # Let magerun ask for variable value - add a question mark |
||
76 | \${my.var}=? |
||
77 | |||
78 | ! echo \${my.var} |
||
79 | |||
80 | # Use resolved variables from n98-magerun in shell commands |
||
81 | ! ls -l \${magento.root}/code/local |
||
82 | |||
83 | Pre-defined variables: |
||
84 | |||
85 | * \${magento.root} -> Magento Root-Folder |
||
86 | * \${magento.version} -> Magento Version i.e. 1.7.0.2 |
||
87 | * \${magento.edition} -> Magento Edition -> Community or Enterprise |
||
88 | * \${magerun.version} -> Magerun version i.e. 1.66.0 |
||
89 | * \${php.version} -> PHP Version |
||
90 | * \${script.file} -> Current script file path |
||
91 | * \${script.dir} -> Current script file dir |
||
92 | |||
93 | Variables can be passed to a script with "--define (-d)" option. |
||
94 | |||
95 | Example: |
||
96 | |||
97 | $ n98-magerun.phar script -d foo=bar filename |
||
98 | |||
99 | # This will register the variable \${foo} with value bar. |
||
100 | |||
101 | It's possible to define multiple values by passing more than one option. |
||
102 | HELP; |
||
103 | $this->setHelp($help); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function isEnabled() |
||
113 | |||
114 | protected function execute(InputInterface $input, OutputInterface $output) |
||
115 | { |
||
116 | $this->_scriptFilename = $input->getArgument('filename'); |
||
117 | $this->_stopOnError = $input->getOption('stop-on-error'); |
||
118 | $this->_initDefines($input); |
||
119 | $script = $this->_getContent($this->_scriptFilename); |
||
120 | $commands = explode("\n", $script); |
||
121 | $this->initScriptVars(); |
||
122 | |||
123 | foreach ($commands as $commandString) { |
||
124 | $commandString = trim($commandString); |
||
125 | if (empty($commandString)) { |
||
126 | continue; |
||
127 | } |
||
128 | $firstChar = substr($commandString, 0, 1); |
||
129 | |||
130 | switch ($firstChar) { |
||
131 | |||
132 | // comment |
||
133 | case '#': |
||
134 | continue; |
||
135 | break; |
||
136 | |||
137 | // set var |
||
138 | case '$': |
||
139 | $this->registerVariable($output, $commandString); |
||
140 | break; |
||
141 | |||
142 | // run shell script |
||
143 | case '!': |
||
144 | $this->runShellCommand($output, $commandString); |
||
145 | break; |
||
146 | |||
147 | default: |
||
148 | $this->runMagerunCommand($input, $output, $commandString); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param InputInterface $input |
||
155 | * @throws \InvalidArgumentException |
||
156 | */ |
||
157 | protected function _initDefines(InputInterface $input) |
||
158 | { |
||
159 | $defines = $input->getOption('define'); |
||
160 | if (is_string($defines)) { |
||
161 | $defines = array($defines); |
||
162 | } |
||
163 | if (count($defines) > 0) { |
||
164 | foreach ($defines as $define) { |
||
165 | if (!strstr($define, '=')) { |
||
166 | throw new \InvalidArgumentException('Invalid define'); |
||
167 | } |
||
168 | $parts = BinaryString::trimExplodeEmpty('=', $define); |
||
169 | $variable = $parts[0]; |
||
170 | $value = null; |
||
171 | if (isset($parts[1])) { |
||
172 | $value = $parts[1]; |
||
173 | } |
||
174 | $this->scriptVars['${' . $variable. '}'] = $value; |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param string $filename |
||
181 | * @throws \RuntimeException |
||
182 | * @internal param string $input |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function _getContent($filename) |
||
199 | |||
200 | /** |
||
201 | * @param OutputInterface $output |
||
202 | * @param string $commandString |
||
203 | * @throws \RuntimeException |
||
204 | * @return void |
||
205 | */ |
||
206 | protected function registerVariable(OutputInterface $output, $commandString) |
||
252 | |||
253 | /** |
||
254 | * @param InputInterface $input |
||
255 | * @param OutputInterface $output |
||
256 | * @param $commandString |
||
257 | * @throws \RuntimeException |
||
258 | */ |
||
259 | protected function runMagerunCommand(InputInterface $input, OutputInterface $output, $commandString) |
||
269 | |||
270 | /** |
||
271 | * @param string $commandString |
||
272 | * @return mixed|string |
||
273 | */ |
||
274 | protected function _prepareShellCommand($commandString) |
||
290 | |||
291 | protected function initScriptVars() |
||
305 | |||
306 | /** |
||
307 | * @param OutputInterface $output |
||
308 | * @param string $commandString |
||
309 | * @internal param $returnValue |
||
310 | */ |
||
311 | protected function runShellCommand(OutputInterface $output, $commandString) |
||
319 | |||
320 | /** |
||
321 | * @param $commandString |
||
322 | * @return mixed |
||
323 | */ |
||
324 | protected function _replaceScriptVars($commandString) |
||
330 | } |
||
331 |
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.