Issues (200)

src/Lib/Loop/LoopBuilder.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
declare(strict_types=1);
13
14
namespace PhpProfiler\Lib\Loop;
15
16
use LogicException;
17
18
final class LoopBuilder
19
{
20
    /** @var array<int, class-string<LoopMiddlewareInterface>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, class-string<LoopMiddlewareInterface>> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<int, class-string<LoopMiddlewareInterface>>.
Loading history...
21
    private array $process_stack = [];
22
    /** @var array<int, array> */
23
    private array $parameter_stack = [];
24
25
    /**
26
     * @param class-string<LoopMiddlewareInterface> $process
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<LoopMiddlewareInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<LoopMiddlewareInterface>.
Loading history...
27
     */
28
    public function addProcess(string $process, array $parameters): self
29
    {
30
        if (!is_a($process, LoopMiddlewareInterface::class, true)) {
31
            throw new LogicException('1st argument must be a name of a class implements LoopMiddlewareInterface');
32
        }
33
        $self = clone $this;
34
        $self->process_stack[] = $process;
35
        $self->parameter_stack[] = $parameters;
36
        return $self;
37
    }
38
39
    public function build(): Loop
40
    {
41
        $process = null;
42
        $stack_num = count($this->process_stack);
43
        for ($i = $stack_num - 1; $i >= 0; $i--) {
44
            $parameters = $this->parameter_stack[$i];
45
            if (!is_null($process)) {
46
                $parameters[] = $process;
47
            }
48
            $loop_class_name = $this->process_stack[$i];
49
            $process = new $loop_class_name(...$parameters);
50
        }
51
        if (is_null($process)) {
52
            throw new LogicException('no LoopProcess specified');
53
        }
54
        return new Loop($process);
55
    }
56
}
57