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