Completed
Push — master ( 3aac06...366db0 )
by Adrien
01:53
created

methodToConfiguration()   C

Complexity

Conditions 9
Paths 33

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 25
cts 25
cp 1
rs 5.2941
c 0
b 0
f 0
cc 9
eloc 25
nc 33
nop 1
crap 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Factory;
6
7
use GraphQL\Doctrine\Annotation\Input;
8
use GraphQL\Doctrine\DocBlockReader;
9
use GraphQL\Doctrine\Types;
10
use GraphQL\Type\Definition\Type;
11
use ReflectionMethod;
12
13
/**
14
 * A factory to create a configuration for all fields of an entity
15
 */
16
class InputFieldsConfigurationFactory extends AbstractFieldsConfigurationFactory
17
{
18 2
    protected function getMethodPattern(): string
19
    {
20 2
        return '~^set[A-Z]~';
21
    }
22
23
    /**
24
     * Get the entire configuration for a method
25
     * @param ReflectionMethod $method
26
     * @return array
27
     */
28 2
    protected function methodToConfiguration(ReflectionMethod $method): ?array
29
    {
30
        // Silently ignore setter with anything than exactly 1 parameter
31 2
        $params = $method->getParameters();
32 2
        if (count($params) !== 1) {
33 1
            return null;
34
        }
35 2
        $param = reset($params);
36
37
        // First get user specified values
38 2
        $field = $this->getInputFieldFromAnnotation($method);
39
40 2
        $fieldName = lcfirst(preg_replace('~^set~', '', $method->getName()));
41 2
        if (!$field->name) {
42 2
            $field->name = $fieldName;
43
        }
44
45 2
        $docBlock = new DocBlockReader($method);
46 2
        if (!$field->description) {
47 2
            $field->description = $docBlock->getMethodDescription();
48
        }
49
50 2
        if (!isset($field->defaultValue) && $param->isDefaultValueAvailable()) {
51 1
            $field->defaultValue = $param->getDefaultValue();
52
        }
53
54
        // If still no type, look for docblock
55 2
        if (!$field->type) {
56 2
            $typeDeclaration = $docBlock->getParameterType($param);
0 ignored issues
show
Security Bug introduced by
It seems like $param defined by reset($params) on line 35 can also be of type false; however, GraphQL\Doctrine\DocBloc...der::getParameterType() does only seem to accept object<ReflectionParameter>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
57 2
            $this->throwIfArray($param, $typeDeclaration);
0 ignored issues
show
Security Bug introduced by
It seems like $param defined by reset($params) on line 35 can also be of type false; however, GraphQL\Doctrine\Factory...Factory::throwIfArray() does only seem to accept object<ReflectionParameter>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
58 2
            $field->type = $this->getTypeFromPhpDeclaration($method, $typeDeclaration, true);
59
        }
60
61
        // If still no type, look for type hint
62 2
        $type = $param->getType();
63 2
        if (!$field->type && $type) {
64 1
            $this->throwIfArray($param, (string) $type);
0 ignored issues
show
Security Bug introduced by
It seems like $param defined by reset($params) on line 35 can also be of type false; however, GraphQL\Doctrine\Factory...Factory::throwIfArray() does only seem to accept object<ReflectionParameter>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
65 1
            $field->type = $this->refelectionTypeToType($type, true);
66
        }
67
68 2
        $field->type = $this->nonNullIfHasDefault($param, $field->type);
0 ignored issues
show
Security Bug introduced by
It seems like $param defined by reset($params) on line 35 can also be of type false; however, GraphQL\Doctrine\Factory...::nonNullIfHasDefault() does only seem to accept object<ReflectionParameter>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
69
70
        // If still no type, cannot continue
71 2
        $this->throwIfNotInputType($param, $field->type, 'Input');
0 ignored issues
show
Security Bug introduced by
It seems like $param defined by reset($params) on line 35 can also be of type false; however, GraphQL\Doctrine\Factory...::throwIfNotInputType() does only seem to accept object<ReflectionParameter>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
72
73 1
        return $field->toArray();
74
    }
75
76
    /**
77
     * Get a field from annotation, or an empty one
78
     * All its types will be converted from string to real instance of Type
79
     *
80
     * @param ReflectionMethod $method
81
     * @return Input
82
     */
83 2
    private function getInputFieldFromAnnotation(ReflectionMethod $method): Input
84
    {
85 2
        $field = $this->getAnnotationReader()->getMethodAnnotation($method, Input::class) ?? new Input();
86 2
        $field->type = $this->getTypeFromPhpDeclaration($method, $field->type);
87
88 2
        return $field;
89
    }
90
}
91