Test Failed
Push — master ( 2d53c5...8b1ae1 )
by Kirill
02:39
created

Context::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend;
11
12
use Railt\SDL\Frontend\IR\JoinableOpcode;
13
14
/**
15
 * Class Context
16
 */
17
class Context
18
{
19
    /**
20
     * @var JoinableOpcode[]
21
     */
22
    private $stack;
23
24
    /**
25
     * @var bool
26
     */
27
    private $await = false;
28
29
    /**
30
     * Context constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->stack = new \SplStack();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SplStack() of type object<SplStack> is incompatible with the declared type array<integer,object<Rai...end\IR\JoinableOpcode>> of property $stack.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
    }
36
37
    /**
38
     * @return null|JoinableOpcode
39
     */
40
    public function current(): ?JoinableOpcode
41
    {
42
        return $this->stack->count() ? $this->stack->top() : null;
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->stack (of type array<integer,object<Rai...end\IR\JoinableOpcode>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method top cannot be called on $this->stack (of type array<integer,object<Rai...end\IR\JoinableOpcode>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
43
    }
44
45
    /**
46
     * @param \Closure $then
47
     * @return \Generator
48
     */
49
    public function transaction(\Closure $then): \Generator
50
    {
51
        $current = $this->current();
52
53
        $result = $then($current);
54
55
        if ($current !== $this->current()) {
56
            $this->close();
57
        }
58
59
        return $result;
60
    }
61
62
    /**
63
     * @return null|JoinableOpcode
64
     */
65
    public function create(): ?JoinableOpcode
66
    {
67
        $this->await = true;
68
69
        return $this->current();
70
    }
71
72
    /**
73
     * @return null|JoinableOpcode
74
     */
75
    public function close(): ?JoinableOpcode
76
    {
77
        $this->await = false;
78
79
        return $this->stack->pop();
0 ignored issues
show
Bug introduced by
The method pop cannot be called on $this->stack (of type array<integer,object<Rai...end\IR\JoinableOpcode>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
80
    }
81
82
    /**
83
     * @param JoinableOpcode $opcode
84
     * @return JoinableOpcode
85
     */
86
    public function match(JoinableOpcode $opcode): JoinableOpcode
87
    {
88
        if ($this->await) {
89
            $this->stack->push($opcode);
0 ignored issues
show
Bug introduced by
The method push cannot be called on $this->stack (of type array<integer,object<Rai...end\IR\JoinableOpcode>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
90
            $this->await = false;
91
        }
92
93
        return $opcode;
94
    }
95
}
96