GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SyncPromiseAdapter::createFulfilled()   A
last analyzed

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 GraphQL\Executor\Promise\Adapter;
6
7
use GraphQL\Error\InvariantViolation;
8
use GraphQL\Executor\ExecutionResult;
9
use GraphQL\Executor\Promise\Promise;
10
use GraphQL\Executor\Promise\PromiseAdapter;
11
use GraphQL\Utils\Utils;
12
use Throwable;
13
use function count;
14
15
/**
16
 * Allows changing order of field resolution even in sync environments
17
 * (by leveraging queue of deferreds and promises)
18
 */
19
class SyncPromiseAdapter implements PromiseAdapter
20
{
21
    /**
22
     * @inheritdoc
23
     */
24
    public function isThenable($value)
25 217
    {
26
        return $value instanceof SyncPromise;
27 217
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function convertThenable($thenable)
33 43
    {
34
        if (! $thenable instanceof SyncPromise) {
35 43
            // End-users should always use Deferred (and don't use SyncPromise directly)
36 1
            throw new InvariantViolation('Expected instance of GraphQL\Deferred, got ' . Utils::printSafe($thenable));
37
        }
38
39 43
        return new Promise($thenable, $this);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 71
    public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null)
46
    {
47
        /** @var SyncPromise $adoptedPromise */
48 71
        $adoptedPromise = $promise->adoptedPromise;
49
50 71
        return new Promise($adoptedPromise->then($onFulfilled, $onRejected), $this);
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 42
    public function create(callable $resolver)
57
    {
58 42
        $promise = new SyncPromise();
59
60
        try {
61 42
            $resolver(
62
                [
63 42
                    $promise,
64 42
                    'resolve',
65
                ],
66
                [
67 42
                    $promise,
68 42
                    'reject',
69
                ]
70
            );
71
        } catch (Throwable $e) {
72
            $promise->reject($e);
73
        }
74
75 42
        return new Promise($promise, $this);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 211
    public function createFulfilled($value = null)
82
    {
83 211
        $promise = new SyncPromise();
84
85 211
        return new Promise($promise->resolve($value), $this);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 1
    public function createRejected($reason)
92
    {
93 1
        $promise = new SyncPromise();
94
95 1
        return new Promise($promise->reject($reason), $this);
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101 43
    public function all(array $promisesOrValues)
102
    {
103 43
        $all = new SyncPromise();
104
105 43
        $total  = count($promisesOrValues);
106 43
        $count  = 0;
107 43
        $result = [];
108
109 43
        foreach ($promisesOrValues as $index => $promiseOrValue) {
110 43
            if ($promiseOrValue instanceof Promise) {
111 43
                $result[$index] = null;
112 43
                $promiseOrValue->then(
113
                    static function ($value) use ($index, &$count, $total, &$result, $all) : void {
114 41
                        $result[$index] = $value;
115 41
                        $count++;
116 41
                        if ($count < $total) {
117 26
                            return;
118
                        }
119
120 41
                        $all->resolve($result);
121 43
                    },
122 43
                    [$all, 'reject']
123
                );
124
            } else {
125 12
                $result[$index] = $promiseOrValue;
126 43
                $count++;
127
            }
128
        }
129 43
        if ($count === $total) {
130 1
            $all->resolve($result);
131
        }
132
133 43
        return new Promise($all, $this);
134
    }
135
136
    /**
137
     * Synchronously wait when promise completes
138
     *
139
     * @return ExecutionResult
140
     */
141 242
    public function wait(Promise $promise)
142
    {
143 242
        $this->beforeWait($promise);
144 242
        $taskQueue = SyncPromise::getQueue();
145 242
146
        while ($promise->adoptedPromise->state === SyncPromise::PENDING &&
0 ignored issues
show
Bug introduced by
The property state does not seem to exist on React\Promise\Promise.
Loading history...
147 242
            ! $taskQueue->isEmpty()
148 242
        ) {
149
            SyncPromise::runQueue();
150 66
            $this->onWait($promise);
151 66
        }
152 66
153
        /** @var SyncPromise $syncPromise */
154
        $syncPromise = $promise->adoptedPromise;
155
156 242
        if ($syncPromise->state === SyncPromise::FULFILLED) {
157
            return $syncPromise->result;
158 242
        }
159 242
160
        if ($syncPromise->state === SyncPromise::REJECTED) {
161
            throw $syncPromise->result;
162
        }
163
164
        throw new InvariantViolation('Could not resolve promise');
165
    }
166
167
    /**
168
     * Execute just before starting to run promise completion
169
     */
170
    protected function beforeWait(Promise $promise)
0 ignored issues
show
Unused Code introduced by
The parameter $promise is not used and could be removed. ( Ignorable by Annotation )

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

170
    protected function beforeWait(/** @scrutinizer ignore-unused */ Promise $promise)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
    {
172 242
    }
173
174 242
    /**
175
     * Execute while running promise completion
176
     */
177
    protected function onWait(Promise $promise)
0 ignored issues
show
Unused Code introduced by
The parameter $promise is not used and could be removed. ( Ignorable by Annotation )

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

177
    protected function onWait(/** @scrutinizer ignore-unused */ Promise $promise)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
    {
179 66
    }
180
}
181