Test Failed
Push — master ( b7ab7b...94ee03 )
by Kirill
02:59
created

ScalarValueBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 4 1
A reduce() 0 16 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\Frontend\Builder\Value;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\SDL\Frontend\AST\Value\AstValueInterface;
14
use Railt\SDL\Frontend\Builder\BaseBuilder;
15
use Railt\SDL\Frontend\Context\ContextInterface;
16
use Railt\SDL\IR\SymbolTable\Value;
17
use Railt\SDL\IR\Type;
18
19
/**
20
 * Class ScalarValueBuilder
21
 */
22
class ScalarValueBuilder extends BaseBuilder
23
{
24
    /**
25
     * @var string[]
26
     */
27
    private const SCALARS = [
28
        'NumberValue',
29
        'StringValue',
30
        'NullValue',
31
    ];
32
33
    /**
34
     * @param RuleInterface $rule
35
     * @return bool
36
     */
37
    public function match(RuleInterface $rule): bool
38
    {
39
        return \in_array($rule->getName(), self::SCALARS, true);
40
    }
41
42
    /**
43
     * @param ContextInterface $ctx
44
     * @param RuleInterface|AstValueInterface $rule
45
     * @return mixed|Value
46
     */
47
    public function reduce(ContextInterface $ctx, RuleInterface $rule)
48
    {
49
        \assert($rule instanceof AstValueInterface);
50
51
        switch ($rule->getName()) {
52
            case 'NumberValue':
53
                $value = $rule->toPrimitive();
54
                return new Value($value, \is_int($value) ? Type::int() : Type::float());
0 ignored issues
show
Bug introduced by
It seems like \is_int($value) ? \Railt...lt\SDL\IR\Type::float() can also be of type object<Railt\SDL\IR\Type...untimeTypeConstructors>; however, Railt\SDL\IR\SymbolTable\Value::__construct() does only seem to accept object<Railt\SDL\IR\Type\TypeInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
56
            case 'StringValue':
57
                return new Value($rule->toPrimitive(), Type::string());
0 ignored issues
show
Bug introduced by
It seems like \Railt\SDL\IR\Type::string() targeting Railt\SDL\IR\Type\TypeCo...eConstructors::string() can also be of type object<Railt\SDL\IR\Type...untimeTypeConstructors>; however, Railt\SDL\IR\SymbolTable\Value::__construct() does only seem to accept object<Railt\SDL\IR\Type\TypeInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
58
59
            case 'NullValue':
60
                return new Value(null, Type::null());
0 ignored issues
show
Bug introduced by
It seems like \Railt\SDL\IR\Type::null() targeting Railt\SDL\IR\Type\TypeCo...ypeConstructors::null() can also be of type object<Railt\SDL\IR\Type...untimeTypeConstructors>; however, Railt\SDL\IR\SymbolTable\Value::__construct() does only seem to accept object<Railt\SDL\IR\Type\TypeInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
        }
62
    }
63
}
64