Skip to content

Commit eae1bbe

Browse files
committed
Fix phpstan errors
1 parent 8a25ee6 commit eae1bbe

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/Config/Parser/Annotation/GraphClass.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class GraphClass extends ReflectionClass
2121

2222
protected array $propertiesExtended = [];
2323

24-
public function __construct(string $className)
24+
/**
25+
* @param mixed $className
26+
*/
27+
public function __construct($className)
2528
{
2629
parent::__construct($className);
2730

@@ -66,6 +69,7 @@ public function getAnnotations(object $from = null)
6669
return self::getAnnotationReader()->getPropertyAnnotations($from);
6770
}
6871

72+
/** @phpstan-ignore-next-line */
6973
throw new AnnotationException(sprintf('Unable to retrieve annotations from object of class "%s".', get_class($from)));
7074
}
7175

src/Config/Parser/AnnotationParser.php

+14-25
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,6 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
485485
$accessAnnotation = self::getFirstAnnotationMatching($annotations, GQL\Access::class);
486486
$publicAnnotation = self::getFirstAnnotationMatching($annotations, GQL\IsPublic::class);
487487

488-
$isMethod = $reflector instanceof ReflectionMethod;
489-
490488
if (!$fieldAnnotation) {
491489
if ($accessAnnotation || $publicAnnotation) {
492490
throw new InvalidArgumentException(sprintf('The annotations "@Access" and/or "@Visible" defined on "%s" are only usable in addition of annotation "@Field"', $reflector->getName()));
@@ -495,7 +493,7 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
495493
return [];
496494
}
497495

498-
if ($isMethod && !$reflector->isPublic()) {
496+
if ($reflector instanceof ReflectionMethod && !$reflector->isPublic()) {
499497
throw new InvalidArgumentException(sprintf('The Annotation "@Field" can only be applied to public method. The method "%s" is not public.', $reflector->getName()));
500498
}
501499

@@ -508,7 +506,16 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
508506

509507
$fieldConfiguration = self::getDescriptionConfiguration($annotations, true) + $fieldConfiguration;
510508

511-
$args = self::getArgs($fieldAnnotation->args, $isMethod && !$fieldAnnotation->argsBuilder ? $reflector : null);
509+
$args = [];
510+
if (!empty($fieldAnnotation->args)) {
511+
foreach ($fieldAnnotation->args as $arg) {
512+
$args[$arg->name] = ['type' => $arg->type]
513+
+ ($arg->description ? ['description' => $arg->description] : [])
514+
+ ($arg->default ? ['defaultValue' => $arg->default] : []);
515+
}
516+
} elseif ($reflector instanceof ReflectionMethod) {
517+
$args = self::guessArgs($reflector);
518+
}
512519

513520
if (!empty($args)) {
514521
$fieldConfiguration['args'] = $args;
@@ -519,7 +526,7 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
519526
if ($fieldAnnotation->resolve) {
520527
$fieldConfiguration['resolve'] = self::formatExpression($fieldAnnotation->resolve);
521528
} else {
522-
if ($isMethod) {
529+
if ($reflector instanceof ReflectionMethod) {
523530
$fieldConfiguration['resolve'] = self::formatExpression(sprintf('call(%s.%s, %s)', $currentValue, $reflector->getName(), self::formatArgsForExpression($args)));
524531
} else {
525532
if ($fieldName !== $reflector->getName() || 'value' !== $currentValue) {
@@ -551,10 +558,11 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
551558
}
552559
} else {
553560
if (!$fieldType) {
554-
if ($isMethod) {
561+
if ($reflector instanceof ReflectionMethod) {
555562
/** @var ReflectionMethod $reflector */
556563
if ($reflector->hasReturnType()) {
557564
try {
565+
// @phpstan-ignore-next-line
558566
$fieldConfiguration['type'] = self::resolveGraphQLTypeFromReflectionType($reflector->getReturnType(), self::VALID_OUTPUT_TYPES);
559567
} catch (Exception $e) {
560568
throw new InvalidArgumentException(sprintf('The attribute "type" on GraphQL annotation "@%s" is missing on method "%s" and cannot be auto-guessed from type hint "%s"', $fieldAnnotationName, $reflector->getName(), (string) $reflector->getReturnType()));
@@ -732,25 +740,6 @@ private static function getDescriptionConfiguration(array $annotations, bool $wi
732740
return $config;
733741
}
734742

735-
/**
736-
* Get args config from an array of @Arg annotation or by auto-guessing if a method is provided.
737-
*/
738-
private static function getArgs(?array $args, ReflectionMethod $method = null): array
739-
{
740-
$config = [];
741-
if (!empty($args)) {
742-
foreach ($args as $arg) {
743-
$config[$arg->name] = ['type' => $arg->type]
744-
+ ($arg->description ? ['description' => $arg->description] : [])
745-
+ ($arg->default ? ['defaultValue' => $arg->default] : []);
746-
}
747-
} elseif ($method) {
748-
$config = self::guessArgs($method);
749-
}
750-
751-
return $config;
752-
}
753-
754743
/**
755744
* Format an array of args to a list of arguments in an expression.
756745
*/

0 commit comments

Comments
 (0)