Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#726)
by Vincent
17:18 queued 10:52
created

AbstractResolver::hasSolution()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Resolver;
6
7
use GraphQL\Type\Definition\Type;
8
use function array_keys;
9
use function call_user_func_array;
10
use function get_class;
11
use function is_array;
12
use function is_callable;
13
use function sprintf;
14
15
abstract class AbstractResolver implements FluentResolverInterface
16
{
17
    private array $solutions = [];
18
    private array $aliases = [];
19
    private array $solutionOptions = [];
20
    private array $fullyLoadedSolutions = [];
21
22 145
    public function addSolution(string $id, $solutionOrFactory, array $aliases = [], array $options = []): self
23
    {
24 145
        $this->fullyLoadedSolutions[$id] = false;
25 145
        $this->addAliases($id, $aliases);
26
27 145
        $this->solutions[$id] = function () use ($id, $solutionOrFactory) {
28 132
            $solution = $solutionOrFactory;
29 132
            if (self::isSolutionFactory($solutionOrFactory)) {
30 131
                if (!isset($solutionOrFactory[1])) {
31
                    $solutionOrFactory[1] = [];
32
                }
33 131
                $solution = call_user_func_array(...$solutionOrFactory);
0 ignored issues
show
Bug introduced by
The call to call_user_func_array() has too few arguments starting with param_arr. ( Ignorable by Annotation )

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

33
                $solution = /** @scrutinizer ignore-call */ call_user_func_array(...$solutionOrFactory);

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...
34
            }
35 131
            $this->checkSolution($id, $solution);
36
37 130
            return $solution;
38
        };
39 145
        $this->solutionOptions[$id] = $options;
40
41 145
        return $this;
42
    }
43
44 135
    public function hasSolution(string $id): bool
45
    {
46 135
        $id = $this->resolveAlias($id);
47
48 135
        return isset($this->solutions[$id]);
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54 130
    public function getSolution(string $id)
55
    {
56 130
        return $this->loadSolution($id);
57
    }
58
59 5
    public function getSolutions(): array
60
    {
61 5
        return $this->loadSolutions();
62
    }
63
64 5
    public function getSolutionAliases(string $id): array
65
    {
66 5
        return array_keys($this->aliases, $id);
67
    }
68
69
    /**
70
     * @return mixed
71
     */
72 70
    public function getSolutionOptions(string $id)
73
    {
74 70
        $id = $this->resolveAlias($id);
75
76 70
        return isset($this->solutionOptions[$id]) ? $this->solutionOptions[$id] : [];
77
    }
78
79
    /**
80
     * @param mixed $solution
81
     */
82 74
    protected function onLoadSolution($solution): void
83
    {
84 74
    }
85
86
    /**
87
     * @return mixed
88
     */
89 135
    private function loadSolution(string $id)
90
    {
91 135
        $id = $this->resolveAlias($id);
92 135
        if (!$this->hasSolution($id)) {
93 6
            return null;
94
        }
95
96 132
        if ($this->fullyLoadedSolutions[$id]) {
97 23
            return $this->solutions[$id];
98
        } else {
99 132
            $loader = $this->solutions[$id];
100 132
            $this->solutions[$id] = $solution = $loader();
101 130
            $this->onLoadSolution($solution);
102 130
            $this->fullyLoadedSolutions[$id] = true;
103
104 130
            return $solution;
105
        }
106
    }
107
108 145
    private function addAliases(string $id, array $aliases): void
109
    {
110 145
        foreach ($aliases as $alias) {
111 145
            $this->aliases[$alias] = $id;
112
        }
113 145
    }
114
115
    /**
116
     * @param object|array $solutionOrFactory
117
     */
118 132
    private static function isSolutionFactory($solutionOrFactory): bool
119
    {
120 132
        return is_array($solutionOrFactory) && isset($solutionOrFactory[0]) && is_callable($solutionOrFactory[0]);
121
    }
122
123 135
    private function resolveAlias(string $alias): string
124
    {
125 135
        return isset($this->aliases[$alias]) ? $this->aliases[$alias] : $alias;
126
    }
127
128
    /**
129
     * @return mixed[]
130
     */
131 5
    private function loadSolutions(): array
132
    {
133 5
        foreach ($this->solutions as $name => &$solution) {
134 5
            $solution = $this->loadSolution($name);
135
        }
136
137 5
        return $this->solutions;
138
    }
139
140
    /**
141
     * @param mixed $solution
142
     */
143 131
    protected function supportsSolution($solution): bool
144
    {
145 131
        $supportedClass = $this->supportedSolutionClass();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $supportedClass is correct as $this->supportedSolutionClass() targeting Overblog\GraphQLBundle\R...upportedSolutionClass() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
146
147 131
        return  null === $supportedClass || $solution instanceof $supportedClass;
148
    }
149
150
    /**
151
     * @param mixed $solution
152
     */
153 131
    protected function checkSolution(string $id, $solution): void
154
    {
155 131
        if (!$this->supportsSolution($solution)) {
156 1
            throw new UnsupportedResolverException(
157 1
                sprintf('Resolver "%s" must be "%s" "%s" given.', $id, $this->supportedSolutionClass(), get_class($solution))
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->supportedSolutionClass() targeting Overblog\GraphQLBundle\R...upportedSolutionClass() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
158
            );
159
        }
160 130
    }
161
162
    /**
163
     * default return null to accept mixed type.
164
     *
165
     * @return string|null supported class name
166
     */
167 74
    protected function supportedSolutionClass(): ?string
168
    {
169 74
        return null;
170
    }
171
}
172