Conditions | 12 |
Paths | 42 |
Total Lines | 135 |
Code Lines | 40 |
Lines | 135 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
92 | public function processCommand(CliEngine $engine, $params = array(), $additionalContext = null) |
||
93 | { |
||
94 | // do we have the name of the file to create? |
||
95 | if (!isset($params[0])) { |
||
96 | echo "*** error: you must specify which Env.php file to create\n"; |
||
97 | exit(1); |
||
98 | } |
||
99 | |||
100 | // we're going to be dealing with some prehistoric parts of PHP |
||
101 | $legacyHandler = new Legacy_ErrorHandler(); |
||
102 | |||
103 | // create the path to the environment |
||
104 | $storyFolder = dirname($params[0]); |
||
105 | if (!file_exists($storyFolder)) { |
||
106 | try { |
||
107 | $legacyHandler->run(function() use ($storyFolder) { |
||
108 | mkdir($storyFolder, 0755, true); |
||
109 | }); |
||
110 | } |
||
111 | catch (Exception $e) { |
||
112 | echo "*** error: unable to create folder '{$storyFolder}'\n"; |
||
113 | exit(1); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | // create the environment inside the folder |
||
118 | $env = <<<EOS |
||
119 | <?php |
||
120 | |||
121 | use Storyplayer\SPv3\Modules\Asserts; |
||
122 | use Storyplayer\SPv3\Modules\Checkpoint; |
||
123 | use Storyplayer\SPv3\Modules\Log; |
||
124 | use Storyplayer\SPv3\Stories\BuildTestEnvironment; |
||
125 | |||
126 | EOS; |
||
127 | |||
128 | if (isset($engine->options->basedOn)) { |
||
129 | foreach ($engine->options->basedOn as $templateClass) { |
||
130 | $story .= "use {$templateClass};\n"; |
||
131 | } |
||
132 | } |
||
133 | $env .= <<<EOS |
||
134 | |||
135 | // ======================================================================== |
||
136 | // |
||
137 | // TEST ENVIRONMENT DETAILS |
||
138 | // |
||
139 | // ------------------------------------------------------------------------ |
||
140 | |||
141 | \$env = BuildTestEnvironment::newTestEnvironment(); |
||
142 | EOS; |
||
143 | |||
144 | if (isset($engine->options->basedOn)) { |
||
145 | foreach ($engine->options->basedOn as $templateClass) { |
||
146 | $story .= "\n\$env->basedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ");"; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | $env .= <<<EOS |
||
151 | |||
152 | |||
153 | // ======================================================================== |
||
154 | // |
||
155 | // TEST ENVIRONMENT SETUP |
||
156 | // |
||
157 | // Add one function per step. This makes it easier to debug and maintain |
||
158 | // your test environment construction. |
||
159 | // |
||
160 | // ------------------------------------------------------------------------ |
||
161 | |||
162 | \$env->addTestEnvironmentSetup(function() { |
||
163 | // what are we doing? |
||
164 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
165 | |||
166 | // add the instructions required to build the environment |
||
167 | |||
168 | // all done |
||
169 | \$log->endAction(); |
||
170 | }); |
||
171 | |||
172 | // ======================================================================== |
||
173 | // |
||
174 | // TEST ENVIRONMENT TEARDOWN |
||
175 | // |
||
176 | // Add one function per step. This makes it easier to debug and maintain |
||
177 | // your test environment cleanup. |
||
178 | // |
||
179 | // ------------------------------------------------------------------------ |
||
180 | |||
181 | \$env->addTestEnvironmentTeardown(function() { |
||
182 | // what are we doing? |
||
183 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
184 | |||
185 | // undo anything that you did in addTestEnvironmentSetup() |
||
186 | |||
187 | // all done |
||
188 | \$log->endAction(); |
||
189 | }); |
||
190 | |||
191 | // ======================================================================== |
||
192 | // |
||
193 | // ALL DONE |
||
194 | // |
||
195 | // Return your constructed test environment object, for Storyplayer |
||
196 | // to execute. |
||
197 | // |
||
198 | // ------------------------------------------------------------------------ |
||
199 | |||
200 | return \$env; |
||
201 | |||
202 | EOS; |
||
203 | |||
204 | // does the file already exist? |
||
205 | if (file_exists($params[0])) { |
||
206 | // has the user used --force? |
||
207 | if (!isset($engine->options->force) || !$engine->options->force) { |
||
208 | echo "*** error: file '{$params[0]}' already exists\n"; |
||
209 | echo "use --force to replace this file with the new Env.php file\n"; |
||
210 | exit(1); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | try { |
||
215 | $legacyHandler->run(function() use($params, $env) { |
||
216 | file_put_contents($params[0], $env); |
||
217 | }); |
||
218 | } |
||
219 | catch (Exception $e) { |
||
220 | echo "*** error: " . $e->getMessage() . "\n"; |
||
221 | exit(1); |
||
222 | } |
||
223 | |||
224 | // all done |
||
225 | return 0; |
||
226 | } |
||
227 | } |
||
228 |
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.