| Conditions | 8 |
| Paths | 8 |
| Total Lines | 62 |
| Code Lines | 32 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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); |
||
| 13 | public static function getFieldFromDirectives( |
||
| 14 | string $fieldName, |
||
| 15 | string $datatypeName, |
||
| 16 | NodeList $directives |
||
| 17 | ): Field { |
||
| 18 | $validators = []; |
||
| 19 | $renderable = []; |
||
| 20 | foreach ($directives as $directive) { |
||
| 21 | $name = $directive->name->value; |
||
| 22 | |||
| 23 | if ($name === 'renderable') { |
||
| 24 | foreach ($directive->arguments as $arg) { |
||
| 25 | /** |
||
| 26 | * @var \GraphQL\Language\AST\ArgumentNode $arg |
||
| 27 | */ |
||
| 28 | |||
| 29 | $argName = $arg->name->value; |
||
| 30 | $argValue = $arg->value->value; /** @phpstan-ignore-line */ |
||
|
|
|||
| 31 | $renderable[$argName] = $argValue; |
||
| 32 | } |
||
| 33 | continue; |
||
| 34 | } |
||
| 35 | |||
| 36 | $validator = null; |
||
| 37 | try { |
||
| 38 | $validator = ValidatorFactory::class($name); |
||
| 39 | } catch (ClassNotFoundException $e) { |
||
| 40 | continue; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ValidatorMetadata $metadata |
||
| 45 | */ |
||
| 46 | $metadata = $validator::getMetadata(); |
||
| 47 | $arguments = []; |
||
| 48 | |||
| 49 | foreach ($directive->arguments as $arg) { |
||
| 50 | /** |
||
| 51 | * @var \GraphQL\Language\AST\ArgumentNode $arg |
||
| 52 | */ |
||
| 53 | |||
| 54 | $argName = $arg->name->value; |
||
| 55 | $argValue = $arg->value->value; /** @phpstan-ignore-line */ |
||
| 56 | |||
| 57 | $argValidator = $metadata->argument($argName); |
||
| 58 | if (!$argValidator) { |
||
| 59 | throw new Exception("Directive $validator does not have argument $argName"); |
||
| 60 | } |
||
| 61 | if ($argValidator->type === 'Int') { |
||
| 62 | $argValue = (int)$argValue; |
||
| 63 | } |
||
| 64 | $arguments[$argName] = $argValue; |
||
| 65 | } |
||
| 66 | |||
| 67 | $validators[$name] = $arguments; |
||
| 68 | } |
||
| 69 | |||
| 70 | return new Field( |
||
| 71 | $fieldName, |
||
| 72 | $datatypeName, |
||
| 73 | $renderable, |
||
| 74 | $validators |
||
| 75 | ); |
||
| 78 |