Completed
Push — master ( 9a3771...84f8db )
by Ryosuke
05:40
created

GeneratorContainer::valid()   B

Complexity

Conditions 5
Paths 11

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 8.8571
cc 5
eloc 7
nc 11
nop 0
crap 5
1
<?php
2
3
namespace mpyw\Co\Internal;
4
use mpyw\Co\CoInterface;
5
6
class GeneratorContainer
7
{
8
    /**
9
     * Generator.
10
     * @var \Generator
11
     */
12
    private $g;
13
14
    /**
15
     * Generator object hash.
16
     * @var string
17
     */
18
    private $h;
19
20
    /**
21
     * Thrown exception.
22
     * @var \RuntimeException
23
     */
24
    private $e;
25
26
    /**
27
     * Parent yield key.
28
     * @var mixed
29
     */
30
    private $yieldKey;
31
32
    /**
33
     * Constructor.
34
     * @param \Generator $g
35
     */
36 42
    public function __construct(\Generator $g, $yield_key = null)
37 42
    {
38 42
        $this->g = $g;
39 42
        $this->h = spl_object_hash($g);
40 42
        $this->yieldKey = $yield_key;
41 42
        $this->valid();
42 42
    }
43
44
    /**
45
     * Return parent yield key.
46
     * @return mixed
47
     */
48 27
    public function getYieldKey()
49 27
    {
50 27
        return $this->yieldKey;
51
    }
52
53
    /**
54
     * Return generator hash.
55
     * @return string
56
     */
57 28
    public function __toString()
58 28
    {
59 28
        return $this->h;
60
    }
61
62
    /**
63
     * Return whether generator is actually working.
64
     * @return bool
65
     */
66 42
    public function valid()
67 42
    {
68
        try {
69 42
            $this->g->current();
70 42
            return $this->e === null && $this->g->valid() && $this->g->key() !== CoInterface::RETURN_WITH;
71 2
        } catch (\Throwable $e) {} catch (\Exception $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
72 2
        $this->e = $e;
0 ignored issues
show
Documentation Bug introduced by
It seems like $e of type object<Throwable> or object<Exception> is incompatible with the declared type object<RuntimeException> of property $e.

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...
73 2
        return false;
74
    }
75
76
    /**
77
     * Return current key.
78
     * @return mixed
79
     */
80 32
    public function key()
81 32
    {
82 32
        $this->validateValidity();
83 32
        return $this->g->key();
84
    }
85
86
    /**
87
     * Return current value.
88
     * @return mixed
89
     */
90 34
    public function current()
91 34
    {
92 34
        $this->validateValidity();
93 34
        return $this->g->current();
94
    }
95
96
    /**
97
     * Send value into generator.
98
     * @param mixed $value
99
     * @NOTE: This method returns nothing,
100
     *        while original generator returns something.
101
     */
102 30 View Code Duplication
    public function send($value)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103 30
    {
104 30
        $this->validateValidity();
105
        try {
106 30
            $this->g->send($value);
107 27
            return;
108 19
        } catch (\Throwable $e) {} catch (\Exception $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
109 19
        $this->e = $e;
0 ignored issues
show
Documentation Bug introduced by
It seems like $e of type object<Throwable> or object<Exception> is incompatible with the declared type object<RuntimeException> of property $e.

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...
110 19
    }
111
112
    /**
113
     * Throw exception into generator.
114
     * @param \Throwable|\RuntimeException $e
115
     * @NOTE: This method returns nothing,
116
     *        while original generator returns something.
117
     */
118 19 View Code Duplication
    public function throw_($e)
2 ignored issues
show
Unused Code introduced by
The parameter $e is not used and could be removed.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119 19
    {
120 19
        $this->validateValidity();
121
        try {
122 19
            $this->g->throw($e);
123 15
            return;
124 13
        } catch (\Throwable $e) {} catch (\Exception $e) {}
1 ignored issue
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
125 13
        $this->e = $e;
0 ignored issues
show
Documentation Bug introduced by
It seems like $e of type object<Throwable> or object<Exception> is incompatible with the declared type object<RuntimeException> of property $e.

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...
126 13
    }
127
128
    /**
129
     * Return whether Throwable is thrown.
130
     * @return bool
131
     */
132 30
    public function thrown()
133 30
    {
134 30
        return $this->e !== null;
135
    }
136
137
    /**
138
     * Return value that generator has returned or thrown.
139
     * @return mixed
140
     */
141 33
    public function getReturnOrThrown()
142 33
    {
143 33
        $this->validateInvalidity();
144 32
        if ($this->e === null && $this->g->valid() && !$this->valid()) {
145 9
            return $this->g->current();
146
        }
147 31
        if ($this->e) {
148 23
            return $this->e;
149
        }
150 28
        return method_exists($this->g, 'getReturn') ? $this->g->getReturn() : null;
151
    }
152
153
    /**
154
     * Validate that generator has finished running.
155
     * @throws LogicException
156
     */
157 35
    private function validateValidity()
158 35
    {
159 35
        if (!$this->valid()) {
160 2
            throw new \BadMethodCallException('Unreachable here.');
161
        }
162 35
    }
163
164
    /**
165
     * Validate that generator is still running.
166
     * @throws LogicException
167
     */
168 33
    private function validateInvalidity()
169 33
    {
170 33
        if ($this->valid()) {
171 1
            throw new \BadMethodCallException('Unreachable here.');
172
        }
173 32
    }
174
}
175