| Total Lines | 84 |
| Code Lines | 44 |
| 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 |
||
| 41 | public function testCollectFields(Schema $schema, DocumentNode $documentNode, string $operationName, ?array $variableValues) |
||
| 42 | { |
||
| 43 | $runtime = new class($variableValues) implements Runtime |
||
| 44 | { |
||
| 45 | /** @var Throwable[] */ |
||
| 46 | public $errors = []; |
||
| 47 | |||
| 48 | /** @var mixed[]|null */ |
||
| 49 | public $variableValues; |
||
| 50 | |||
| 51 | public function __construct($variableValues) |
||
| 52 | { |
||
| 53 | $this->variableValues = $variableValues; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function evaluate(ValueNode $valueNode, InputType $type) |
||
| 57 | { |
||
| 58 | return AST::valueFromAST($valueNode, $type, $this->variableValues); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function addError($error) |
||
| 62 | { |
||
| 63 | $this->errors[] = $error; |
||
| 64 | } |
||
| 65 | }; |
||
| 66 | |||
| 67 | $collector = new Collector($schema, $runtime); |
||
| 68 | $collector->initialize($documentNode, $operationName); |
||
| 69 | |||
| 70 | $pipeline = []; |
||
| 71 | foreach ($collector->collectFields($collector->rootType, $collector->operation->selectionSet) as $shared) { |
||
|
|
|||
| 72 | $execution = new stdClass(); |
||
| 73 | if (! empty($shared->fieldNodes)) { |
||
| 74 | $execution->fieldNodes = array_map(static function (Node $node) { |
||
| 75 | return $node->toArray(true); |
||
| 76 | }, $shared->fieldNodes); |
||
| 77 | } |
||
| 78 | if (! empty($shared->fieldName)) { |
||
| 79 | $execution->fieldName = $shared->fieldName; |
||
| 80 | } |
||
| 81 | if (! empty($shared->resultName)) { |
||
| 82 | $execution->resultName = $shared->resultName; |
||
| 83 | } |
||
| 84 | if (! empty($shared->argumentValueMap)) { |
||
| 85 | $execution->argumentValueMap = []; |
||
| 86 | /** @var Node $valueNode */ |
||
| 87 | foreach ($shared->argumentValueMap as $argumentName => $valueNode) { |
||
| 88 | $execution->argumentValueMap[$argumentName] = $valueNode->toArray(true); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $pipeline[] = $execution; |
||
| 93 | } |
||
| 94 | if (strncmp($operationName, 'ShouldEmitError', strlen('ShouldEmitError')) === 0) { |
||
| 95 | self::assertNotEmpty($runtime->errors, 'There should be errors.'); |
||
| 96 | } else { |
||
| 97 | self::assertEmpty($runtime->errors, 'There must be no errors. Got: ' . json_encode($runtime->errors, JSON_PRETTY_PRINT)); |
||
| 98 | |||
| 99 | if (strncmp($operationName, 'ShouldNotEmit', strlen('ShouldNotEmit')) === 0) { |
||
| 100 | self::assertEmpty($pipeline, 'No instructions should be emitted.'); |
||
| 101 | } else { |
||
| 102 | self::assertNotEmpty($pipeline, 'There should be some instructions emitted.'); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $result = []; |
||
| 107 | if (! empty($runtime->errors)) { |
||
| 108 | $result['errors'] = array_map( |
||
| 109 | FormattedError::prepareFormatter(null, false), |
||
| 110 | $runtime->errors |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | if (! empty($pipeline)) { |
||
| 114 | $result['pipeline'] = $pipeline; |
||
| 115 | } |
||
| 116 | |||
| 117 | $json = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n"; |
||
| 118 | |||
| 119 | $fileName = __DIR__ . DIRECTORY_SEPARATOR . basename(__FILE__, '.php') . 'Snapshots' . DIRECTORY_SEPARATOR . $operationName . '.json'; |
||
| 120 | if (! file_exists($fileName)) { |
||
| 121 | file_put_contents($fileName, $json); |
||
| 122 | } |
||
| 123 | |||
| 124 | self::assertStringEqualsFile($fileName, $json); |
||
| 125 | } |
||
| 379 |