Test Failed
Push — master ( 08713d...9fa0b4 )
by Kirill
03:13
created

ValueInvocation   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 108
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invoke() 0 13 3
A checkRecursiveReferring() 0 9 2
A has() 0 12 3
A getStackAsString() 0 4 1
A push() 0 4 1
A invokeInput() 0 19 5
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\Compiler\Builder\Common;
11
12
use Railt\Reflection\Contracts\Definition;
13
use Railt\Reflection\Contracts\Definition\InputDefinition;
14
use Railt\Reflection\Contracts\Invocation\InputInvocation;
15
use Railt\Reflection\Contracts\Invocation\TypeInvocation;
16
use Railt\SDL\Exception\SemanticException;
17
use Railt\SDL\Exception\TypeConflictException;
18
19
/**
20
 * Class ValueInvocation
21
 */
22
class ValueInvocation
23
{
24
    /**
25
     * @var \SplQueue
26
     */
27
    private $stack;
28
29
    /**
30
     * ValueInvocation constructor.
31
     */
32
    public function __construct()
33
    {
34
        $this->stack = new \SplQueue();
35
    }
36
37
    /**
38
     * @param mixed $value
39
     * @return mixed
40
     * @throws \Railt\Io\Exception\ExternalFileException
41
     * @throws \InvalidArgumentException
42
     */
43
    public function invoke($value)
44
    {
45
        if ($value instanceof Definition) {
46
            $this->checkRecursiveReferring($value);
47
            $this->push($value);
48
        }
49
50
        if ($value instanceof InputInvocation) {
0 ignored issues
show
Bug introduced by
The class Railt\Reflection\Contrac...ocation\InputInvocation does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
51
            return $this->invokeInput($value);
52
        }
53
54
        return $value;
55
    }
56
57
    /**
58
     * @param Definition $value
59
     * @throws \Railt\Io\Exception\ExternalFileException
60
     */
61
    private function checkRecursiveReferring(Definition $value): void
62
    {
63
        if ($this->has($value)) {
64
            $error = 'Recursive reference (%s -> %s) detected while %s invocation';
65
            $error = \sprintf($error, $this->getStackAsString(), $value, $value);
66
67
            throw (new SemanticException($error))->throwsIn($value->getFile(), $value->getLine(), $value->getColumn());
68
        }
69
    }
70
71
    /**
72
     * @param Definition $definition
73
     * @return bool
74
     */
75
    private function has(Definition $definition): bool
76
    {
77
        $stack = clone $this->stack;
78
79
        foreach ($stack as $i) {
80
            if ((string)$i === (string)$definition) {
81
                return true;
82
            }
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function getStackAsString(): string
92
    {
93
        return \implode(' -> ', \iterator_to_array(clone $this->stack));
94
    }
95
96
    /**
97
     * @param Definition $definition
98
     */
99
    private function push(Definition $definition): void
100
    {
101
        $this->stack->push($definition);
102
    }
103
104
    /**
105
     * @param InputInvocation $input
106
     * @return InputInvocation
107
     * @throws \InvalidArgumentException
108
     * @throws \Railt\Io\Exception\ExternalFileException
109
     */
110
    private function invokeInput(InputInvocation $input): InputInvocation
111
    {
112
        /** @var InputDefinition $definition */
113
        $definition = $input->getDefinition();
114
115
        foreach ($definition->getFields() as $field) {
116
            $argument = $input->getArgument($field->getName());
117
118
            if ($argument === null && $field->isNonNull() && ! $field->hasDefaultValue()) {
119
                $error = \sprintf('Missing value for required argument %s', $field);
120
                throw (new TypeConflictException($error))->throwsIn($input->getFile(), $input->getLine(),
121
                        $input->getColumn());
122
            }
123
124
            $this->invoke($argument);
125
        }
126
127
        return $input;
128
    }
129
}
130