| Conditions | 9 |
| Paths | 15 |
| Total Lines | 140 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 2 | 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 |
||
| 143 | protected function generateRelationshipMethods(PhpTrait $trait, Table $model) { |
||
| 144 | if ($model->isReadOnly()) { |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | $fields = []; |
||
| 149 | $methods = []; |
||
| 150 | $plural = []; |
||
| 151 | $relationships = $this->modelService->getRelationships($model); |
||
| 152 | |||
| 153 | if ($relationships->size() > 0) { |
||
| 154 | $trait->addUseStatement('Tobscure\\JsonApi\\Relationship'); |
||
| 155 | $trait->setMethod(PhpMethod::create('addRelationshipSelfLink') |
||
| 156 | ->addParameter(PhpParameter::create('relationship') |
||
| 157 | ->setType('Relationship') |
||
| 158 | ) |
||
| 159 | ->addParameter(PhpParameter::create('model') |
||
| 160 | ->setType('mixed') |
||
| 161 | ) |
||
| 162 | ->addParameter(PhpParameter::create('related') |
||
| 163 | ->setType('string') |
||
| 164 | ) |
||
| 165 | ->setAbstract(true) |
||
| 166 | ->setVisibility(PhpMethod::VISIBILITY_PROTECTED) |
||
| 167 | ->setType('Relationship') |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | foreach ($relationships->getAll() as $rel) { |
||
| 172 | // one-to-one |
||
| 173 | if ($rel->getType() == Relationship::ONE_TO_ONE) { |
||
| 174 | $foreign = $rel->getForeign(); |
||
| 175 | $relatedName = $rel->getRelatedName(); |
||
| 176 | $typeName = $rel->getRelatedTypeName(); |
||
| 177 | $method = NameUtils::toCamelCase($relatedName); |
||
| 178 | $fields[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
| 179 | $trait->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
| 180 | $trait->addUseStatement('Tobscure\\JsonApi\\Resource'); |
||
| 181 | |||
| 182 | // read |
||
| 183 | $body = $this->twig->render('to-one-read.twig', [ |
||
| 184 | 'class' => $foreign->getPhpName(), |
||
| 185 | 'related' => $relatedName, |
||
| 186 | 'related_type' => $typeName |
||
| 187 | ]); |
||
| 188 | } |
||
| 189 | |||
| 190 | // ?-to-many |
||
| 191 | else { |
||
| 192 | $foreign = $rel->getForeign(); |
||
| 193 | $typeName = $rel->getRelatedPluralTypeName(); |
||
| 194 | $method = NameUtils::toCamelCase($rel->getRelatedPluralName()); |
||
| 195 | $fields[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
| 196 | $trait->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
| 197 | $trait->addUseStatement('Tobscure\\JsonApi\\Collection'); |
||
| 198 | |||
| 199 | // read |
||
| 200 | $body = $this->twig->render('to-many-read.twig', [ |
||
| 201 | 'class' => $foreign->getPhpName(), |
||
| 202 | 'related' => $typeName, |
||
| 203 | 'related_type' => $rel->getRelatedTypeName() |
||
| 204 | ]); |
||
| 205 | |||
| 206 | // method name for collection |
||
| 207 | $methods[$typeName] = NameUtils::toStudlyCase(NameUtils::singularize($typeName)); |
||
| 208 | $plural[$typeName] = NameUtils::pluralize(NameUtils::toStudlyCase(NameUtils::singularize($typeName))); |
||
| 209 | if ($rel->getType() == Relationship::MANY_TO_MANY |
||
| 210 | && $rel->getForeign() == $rel->getModel()) { |
||
| 211 | $lk = $rel->getLocalKey(); |
||
| 212 | $methods[$typeName] = $foreign->getPhpName() . 'RelatedBy' . $lk->getLocalColumn()->getPhpName(); |
||
| 213 | $plural[$typeName] = NameUtils::pluralize($foreign->getPhpName()) . 'RelatedBy' . $lk->getLocalColumn()->getPhpName(); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | // set read method on class |
||
| 218 | $trait->setMethod(PhpMethod::create($method) |
||
| 219 | ->addParameter(PhpParameter::create('model')) |
||
| 220 | ->setBody($body) |
||
| 221 | ->setType('Relationship') |
||
| 222 | ); |
||
| 223 | |||
| 224 | // add reverse many-to-many |
||
| 225 | if ($rel->getType() == Relationship::MANY_TO_MANY |
||
| 226 | && $rel->getForeign() == $rel->getModel()) { |
||
| 227 | $foreign = $rel->getForeign(); |
||
| 228 | $typeName = $rel->getReverseRelatedPluralTypeName(); |
||
| 229 | $method = NameUtils::toCamelCase($rel->getReverseRelatedPluralName()); |
||
| 230 | $fields[$typeName] = $foreign->getPhpName() . '::getSerializer()->getType(null)'; |
||
| 231 | $trait->addUseStatement($foreign->getNamespace() . '\\' . $foreign->getPhpName()); |
||
| 232 | $trait->addUseStatement('Tobscure\\JsonApi\\Collection'); |
||
| 233 | |||
| 234 | // read |
||
| 235 | $body = $this->twig->render('to-many-read.twig', [ |
||
| 236 | 'class' => $foreign->getPhpName(), |
||
| 237 | 'related' => $typeName, |
||
| 238 | 'related_type' => $rel->getReverseRelatedTypeName() |
||
| 239 | ]); |
||
| 240 | |||
| 241 | $fk = $rel->getForeignKey(); |
||
| 242 | $methods[$typeName] = $foreign->getPhpName() . 'RelatedBy' . $fk->getLocalColumn()->getPhpName(); |
||
| 243 | $plural[$typeName] = NameUtils::pluralize($foreign->getPhpName()) . 'RelatedBy' . $fk->getLocalColumn()->getPhpName(); |
||
| 244 | |||
| 245 | // set read method on class |
||
| 246 | $trait->setMethod(PhpMethod::create($method) |
||
| 247 | ->addParameter(PhpParameter::create('model')) |
||
| 248 | ->setBody($body) |
||
| 249 | ->setType('Relationship') |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | // method: getRelationships() : array |
||
| 255 | $trait->setMethod(PhpMethod::create('getRelationships') |
||
| 256 | ->setBody($this->twig->render('getRelationships.twig', [ |
||
| 257 | 'fields' => $fields |
||
| 258 | ])) |
||
| 259 | ); |
||
| 260 | |||
| 261 | // method: getCollectionMethodName($relatedName) : string |
||
| 262 | $trait->setProperty(PhpProperty::create('methodNames') |
||
| 263 | ->setExpression($this->codegenService->mapToCode($methods)) |
||
| 264 | ->setVisibility(PhpProperty::VISIBILITY_PRIVATE) |
||
| 265 | ); |
||
| 266 | $trait->setMethod(PhpMethod::create('getCollectionMethodName') |
||
| 267 | ->addParameter(PhpParameter::create('relatedName')) |
||
| 268 | ->setBody($this->twig->render('getCollectionMethodName.twig')) |
||
| 269 | ->setVisibility(PhpMethod::VISIBILITY_PROTECTED) |
||
| 270 | ); |
||
| 271 | |||
| 272 | // method: getCollectionMethodPluralName($relatedName) : string |
||
| 273 | $trait->setProperty(PhpProperty::create('methodPluralNames') |
||
| 274 | ->setExpression($this->codegenService->mapToCode($plural)) |
||
| 275 | ->setVisibility(PhpProperty::VISIBILITY_PRIVATE) |
||
| 276 | ); |
||
| 277 | $trait->setMethod(PhpMethod::create('getCollectionMethodPluralName') |
||
| 278 | ->addParameter(PhpParameter::create('relatedName')) |
||
| 279 | ->setBody($this->twig->render('getCollectionMethodPluralName.twig')) |
||
| 280 | ->setVisibility(PhpMethod::VISIBILITY_PROTECTED) |
||
| 281 | ); |
||
| 282 | } |
||
| 283 | |||
| 292 | } |
It seems like you are relying on a variable being defined by an iteration: