| Conditions | 7 |
| Paths | 8 |
| Total Lines | 183 |
| Code Lines | 95 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 declare(strict_types=1); |
||
| 346 | protected function makeGraphql(): void |
||
| 347 | { |
||
| 348 | /* |
||
| 349 | * card |
||
| 350 | */ |
||
| 351 | $cardFieldNames = array_map( |
||
| 352 | function (Field $field) { |
||
| 353 | return $field->getName(); |
||
| 354 | }, |
||
| 355 | $this->cardFields |
||
| 356 | ); |
||
| 357 | $graphqlQuery = $this->fModel->mapFields( |
||
| 358 | function (Field $f) use ($cardFieldNames) { |
||
| 359 | if (in_array($f->getName(), $cardFieldNames)) { |
||
| 360 | // TODO: filter subfields in relationships |
||
| 361 | return $f->toGraphqlQuery(); |
||
| 362 | } |
||
| 363 | return null; |
||
| 364 | } |
||
| 365 | ); |
||
| 366 | $cardFieldParameters = join("\n", array_filter($graphqlQuery)); |
||
| 367 | |||
| 368 | // generate filters for query |
||
| 369 | $filters = $this->templateParameters['filters'] ?? []; |
||
| 370 | if ($filters) { |
||
| 371 | $filtersQuery = ', ' . join( |
||
| 372 | ', ', |
||
| 373 | array_map( |
||
| 374 | function ($item) { |
||
| 375 | // TODO: still buggy, misses the internal ! in [Xyz!]! |
||
| 376 | return '$' . $item['graphqlType']; |
||
| 377 | }, |
||
| 378 | $filters |
||
| 379 | ) |
||
| 380 | ); |
||
| 381 | $filtersParams = ', ' . join( |
||
| 382 | ', ', |
||
| 383 | array_map( |
||
| 384 | function ($item) { |
||
| 385 | return $item['name'] . ': $' . $item['name']; |
||
| 386 | }, |
||
| 387 | $filters |
||
| 388 | ) |
||
| 389 | ); |
||
| 390 | } else { |
||
| 391 | $filtersQuery = $filtersParams = ''; |
||
| 392 | } |
||
| 393 | |||
| 394 | $listQuery = <<<EOF |
||
| 395 | query (\$page: Int!$filtersQuery) { |
||
| 396 | {$this->lowerNamePlural}(page: \$page$filtersParams) { |
||
| 397 | data { |
||
| 398 | id |
||
| 399 | $cardFieldParameters |
||
| 400 | } |
||
| 401 | |||
| 402 | paginatorInfo { |
||
| 403 | currentPage |
||
| 404 | perPage |
||
| 405 | total |
||
| 406 | lastPage |
||
| 407 | } |
||
| 408 | } |
||
| 409 | } |
||
| 410 | EOF; |
||
| 411 | $this->collection->push( |
||
| 412 | new GeneratedItem( |
||
| 413 | GeneratedItem::TYPE_FRONTEND, |
||
| 414 | $listQuery, |
||
| 415 | $this->fModel->getName() . '/queryList.graphql' |
||
| 416 | ) |
||
| 417 | ); |
||
| 418 | |||
| 419 | /* |
||
| 420 | * table |
||
| 421 | */ |
||
| 422 | $tableFieldNames = array_map( |
||
| 423 | function (Field $field) { |
||
| 424 | return $field->getName(); |
||
| 425 | }, |
||
| 426 | $this->tableFields |
||
| 427 | ); |
||
| 428 | |||
| 429 | $graphqlQuery = $this->fModel->mapFields( |
||
| 430 | function (Field $f) use ($tableFieldNames) { |
||
| 431 | if (in_array($f->getName(), $tableFieldNames)) { |
||
| 432 | // TODO: filter subfields in relationships |
||
| 433 | return $f->toGraphqlQuery(); |
||
| 434 | } |
||
| 435 | return null; |
||
| 436 | } |
||
| 437 | ); |
||
| 438 | |||
| 439 | $tableFieldParameters = join("\n", array_filter($graphqlQuery)); |
||
| 440 | |||
| 441 | $tableQuery = <<<EOF |
||
| 442 | query (\$page: Int!$filtersQuery) { |
||
| 443 | {$this->lowerNamePlural}(page: \$page$filtersParams) { |
||
| 444 | data { |
||
| 445 | id |
||
| 446 | $tableFieldParameters |
||
| 447 | } |
||
| 448 | |||
| 449 | paginatorInfo { |
||
| 450 | currentPage |
||
| 451 | perPage |
||
| 452 | total |
||
| 453 | lastPage |
||
| 454 | } |
||
| 455 | } |
||
| 456 | } |
||
| 457 | EOF; |
||
| 458 | $this->collection->push( |
||
| 459 | new GeneratedItem( |
||
| 460 | GeneratedItem::TYPE_FRONTEND, |
||
| 461 | $tableQuery, |
||
| 462 | $this->fModel->getName() . '/queryTable.graphql' |
||
| 463 | ) |
||
| 464 | ); |
||
| 465 | |||
| 466 | /* |
||
| 467 | * item |
||
| 468 | */ |
||
| 469 | $graphqlQuery = $this->fModel->mapFields( |
||
| 470 | function (Field $f) { |
||
| 471 | return \Modelarium\Frontend\Util::fieldShow($f) ? $f->toGraphqlQuery() : null; |
||
| 472 | } |
||
| 473 | ); |
||
| 474 | $graphqlQuery = join("\n", array_filter($graphqlQuery)); |
||
| 475 | |||
| 476 | $hasCan = $this->fModel->getExtradataValue('hasCan', 'value', false); |
||
| 477 | $canAttribute = $hasCan ? 'can' : ''; // TODO: subvalues? |
||
| 478 | if ($this->keyAttribute === 'id') { |
||
| 479 | $keyAttributeType = 'ID'; |
||
| 480 | } else { |
||
| 481 | $keyAttributeType = $this->fModel->getField($this->keyAttribute)->getDatatype()->getGraphqlType(); |
||
| 482 | } |
||
| 483 | |||
| 484 | $itemQuery = <<<EOF |
||
| 485 | query (\${$this->keyAttribute}: {$keyAttributeType}!) { |
||
| 486 | {$this->lowerName}({$this->keyAttribute}: \${$this->keyAttribute}) { |
||
| 487 | id |
||
| 488 | $graphqlQuery |
||
| 489 | $canAttribute |
||
| 490 | } |
||
| 491 | } |
||
| 492 | EOF; |
||
| 493 | |||
| 494 | $this->collection->push( |
||
| 495 | new GeneratedItem( |
||
| 496 | GeneratedItem::TYPE_FRONTEND, |
||
| 497 | $itemQuery, |
||
| 498 | $this->fModel->getName() . '/queryItem.graphql' |
||
| 499 | ) |
||
| 500 | ); |
||
| 501 | |||
| 502 | $upsertMutation = <<<EOF |
||
| 503 | mutation upsert(\${$this->lowerName}: {$this->studlyName}Input!) { |
||
| 504 | upsert{$this->studlyName}(input: \${$this->lowerName}) { |
||
| 505 | id |
||
| 506 | } |
||
| 507 | } |
||
| 508 | EOF; |
||
| 509 | $this->collection->push( |
||
| 510 | new GeneratedItem( |
||
| 511 | GeneratedItem::TYPE_FRONTEND, |
||
| 512 | $upsertMutation, |
||
| 513 | $this->fModel->getName() . '/mutationUpsert.graphql' |
||
| 514 | ) |
||
| 515 | ); |
||
| 516 | |||
| 517 | $deleteMutation = <<<EOF |
||
| 518 | mutation delete(\$id: ID!) { |
||
| 519 | delete{$this->studlyName}(id: \$id) { |
||
| 520 | id |
||
| 521 | } |
||
| 522 | } |
||
| 523 | EOF; |
||
| 524 | $this->collection->push( |
||
| 525 | new GeneratedItem( |
||
| 526 | GeneratedItem::TYPE_FRONTEND, |
||
| 527 | $deleteMutation, |
||
| 528 | $this->fModel->getName() . '/mutationDelete.graphql' |
||
| 529 | ) |
||
| 640 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.