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