Passed
Push — master ( 0ff30e...cb8abc )
by Bruno
09:58
created

MinLengthDirective::definition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 3
rs 9.9
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Modelarium\Laravel\Lighthouse\Directives;
4
5
use Closure;
6
use Formularium\DatatypeFactory;
7
use Formularium\Validator\MinLength;
8
use GraphQL\Type\Definition\ResolveInfo;
9
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
10
use Nuwave\Lighthouse\Exceptions\ValidationException;
11
use Nuwave\Lighthouse\Schema\Values\FieldValue;
12
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
13
use Nuwave\Lighthouse\Support\Contracts\ProvidesRules;
14
use Nuwave\Lighthouse\Support\Traits\HasResolverArguments;
15
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
16
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
17
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
18
19
class MinLengthDirective extends BaseDirective implements FieldMiddleware, DefinedDirective
0 ignored issues
show
Deprecated Code introduced by
The interface Nuwave\Lighthouse\Suppor...tracts\DefinedDirective has been deprecated: The method definition() will be moved to ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

19
class MinLengthDirective extends BaseDirective implements FieldMiddleware, /** @scrutinizer ignore-deprecated */ DefinedDirective

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
20
{
21
    use HasResolverArguments;
22
23
    public static function definition(): string
24
    {
25
        return /** @lang GraphQL */ <<<'SDL'
26
"""
27
Base class to extend on model.
28
"""
29
directive @minLength(
30
    """
31
    The base class name with namespace
32
    """
33
    value: Int!
34
) on OBJECT
35
SDL;
36
    }
37
38
    /**
39
     * Resolve the field directive.
40
     */
41
    public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
42
    {
43
        $previousResolver = $fieldValue->getResolver();
44
45
        // Wrap around the resolver
46
        $wrappedResolver = function ($root, array $args, GraphQLContext $context, ResolveInfo $info) use ($previousResolver): string {
47
            error_log("xxxxxx");
48
49
            // Call the resolver, passing along the resolver arguments
50
            /** @var string $result */
51
            $result = $previousResolver($root, $args, $context, $info);
52
53
            MinLength::validate(
0 ignored issues
show
Bug introduced by
The call to Formularium\Validator\MinLength::validate() has too few arguments starting with datatype. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
            MinLength::/** @scrutinizer ignore-call */ 
54
                       validate(

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
54
                $result,
55
                ['value' => $this->directiveArgValue('value')]
56
            );
57
            return $result;
58
        };
59
60
        // Place the wrapped resolver back upon the FieldValue
61
        // It is not resolved right now - we just prepare it
62
        $fieldValue->setResolver($wrappedResolver);
63
64
        // Keep the middleware chain going
65
        return $next($fieldValue);
66
    }
67
}
68