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