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