| Conditions | 7 |
| Paths | 1 |
| Total Lines | 188 |
| 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 |
||
| 298 | protected function registerScalarTypes() |
||
| 299 | { |
||
| 300 | /** |
||
| 301 | * @param $value |
||
| 302 | * @return bool |
||
| 303 | * @throws \TypeError |
||
| 304 | */ |
||
| 305 | function coerceBoolean($value): bool |
||
| 306 | { |
||
| 307 | if (!is_scalar($value)) { |
||
| 308 | throw new \TypeError(sprintf('Boolean cannot represent a non-scalar value: %s', $value)); |
||
| 309 | } |
||
| 310 | |||
| 311 | return (bool)$value; |
||
| 312 | } |
||
| 313 | |||
| 314 | $this->singleton('GraphQLBoolean', function () { |
||
| 315 | return GraphQLScalarType([ |
||
| 316 | 'name' => TypeNameEnum::BOOLEAN, |
||
| 317 | 'description' => 'The `Boolean` scalar type represents `true` or `false`.', |
||
| 318 | 'serialize' => function ($value) { |
||
| 319 | return coerceBoolean($value); |
||
| 320 | }, |
||
| 321 | 'parseValue' => function ($value) { |
||
| 322 | return coerceBoolean($value); |
||
| 323 | }, |
||
| 324 | |||
| 325 | 'parseLiteral' => function (NodeInterface $astNode) { |
||
| 326 | /** @var BooleanValueNode $astNode */ |
||
| 327 | return $astNode->getKind() === NodeKindEnum::BOOLEAN ? $astNode->getValue() : null; |
||
| 328 | }, |
||
| 329 | ]); |
||
| 330 | }); |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $value |
||
| 334 | * @return float |
||
| 335 | * @throws \TypeError |
||
| 336 | */ |
||
| 337 | function coerceFloat($value): float |
||
| 338 | { |
||
| 339 | if ($value === '') { |
||
| 340 | throw new \TypeError('Float cannot represent non numeric value: (empty string)'); |
||
| 341 | } |
||
| 342 | |||
| 343 | if (is_numeric($value) || \is_bool($value)) { |
||
| 344 | return (float)$value; |
||
| 345 | } |
||
| 346 | |||
| 347 | throw new \TypeError(sprintf('Float cannot represent non numeric value: %s', $value)); |
||
| 348 | } |
||
| 349 | |||
| 350 | $this->singleton('GraphQLFloat', function () { |
||
| 351 | return GraphQLScalarType([ |
||
| 352 | 'name' => TypeNameEnum::FLOAT, |
||
| 353 | 'description' => |
||
| 354 | 'The `Float` scalar type represents signed double-precision fractional ' . |
||
| 355 | 'values as specified by ' . |
||
| 356 | '[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point).', |
||
| 357 | 'serialize' => function ($value) { |
||
| 358 | return coerceFloat($value); |
||
| 359 | }, |
||
| 360 | 'parseValue' => function ($value) { |
||
| 361 | return coerceFloat($value); |
||
| 362 | }, |
||
| 363 | 'parseLiteral' => function (NodeInterface $astNode) { |
||
| 364 | /** @var FloatValueNode $astNode */ |
||
| 365 | return in_array($astNode->getKind(), [NodeKindEnum::FLOAT, NodeKindEnum::INT], true) |
||
| 366 | ? $astNode->getValue() |
||
| 367 | : null; |
||
| 368 | }, |
||
| 369 | ]); |
||
| 370 | }); |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param $value |
||
| 374 | * @return int |
||
| 375 | * @throws \TypeError |
||
| 376 | */ |
||
| 377 | function coerceInt($value) |
||
| 378 | { |
||
| 379 | if ($value === '') { |
||
| 380 | throw new \TypeError('Int cannot represent non 32-bit signed integer value: (empty string)'); |
||
| 381 | } |
||
| 382 | |||
| 383 | if (\is_bool($value)) { |
||
| 384 | $value = (int)$value; |
||
| 385 | } |
||
| 386 | |||
| 387 | if (!\is_int($value) || $value > MAX_INT || $value < MIN_INT) { |
||
| 388 | throw new \TypeError(sprintf('Int cannot represent non 32-bit signed integer value: %s', $value)); |
||
| 389 | } |
||
| 390 | |||
| 391 | $intValue = (int)$value; |
||
| 392 | $floatValue = (float)$value; |
||
| 393 | |||
| 394 | if ($floatValue != $intValue || floor($floatValue) !== $floatValue) { |
||
| 395 | throw new \TypeError(sprintf('Int cannot represent non-integer value: %s', $value)); |
||
| 396 | } |
||
| 397 | |||
| 398 | return $intValue; |
||
| 399 | } |
||
| 400 | |||
| 401 | $this->singleton('GraphQLInt', function () { |
||
| 402 | return GraphQLScalarType([ |
||
| 403 | 'name' => TypeNameEnum::INT, |
||
| 404 | 'description' => |
||
| 405 | 'The `Int` scalar type represents non-fractional signed whole numeric ' . |
||
| 406 | 'values. Int can represent values between -(2^31) and 2^31 - 1.', |
||
| 407 | 'serialize' => function ($value) { |
||
| 408 | return coerceInt($value); |
||
| 409 | }, |
||
| 410 | 'parseValue' => function ($value) { |
||
| 411 | return coerceInt($value); |
||
| 412 | }, |
||
| 413 | 'parseLiteral' => function (NodeInterface $astNode) { |
||
| 414 | /** @var IntValueNode $astNode */ |
||
| 415 | return $astNode->getKind() === NodeKindEnum::INT ? $astNode->getValue() : null; |
||
| 416 | }, |
||
| 417 | ]); |
||
| 418 | }); |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param $value |
||
| 422 | * @return string |
||
| 423 | * @throws \TypeError |
||
| 424 | */ |
||
| 425 | function coerceString($value): string |
||
| 426 | { |
||
| 427 | if ($value === null) { |
||
| 428 | return 'null'; |
||
| 429 | } |
||
| 430 | |||
| 431 | if ($value === true) { |
||
| 432 | return 'true'; |
||
| 433 | } |
||
| 434 | |||
| 435 | if ($value === false) { |
||
| 436 | return 'false'; |
||
| 437 | } |
||
| 438 | |||
| 439 | if (!is_scalar($value)) { |
||
| 440 | throw new \TypeError('String cannot represent a non-scalar value'); |
||
| 441 | } |
||
| 442 | |||
| 443 | return (string)$value; |
||
| 444 | } |
||
| 445 | |||
| 446 | $this->singleton('GraphQLID', function () { |
||
| 447 | return GraphQLScalarType([ |
||
| 448 | 'name' => TypeNameEnum::ID, |
||
| 449 | 'description' => |
||
| 450 | 'The `ID` scalar type represents a unique identifier, often used to ' . |
||
| 451 | 'refetch an object or as key for a cache. The ID type appears in a JSON ' . |
||
| 452 | 'response as a String; however, it is not intended to be human-readable. ' . |
||
| 453 | 'When expected as an input type, any string (such as `"4"`) or integer ' . |
||
| 454 | '(such as `4`) input value will be accepted as an ID.', |
||
| 455 | 'serialize' => function ($value) { |
||
| 456 | return coerceString($value); |
||
| 457 | }, |
||
| 458 | 'parseValue' => function ($value) { |
||
| 459 | return coerceString($value); |
||
| 460 | }, |
||
| 461 | 'parseLiteral' => function (NodeInterface $astNode) { |
||
| 462 | /** @var StringValueNode $astNode */ |
||
| 463 | return in_array($astNode->getKind(), [NodeKindEnum::STRING, NodeKindEnum::INT], true) |
||
| 464 | ? $astNode->getValue() |
||
| 465 | : null; |
||
| 466 | }, |
||
| 467 | ]); |
||
| 468 | }); |
||
| 469 | |||
| 470 | $this->singleton('GraphQLString', function () { |
||
| 471 | return GraphQLScalarType([ |
||
| 472 | 'name' => TypeNameEnum::STRING, |
||
| 473 | 'description' => |
||
| 474 | 'The `String` scalar type represents textual data, represented as UTF-8 ' . |
||
| 475 | 'character sequences. The String type is most often used by GraphQL to ' . |
||
| 476 | 'represent free-form human-readable text.', |
||
| 477 | 'serialize' => function ($value) { |
||
| 478 | return coerceString($value); |
||
| 479 | }, |
||
| 480 | 'parseValue' => function ($value) { |
||
| 481 | return coerceString($value); |
||
| 482 | }, |
||
| 483 | 'parseLiteral' => function (NodeInterface $astNode) { |
||
| 484 | /** @var StringValueNode $astNode */ |
||
| 485 | return $astNode->getKind() === NodeKindEnum::STRING ? $astNode->getValue() : null; |
||
| 486 | }, |
||
| 558 |