| Conditions | 12 |
| Paths | 42 |
| Total Lines | 200 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 91 | public function processCommand(CliEngine $engine, $params = array(), $additionalContext = null) |
||
| 92 | { |
||
| 93 | // do we have the name of the file to create? |
||
| 94 | if (!isset($params[0])) { |
||
| 95 | echo "*** error: you must specify which story to create\n"; |
||
| 96 | exit(1); |
||
| 97 | } |
||
| 98 | |||
| 99 | // we're going to be dealing with some prehistoric parts of PHP |
||
| 100 | $legacyHandler = new Legacy_ErrorHandler(); |
||
| 101 | |||
| 102 | // create the path to the story |
||
| 103 | $storyFolder = dirname($params[0]); |
||
| 104 | if (!file_exists($storyFolder)) { |
||
| 105 | try { |
||
| 106 | $legacyHandler->run(function() use ($storyFolder) { |
||
| 107 | mkdir($storyFolder, 0755, true); |
||
| 108 | }); |
||
| 109 | } |
||
| 110 | catch (Exception $e) { |
||
| 111 | echo "*** error: unable to create folder '{$storyFolder}'\n"; |
||
| 112 | exit(1); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | // create the story inside the folder |
||
| 117 | $story = <<<EOS |
||
| 118 | <?php |
||
| 119 | |||
| 120 | use Storyplayer\SPv2\Modules\Asserts; |
||
| 121 | use Storyplayer\SPv2\Modules\Checkpoint; |
||
| 122 | use Storyplayer\SPv2\Modules\Log; |
||
| 123 | use Storyplayer\SPv2\Stories\BuildStory; |
||
| 124 | |||
| 125 | EOS; |
||
| 126 | |||
| 127 | if (isset($engine->options->basedOn)) { |
||
| 128 | foreach ($engine->options->basedOn as $templateClass) { |
||
| 129 | $story .= "use {$templateClass};\n"; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | $story .= <<<EOS |
||
| 133 | |||
| 134 | // ======================================================================== |
||
| 135 | // |
||
| 136 | // STORY DETAILS |
||
| 137 | // |
||
| 138 | // ------------------------------------------------------------------------ |
||
| 139 | |||
| 140 | \$story = BuildStory::newStory(); |
||
| 141 | EOS; |
||
| 142 | |||
| 143 | if (isset($engine->options->basedOn)) { |
||
| 144 | foreach ($engine->options->basedOn as $templateClass) { |
||
| 145 | $story .= "\n\$story->basedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ");"; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $story .= <<<EOS |
||
| 150 | |||
| 151 | |||
| 152 | // ======================================================================== |
||
| 153 | // |
||
| 154 | // TEST SETUP / TEAR-DOWN |
||
| 155 | // |
||
| 156 | // ------------------------------------------------------------------------ |
||
| 157 | |||
| 158 | /* |
||
| 159 | \$story->addTestSetup(function() { |
||
| 160 | // what are we doing? |
||
| 161 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
| 162 | |||
| 163 | // setup the conditions for this specific test |
||
| 164 | \$checkpoint = Checkpoint::getCheckpoint(); |
||
| 165 | |||
| 166 | // all done |
||
| 167 | \$log->endAction(); |
||
| 168 | }); |
||
| 169 | */ |
||
| 170 | |||
| 171 | /* |
||
| 172 | \$story->addTestTeardown(function() { |
||
| 173 | // what are we doing? |
||
| 174 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
| 175 | |||
| 176 | // undo anything that you did in addTestSetup() |
||
| 177 | |||
| 178 | // all done |
||
| 179 | \$log->endAction(); |
||
| 180 | }); |
||
| 181 | */ |
||
| 182 | |||
| 183 | // ======================================================================== |
||
| 184 | // |
||
| 185 | // PRE-TEST PREDICTION |
||
| 186 | // |
||
| 187 | // ------------------------------------------------------------------------ |
||
| 188 | |||
| 189 | /* |
||
| 190 | \$story->addPreTestPrediction(function() { |
||
| 191 | // what are we doing? |
||
| 192 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
| 193 | |||
| 194 | // if it is okay for your story to fail, detect that here |
||
| 195 | |||
| 196 | // all done |
||
| 197 | \$log->endAction(); |
||
| 198 | }); |
||
| 199 | */ |
||
| 200 | |||
| 201 | // ======================================================================== |
||
| 202 | // |
||
| 203 | // PRE-TEST INSPECTION |
||
| 204 | // |
||
| 205 | // ------------------------------------------------------------------------ |
||
| 206 | |||
| 207 | /* |
||
| 208 | \$story->addPreTestInspection(function() { |
||
| 209 | // what are we doing? |
||
| 210 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
| 211 | |||
| 212 | // get the checkpoint - we're going to store data in here |
||
| 213 | \$checkpoint = Checkpoint::getCheckpoint(); |
||
| 214 | |||
| 215 | // store any data that your story is about to change, so that you |
||
| 216 | // can do a before and after comparison |
||
| 217 | |||
| 218 | // all done |
||
| 219 | \$log->endAction(); |
||
| 220 | }); |
||
| 221 | */ |
||
| 222 | |||
| 223 | // ======================================================================== |
||
| 224 | // |
||
| 225 | // ACTIONS |
||
| 226 | // |
||
| 227 | // ------------------------------------------------------------------------ |
||
| 228 | |||
| 229 | /* |
||
| 230 | \$story->addAction(function() { |
||
| 231 | // what are we doing? |
||
| 232 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
| 233 | |||
| 234 | // use the checkpoint to store any data collected during the action |
||
| 235 | // this data will be examined in the postTestInspection phase |
||
| 236 | \$checkpoint = Checkpoint::getCheckpoint(); |
||
| 237 | |||
| 238 | // this is where you perform the steps of your user story |
||
| 239 | |||
| 240 | // all done |
||
| 241 | \$log->endAction(); |
||
| 242 | }); |
||
| 243 | */ |
||
| 244 | |||
| 245 | // ======================================================================== |
||
| 246 | // |
||
| 247 | // POST-TEST INSPECTION |
||
| 248 | // |
||
| 249 | // ------------------------------------------------------------------------ |
||
| 250 | |||
| 251 | \$story->addPostTestInspection(function() { |
||
| 252 | // what are we doing? |
||
| 253 | \$log = Log::usingLog()->startAction("describe what we are doing"); |
||
| 254 | |||
| 255 | // the information to guide our checks is in the checkpoint |
||
| 256 | \$checkpoint = Checkpoint::getCheckpoint(); |
||
| 257 | |||
| 258 | // gather new data, and make sure that your action actually changed |
||
| 259 | // something. never assume that the action worked just because it |
||
| 260 | // completed to the end with no errors or exceptions! |
||
| 261 | |||
| 262 | // all done |
||
| 263 | \$log->endAction(); |
||
| 264 | }); |
||
| 265 | |||
| 266 | EOS; |
||
| 267 | |||
| 268 | // does the file already exist? |
||
| 269 | if (file_exists($params[0])) { |
||
| 270 | // has the user used --force? |
||
| 271 | if (!isset($engine->options->force) || !$engine->options->force) { |
||
| 272 | echo "*** error: file '{$params[0]}' already exists\n"; |
||
| 273 | echo "use --force to replace this file with the new story file\n"; |
||
| 274 | exit(1); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | try { |
||
| 279 | $legacyHandler->run(function() use($params, $story) { |
||
| 280 | file_put_contents($params[0], $story); |
||
| 281 | }); |
||
| 282 | } |
||
| 283 | catch (Exception $e) { |
||
| 284 | echo "*** error: " . $e->getMessage() . "\n"; |
||
| 285 | exit(1); |
||
| 286 | } |
||
| 287 | |||
| 288 | // all done |
||
| 289 | return 0; |
||
| 290 | } |
||
| 291 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.