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

TypeDefinitionBuilder::reduce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 16
ccs 0
cts 11
cp 0
crap 6
rs 9.7333
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\Builder;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\SDL\Exception\InvalidArgumentException;
14
use Railt\SDL\Frontend\AST\TypeNameNode;
15
use Railt\SDL\Frontend\Builder\Definition\Definition;
16
use Railt\SDL\Frontend\Context\ContextInterface;
17
use Railt\SDL\Frontend\Matcher;
18
use Railt\SDL\Frontend\Parser;
19
use Railt\SDL\IR\SymbolTable\ValueInterface;
20
use Railt\SDL\IR\Type;
21
use Railt\SDL\IR\Type\Name;
22
use Railt\SDL\IR\Type\TypeNameInterface;
23
24
/**
25
 * Class TypeDefinitionHeaderBuilder
26
 */
27
class TypeDefinitionBuilder extends BaseBuilder
28
{
29
    /**
30
     * @param RuleInterface $rule
31
     * @return bool
32
     */
33
    public function match(RuleInterface $rule): bool
34
    {
35
        return $rule->getName() === 'TypeDefinition';
36
    }
37
38
    /**
39
     * @param ContextInterface $ctx
40
     * @param RuleInterface $rule
41
     * @return \Generator|Definition
42
     * @throws InvalidArgumentException
43
     */
44
    public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator
45
    {
46
        /** @var TypeNameInterface $name */
47
        $name = yield $rule->first('> #TypeName');
48
49
        $definition = new Definition($ctx, $name);
50
51
        foreach ($rule->find('> #GenericDefinitionArgument') as $argument) {
52
            yield from $name = $this->getArgumentName($argument);
53
            yield from $value = $this->getArgumentValue($argument);
54
55
            $definition->addArgument((string)$name->getReturn(), $value->getReturn());
56
        }
57
58
        return $definition;
59
    }
60
61
    /**
62
     * @param RuleInterface $argument
63
     * @return \Generator|TypeNameInterface
64
     */
65
    private function getArgumentName(RuleInterface $argument): \Generator
66
    {
67
        /** @var TypeNameInterface $name */
68
        return yield $argument->first('> #GenericDefinitionArgumentName')->getChild(0);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\NodeInterface as the method getChild() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\Delegate\IncludeDelegate, Railt\Compiler\Grammar\Delegate\RuleDelegate, Railt\Compiler\Grammar\Delegate\TokenDelegate, Railt\Parser\Ast\Rule, Railt\SDL\Frontend\AST\Value\AbstractAstValueNode, Railt\SDL\Frontend\AST\Value\ConstantValueNode, Railt\SDL\Frontend\AST\Value\NullValueNode, Railt\SDL\Frontend\AST\Value\NumberValueNode, Railt\SDL\Frontend\AST\Value\StringValueNode.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
69
    }
70
71
    /**
72
     * @param RuleInterface $argument
73
     * @return \Generator|TypeNameInterface
74
     */
75
    private function getArgumentValue(RuleInterface $argument): \Generator
76
    {
77
        /** @var ValueInterface|TypeNameInterface $value */
78
        return yield $argument->first('> #GenericDefinitionArgumentValue')->getChild(0);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\NodeInterface as the method getChild() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\Delegate\IncludeDelegate, Railt\Compiler\Grammar\Delegate\RuleDelegate, Railt\Compiler\Grammar\Delegate\TokenDelegate, Railt\Parser\Ast\Rule, Railt\SDL\Frontend\AST\Value\AbstractAstValueNode, Railt\SDL\Frontend\AST\Value\ConstantValueNode, Railt\SDL\Frontend\AST\Value\NullValueNode, Railt\SDL\Frontend\AST\Value\NumberValueNode, Railt\SDL\Frontend\AST\Value\StringValueNode.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
79
    }
80
}
81