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