[style changes] arrays and callback

This commit is contained in:
Andreas Heiberg 2016-11-10 16:12:44 +00:00 committed by vladar
parent 3c98963f72
commit 80a662564e
6 changed files with 35 additions and 26 deletions

View File

@ -5,7 +5,6 @@ use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation; use GraphQL\Language\SourceLocation;
use GraphQL\Utils; use GraphQL\Utils;
/** /**
* Class Error * Class Error
* A GraphQLError describes an Error found during the parse, validate, or * A GraphQLError describes an Error found during the parse, validate, or
@ -141,8 +140,12 @@ class Error extends \Exception implements \JsonSerializable
{ {
if (null === $this->positions) { if (null === $this->positions) {
if (!empty($this->nodes)) { if (!empty($this->nodes)) {
$positions = array_map(function($node) { return isset($node->loc) ? $node->loc->start : null; }, $this->nodes); $positions = array_map(function($node) {
$this->positions = array_filter($positions, function($p) {return $p !== null;}); return isset($node->loc) ? $node->loc->start : null;
}, $this->nodes);
$this->positions = array_filter($positions, function($p) {
return $p !== null;
});
} }
} }
return $this->positions; return $this->positions;

View File

@ -4,10 +4,6 @@ namespace GraphQL\Error;
use GraphQL\Language\Source; use GraphQL\Language\Source;
use GraphQL\Language\SourceLocation; use GraphQL\Language\SourceLocation;
/**
* Class SyntaxError
* @package GraphQL\Error
*/
class SyntaxError extends Error class SyntaxError extends Error
{ {
/** /**
@ -33,9 +29,9 @@ class SyntaxError extends Error
public static function highlightSourceAtLocation(Source $source, SourceLocation $location) public static function highlightSourceAtLocation(Source $source, SourceLocation $location)
{ {
$line = $location->line; $line = $location->line;
$prevLineNum = (string)($line - 1); $prevLineNum = (string) ($line - 1);
$lineNum = (string)$line; $lineNum = (string) $line;
$nextLineNum = (string)($line + 1); $nextLineNum = (string) ($line + 1);
$padLen = mb_strlen($nextLineNum, 'UTF-8'); $padLen = mb_strlen($nextLineNum, 'UTF-8');
$unicodeChars = json_decode('"\u2028\u2029"'); // Quick hack to get js-compatible representation of these chars $unicodeChars = json_decode('"\u2028\u2029"'); // Quick hack to get js-compatible representation of these chars

View File

@ -42,11 +42,17 @@ class Printer
{ {
public function doPrint($ast) public function doPrint($ast)
{ {
return Visitor::visit($ast, array( return Visitor::visit($ast, [
'leave' => array( 'leave' => [
Node::NAME => function($node) {return '' . $node->value;}, Node::NAME => function($node) {
Node::VARIABLE => function($node) {return '$' . $node->name;}, return '' . $node->value;
Node::DOCUMENT => function(Document $node) {return $this->join($node->definitions, "\n\n") . "\n";}, },
Node::VARIABLE => function($node) {
return '$' . $node->name;
},
Node::DOCUMENT => function(Document $node) {
return $this->join($node->definitions, "\n\n") . "\n";
},
Node::OPERATION_DEFINITION => function(OperationDefinition $node) { Node::OPERATION_DEFINITION => function(OperationDefinition $node) {
$op = $node->operation; $op = $node->operation;
$name = $node->name; $name = $node->name;
@ -187,13 +193,15 @@ class Printer
$this->block($def->fields) $this->block($def->fields)
], ' '); ], ' ');
}, },
Node::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinition $def) {return "extend {$def->definition}";}, Node::TYPE_EXTENSION_DEFINITION => function(TypeExtensionDefinition $def) {
return "extend {$def->definition}";
},
Node::DIRECTIVE_DEFINITION => function(DirectiveDefinition $def) { Node::DIRECTIVE_DEFINITION => function(DirectiveDefinition $def) {
return 'directive @' . $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')') return 'directive @' . $def->name . $this->wrap('(', $this->join($def->arguments, ', '), ')')
. ' on ' . $this->join($def->locations, ' | '); . ' on ' . $this->join($def->locations, ' | ');
} }
) ]
)); ]);
} }
/** /**

View File

@ -36,7 +36,7 @@ class Source
$utfChars = json_decode('"\u2028\u2029"'); $utfChars = json_decode('"\u2028\u2029"');
$lineRegexp = '/\r\n|[\n\r'.$utfChars.']/su'; $lineRegexp = '/\r\n|[\n\r'.$utfChars.']/su';
$matches = array(); $matches = [];
preg_match_all($lineRegexp, mb_substr($this->body, 0, $position, 'UTF-8'), $matches, PREG_OFFSET_CAPTURE); preg_match_all($lineRegexp, mb_substr($this->body, 0, $position, 'UTF-8'), $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $index => $match) { foreach ($matches[0] as $index => $match) {

View File

@ -38,7 +38,7 @@ class Visitor
return $r; return $r;
} }
public static $visitorKeys = array( public static $visitorKeys = [
Node::NAME => [], Node::NAME => [],
Node::DOCUMENT => ['definitions'], Node::DOCUMENT => ['definitions'],
Node::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'], Node::OPERATION_DEFINITION => ['name', 'variableDefinitions', 'directives', 'selectionSet'],
@ -77,7 +77,7 @@ class Visitor
Node::INPUT_OBJECT_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ], Node::INPUT_OBJECT_TYPE_DEFINITION => [ 'name', 'directives', 'fields' ],
Node::TYPE_EXTENSION_DEFINITION => [ 'definition' ], Node::TYPE_EXTENSION_DEFINITION => [ 'definition' ],
Node::DIRECTIVE_DEFINITION => [ 'name', 'arguments', 'locations' ] Node::DIRECTIVE_DEFINITION => [ 'name', 'arguments', 'locations' ]
); ];
/** /**
* visit() will walk through an AST using a depth first traversal, calling * visit() will walk through an AST using a depth first traversal, calling
@ -279,16 +279,16 @@ class Visitor
} }
if (!$isLeaving) { if (!$isLeaving) {
$stack = array( $stack = [
'inArray' => $inArray, 'inArray' => $inArray,
'index' => $index, 'index' => $index,
'keys' => $keys, 'keys' => $keys,
'edits' => $edits, 'edits' => $edits,
'prev' => $stack 'prev' => $stack
); ];
$inArray = is_array($node); $inArray = is_array($node);
$keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: array(); $keys = ($inArray ? $node : $visitorKeys[$node->kind]) ?: [];
$index = -1; $index = -1;
$edits = []; $edits = [];
if ($parent) { if ($parent) {

View File

@ -11,10 +11,12 @@ class Utils
{ {
/** /**
* @param object $obj * @param object $obj
* @param array $vars * @param array $vars
* @param array $requiredKeys
*
* @return array * @return array
*/ */
public static function assign($obj, array $vars, array $requiredKeys = array()) public static function assign($obj, array $vars, array $requiredKeys = [])
{ {
foreach ($requiredKeys as $key) { foreach ($requiredKeys as $key) {
if (!isset($key, $vars)) { if (!isset($key, $vars)) {