| Conditions | 15 |
| Paths | 667 |
| Total Lines | 75 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 59 | public function performAction() |
||
| 60 | { |
||
| 61 | try { |
||
| 62 | $skipRoot = false; |
||
| 63 | $baseName = null; |
||
| 64 | $schema = $this->loadSchema($skipRoot, $baseName); |
||
| 65 | |||
| 66 | $builderOptions = $this->makeGoBuilderOptions(); |
||
| 67 | $builder = new GoBuilder(); |
||
| 68 | $builder->options = $builderOptions; |
||
| 69 | |||
| 70 | $pathToNameHook = new StripPrefixPathToNameHook(); |
||
| 71 | |||
| 72 | if (!empty($this->defPtr)) { |
||
| 73 | $pathToNameHook->prefixes = []; |
||
| 74 | foreach ($this->defPtr as $defPtr) { |
||
| 75 | if (isset($baseName)) { |
||
| 76 | $pathToNameHook->prefixes[] = $baseName . $defPtr; |
||
| 77 | } |
||
| 78 | $pathToNameHook->prefixes[] = $defPtr; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if (isset($baseName)) { |
||
| 83 | $pathToNameHook->prefixes[] = $baseName; |
||
| 84 | } |
||
| 85 | |||
| 86 | $builder->pathToNameHook = $pathToNameHook; |
||
| 87 | |||
| 88 | $builder->structCreatedHook = new StructHookCallback(function (StructDef $structDef, $path, $schema) { |
||
| 89 | if ('#' === $path) { |
||
| 90 | $structDef->setName($this->rootName); |
||
| 91 | } |
||
| 92 | }); |
||
| 93 | if ($schema instanceof Schema) { |
||
| 94 | $builder->getType($schema); |
||
| 95 | } |
||
| 96 | |||
| 97 | $goFile = new GoFile($this->packageName); |
||
| 98 | $goFile->fileComment = 'Code generated by github.com/swaggest/json-cli ' . App::$ver . ', DO NOT EDIT.'; |
||
| 99 | $goFile->setComment('Package ' . $this->packageName . ' contains JSON mapping structures.'); |
||
| 100 | |||
| 101 | $goTestFile = new GoFile($this->packageName . '_test'); |
||
| 102 | $goTestFile->setPackage($this->packageName); |
||
| 103 | $goTestFile->fileComment = 'Code generated by github.com/swaggest/json-cli ' . App::$ver . ', DO NOT EDIT.'; |
||
| 104 | |||
| 105 | mt_srand(1); |
||
| 106 | |||
| 107 | foreach ($builder->getGeneratedStructs() as $generatedStruct) { |
||
| 108 | if ($skipRoot && $generatedStruct->path === '#') { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | $goFile->getCode()->addSnippet($generatedStruct->structDef); |
||
| 112 | if ($this->withTests) { |
||
| 113 | $goTestFile->getCode()->addSnippet(MarshalingTestFunc::make($generatedStruct, $builder->options)); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | $goFile->getCode()->addSnippet($builder->getCode()); |
||
| 117 | |||
| 118 | if ($this->output) { |
||
| 119 | if (!file_exists(dirname($this->output))) { |
||
| 120 | $this->response->error('Destination directory does not exist, please create: ' . dirname($this->output)); |
||
| 121 | throw new ExitCode('', 1); |
||
| 122 | } |
||
| 123 | file_put_contents($this->output, $goFile->render()); |
||
| 124 | |||
| 125 | if ($this->withTests) { |
||
| 126 | file_put_contents(str_replace('.go', '_test.go', $this->output), $goTestFile->render()); |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | echo $goFile->render(); |
||
| 130 | } |
||
| 131 | } catch (\Exception $e) { |
||
| 132 | $this->response->error($e->getMessage()); |
||
| 133 | throw new ExitCode('', 1); |
||
| 134 | } |
||
| 136 | } |