Completed
Push — master ( fbefd9...bc1382 )
by Adrien
04:14
created

InputFieldsConfigurationFactory::completeField()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 20
cts 20
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 19
nc 32
nop 3
crap 8
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\Type\Definition\Type;
10
use ReflectionMethod;
11
use ReflectionParameter;
12
13
/**
14
 * A factory to create a configuration for all setters of an entity
15
 */
16
class InputFieldsConfigurationFactory extends AbstractFieldsConfigurationFactory
17
{
18 3
    protected function getMethodPattern(): string
19
    {
20 3
        return '~^set[A-Z]~';
21
    }
22
23
    /**
24
     * Get the entire configuration for a method
25
     * @param ReflectionMethod $method
26
     * @return array
27
     */
28 3
    protected function methodToConfiguration(ReflectionMethod $method): ?array
29
    {
30
        // Silently ignore setter with anything than exactly 1 parameter
31 3
        $params = $method->getParameters();
32 3
        if (count($params) !== 1) {
33 2
            return null;
34
        }
35 3
        $param = reset($params);
36
37
        // Get a field from annotation, or an empty one
38 3
        $field = $this->getAnnotationReader()->getMethodAnnotation($method, Input::class) ?? new Input();
39
40 3
        if (!$field->type instanceof Type) {
41 3
            $this->convertTypeDeclarationsToInstances($method, $field);
42 3
            $this->completeField($method, $param, $field);
1 ignored issue
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...actory::completeField() 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...
43
        }
44
45 2
        return $field->toArray();
46
    }
47
48
    /**
49
     * All its types will be converted from string to real instance of Type
50
     *
51
     * @param ReflectionMethod $method
52
     * @param Field $field
53
     */
54 3
    private function convertTypeDeclarationsToInstances(ReflectionMethod $method, Input $field): void
55
    {
56 3
        $field->type = $this->getTypeFromPhpDeclaration($method, $field->type);
57 3
    }
58
59
    /**
60
     * Complete field with info from doc blocks and type hints
61
     * @param ReflectionMethod $method
62
     * @param ReflectionParameter $param
63
     * @param Field $field
64
     */
65 3
    private function completeField(ReflectionMethod $method, ReflectionParameter $param, Input $field): void
66
    {
67 3
        $fieldName = lcfirst(preg_replace('~^set~', '', $method->getName()));
68 3
        if (!$field->name) {
69 3
            $field->name = $fieldName;
70
        }
71
72 3
        $docBlock = new DocBlockReader($method);
73 3
        if (!$field->description) {
74 3
            $field->description = $docBlock->getMethodDescription();
75
        }
76
77 3
        if (!isset($field->defaultValue) && $param->isDefaultValueAvailable()) {
78 2
            $field->defaultValue = $param->getDefaultValue();
79
        }
80
81
        // If still no type, look for docblock
82 3
        if (!$field->type) {
83 3
            $typeDeclaration = $docBlock->getParameterType($param);
84 3
            $this->throwIfArray($param, $typeDeclaration);
85 3
            $field->type = $this->getTypeFromPhpDeclaration($method, $typeDeclaration, true);
86
        }
87
88
        // If still no type, look for type hint
89 3
        $type = $param->getType();
90 3
        if (!$field->type && $type) {
91 2
            $this->throwIfArray($param, (string) $type);
92 2
            $field->type = $this->refelectionTypeToType($type, true);
93
        }
94
95 3
        $field->type = $this->nonNullIfHasDefault($param, $field->type);
1 ignored issue
show
Bug introduced by
It seems like $field->type can also be of type null or string; however, GraphQL\Doctrine\Factory...::nonNullIfHasDefault() does only seem to accept object<GraphQL\Type\Definition\Type>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
96
97
        // If still no type, cannot continue
98 3
        $this->throwIfNotInputType($param, $field->type, 'Input');
99 2
    }
100
}
101