TestController::test()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 9.6111
c 0
b 0
f 0
cc 5
nc 3
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers\Fixtures;
5
6
use ArrayObject;
7
use TheCodingMachine\GraphQL\Controllers\Annotations\Logged;
8
use TheCodingMachine\GraphQL\Controllers\Annotations\Mutation;
9
use TheCodingMachine\GraphQL\Controllers\Annotations\Query;
10
use TheCodingMachine\GraphQL\Controllers\Annotations\Right;
11
use TheCodingMachine\GraphQL\Controllers\Types\ID;
12
13
class TestController
14
{
15
    /**
16
     * @Query
17
     * @param int $int
18
     * @param TestObject[] $list
19
     * @param bool|null $boolean
20
     * @param float|null $float
21
     * @param \DateTimeImmutable|null $dateTimeImmutable
22
     * @param \DateTime|\DateTimeInterface|null $dateTime
23
     * @param string $withDefault
24
     * @param null|string $string
25
     * @param ID|null $id
26
     * @return TestObject
27
     */
28
    public function test(int $int, array $list, ?bool $boolean, ?float $float, ?\DateTimeImmutable $dateTimeImmutable, ?\DateTimeInterface $dateTime, string $withDefault = 'default', ?string $string = null, ID $id = null): TestObject
29
    {
30
        $str = '';
31
        foreach ($list as $test) {
32
            if (!$test instanceof TestObject) {
33
                throw new \RuntimeException('TestObject instance expected.');
34
            }
35
            $str .= $test->getTest();
36
        }
37
        return new TestObject($string.$int.$str.($boolean?'true':'false').$float.$dateTimeImmutable->format('YmdHis').$dateTime->format('YmdHis').$withDefault.($id !== null ? $id->val() : ''));
0 ignored issues
show
Bug introduced by
The method format() does not exist on null. ( Ignorable by Annotation )

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

37
        return new TestObject($string.$int.$str.($boolean?'true':'false').$float.$dateTimeImmutable->format('YmdHis').$dateTime->/** @scrutinizer ignore-call */ format('YmdHis').$withDefault.($id !== null ? $id->val() : ''));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
    }
39
40
    /**
41
     * @Mutation
42
     * @param TestObject $testObject
43
     * @return TestObject
44
     */
45
    public function mutation(TestObject $testObject): TestObject
46
    {
47
        return $testObject;
48
    }
49
50
    /**
51
     * @Query
52
     * @Logged
53
     */
54
    public function testLogged(): TestObject
55
    {
56
        return new TestObject('foo');
57
    }
58
59
    /**
60
     * @Query
61
     * @Right(name="CAN_FOO")
62
     */
63
    public function testRight(): TestObject
64
    {
65
        return new TestObject('foo');
66
    }
67
68
    /**
69
     * @Query(outputType="ID")
70
     */
71
    public function testFixReturnType(): TestObject
72
    {
73
        return new TestObject('foo');
74
    }
75
76
    /**
77
     * @Query(name="nameFromAnnotation")
78
     */
79
    public function testNameFromAnnotation(): TestObject
80
    {
81
        return new TestObject('foo');
82
    }
83
84
    /**
85
     * @Query(name="arrayObject")
86
     * @return ArrayObject|TestObject[]
87
     */
88
    public function testArrayObject(): ArrayObject
89
    {
90
        return new ArrayObject([]);
91
    }
92
93
    /**
94
     * @Query(name="arrayObject")
95
     * @return iterable|TestObject[]
96
     */
97
    public function testIterable(): iterable
98
    {
99
        return array();
100
    }
101
102
    /**
103
     * @Query(name="union")
104
     * @return TestObject|TestObject2
105
     */
106
    public function testUnion()
107
    {
108
        return new TestObject2('foo');
109
    }
110
}
111