Conditions | 5 |
Paths | 7 |
Total Lines | 76 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | 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 |
||
141 | protected function generateRelationshipMethods(PhpTrait $class, Table $model) { |
||
142 | if ($model->isReadOnly()) { |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | $fields = []; |
||
147 | $relationships = $this->modelService->getRelationships($model); |
||
148 | |||
149 | if ($relationships->size() > 0) { |
||
150 | $class->addUseStatement('Tobscure\\JsonApi\\Relationship'); |
||
151 | $class->setMethod(PhpMethod::create('addRelationshipSelfLink') |
||
152 | ->addParameter(PhpParameter::create('relationship') |
||
153 | ->setType('Relationship') |
||
154 | ) |
||
155 | ->addParameter(PhpParameter::create('model') |
||
156 | ->setType('mixed') |
||
157 | ) |
||
158 | ->addParameter(PhpParameter::create('related') |
||
159 | ->setType('string') |
||
160 | ) |
||
161 | ->setAbstract(true) |
||
162 | ->setVisibility(PhpMethod::VISIBILITY_PROTECTED) |
||
163 | ->setType('Relationship') |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | foreach ($relationships->getAll() as $rel) { |
||
168 | // one-to-one |
||
169 | if ($rel->getType() == Relationship::ONE_TO_ONE) { |
||
170 | $foreign = $rel->getForeign(); |
||
171 | $relatedName = $rel->getRelatedName(); |
||
172 | $typeName = $rel->getRelatedTypeName(); |
||
173 | $method = NameUtils::toCamelCase($relatedName); |
||
174 | $fields[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
175 | $class->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
176 | $class->addUseStatement('Tobscure\\JsonApi\\Resource'); |
||
177 | |||
178 | // read |
||
179 | $body = $this->twig->render('to-one-read.twig', [ |
||
180 | 'class' => $foreign->getPhpName(), |
||
181 | 'related' => $relatedName, |
||
182 | 'related_type' => $typeName |
||
183 | ]); |
||
184 | } |
||
185 | |||
186 | // ?-to-many |
||
187 | else { |
||
188 | $foreign = $rel->getForeign(); |
||
189 | $typeName = $rel->getRelatedPluralTypeName(); |
||
190 | $method = NameUtils::toCamelCase($rel->getRelatedPluralName()); |
||
191 | $fields[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
192 | $class->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
193 | $class->addUseStatement('Tobscure\\JsonApi\\Collection'); |
||
194 | |||
195 | // read |
||
196 | $body = $this->twig->render('to-many-read.twig', [ |
||
197 | 'class' => $foreign->getPhpName(), |
||
198 | 'related' => $rel->getRelatedPluralName(), |
||
199 | 'related_type' => $rel->getRelatedTypeName() |
||
200 | ]); |
||
201 | } |
||
202 | |||
203 | // set read method on class |
||
204 | $class->setMethod(PhpMethod::create($method) |
||
205 | ->addParameter(PhpParameter::create('model')) |
||
206 | ->setBody($body) |
||
207 | ->setType('Relationship') |
||
208 | ); |
||
209 | } |
||
210 | |||
211 | $class->setMethod(PhpMethod::create('getRelationships') |
||
212 | ->setBody($this->twig->render('getRelationships.twig', [ |
||
213 | 'fields' => $fields |
||
214 | ])) |
||
215 | ); |
||
216 | } |
||
217 | } |
It seems like you are relying on a variable being defined by an iteration: