Conditions | 3 |
Paths | 4 |
Total Lines | 52 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
134 | private function generateModel($modelName) { |
||
135 | $this->logger->info('Generate Serializer from Model: ' . $modelName); |
||
136 | $model = $this->modelService->getModel($modelName); |
||
137 | |||
138 | // trait |
||
139 | $generator = new ModelSerializerTraitGenerator($this->service); |
||
140 | $trait = $generator->generate($model); |
||
1 ignored issue
–
show
|
|||
141 | $this->codegenService->dumpStruct($trait, true); |
||
142 | |||
143 | // class |
||
144 | $serializer = new PhpClass(sprintf('%s\\serializer\\%sSerializer', $this->packageService->getNamespace(), $model->getPhpName())); |
||
145 | $file = new File($this->codegenService->getFilename($serializer)); |
||
146 | |||
147 | // load from file if already exists |
||
148 | if ($file->exists()) { |
||
149 | $serializer = PhpClass::fromFile($file->getPathname()); |
||
150 | } |
||
151 | |||
152 | // generate stub if not |
||
153 | else { |
||
154 | $serializer->setParentClassName('AbstractSerializer'); |
||
155 | $serializer->addUseStatement('keeko\\framework\\model\\AbstractSerializer'); |
||
156 | } |
||
157 | |||
158 | // add serializer trait and write |
||
159 | $serializer->addTrait($trait); |
||
160 | $this->codegenService->dumpStruct($serializer, true); |
||
161 | |||
162 | // add serializer + APIModelInterface on the model |
||
163 | $class = new PhpClass(str_replace('\\\\', '\\', $model->getNamespace() . '\\' . $model->getPhpName())); |
||
164 | $file = new File($this->codegenService->getFilename($class)); |
||
165 | if ($file->exists()) { |
||
166 | $class = PhpClass::fromFile($this->codegenService->getFilename($class)); |
||
167 | $class |
||
168 | ->addUseStatement($serializer->getQualifiedName()) |
||
169 | ->addUseStatement('keeko\\framework\\model\\ApiModelInterface') |
||
170 | ->addInterface('ApiModelInterface') |
||
171 | ->setProperty(PhpProperty::create('serializer') |
||
172 | ->setStatic(true) |
||
173 | ->setVisibility('private') |
||
174 | ) |
||
175 | ->setMethod(PhpMethod::create('getSerializer') |
||
176 | ->setStatic(true) |
||
177 | ->setBody($this->twig->render('get-serializer.twig', [ |
||
178 | 'class' => $serializer->getName() |
||
179 | ])) |
||
180 | ) |
||
181 | ; |
||
182 | |||
183 | $this->codegenService->dumpStruct($class, true); |
||
184 | } |
||
185 | } |
||
186 | |||
201 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: