Issues (200)

src/Lib/Loop/AsyncLoopBuilder.php (2 issues)

Severity
1
<?php
2
3
/**
4
 * This file is part of the sj-i/php-profiler package.
5
 *
6
 * (c) sji <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpProfiler\Lib\Loop;
13
14
use LogicException;
15
16
final class AsyncLoopBuilder
17
{
18
    /** @var array<int, class-string<AsyncLoopMiddlewareInterface>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, class-string<...opMiddlewareInterface>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<int, class-string<AsyncLoopMiddlewareInterface>>.
Loading history...
19
    private array $process_stack = [];
20
    /** @var array<int, array> */
21
    private array $parameter_stack = [];
22
23
    /**
24
     * @param class-string<AsyncLoopMiddlewareInterface> $process
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<AsyncLoopMiddlewareInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<AsyncLoopMiddlewareInterface>.
Loading history...
25
     */
26
    public function addProcess(string $process, array $parameters): self
27
    {
28
        if (!is_a($process, AsyncLoopMiddlewareInterface::class, true)) {
29
            throw new LogicException('1st argument must be a name of a class implements LoopMiddlewareInterface');
30
        }
31
        $self = clone $this;
32
        $self->process_stack[] = $process;
33
        $self->parameter_stack[] = $parameters;
34
        return $self;
35
    }
36
37
    public function build(): AsyncLoop
38
    {
39
        $process = null;
40
        $stack_num = count($this->process_stack);
41
        for ($i = $stack_num - 1; $i >= 0; $i--) {
42
            $parameters = $this->parameter_stack[$i];
43
            if (!is_null($process)) {
44
                $parameters[] = $process;
45
            }
46
            $loop_class_name = $this->process_stack[$i];
47
            $process = new $loop_class_name(...$parameters);
48
        }
49
        if (is_null($process)) {
50
            throw new LogicException('no LoopProcess specified');
51
        }
52
        return new AsyncLoop($process);
53
    }
54
}
55