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