Test Failed
Push — master ( 33bfdc...47f867 )
by Kirill
02:32
created

TypeDefinitionBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 4 1
A reduce() 0 16 2
A getArgumentName() 0 5 1
A getArgumentValue() 0 5 1
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\Common;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\SDL\Exception\InvalidArgumentException;
14
use Railt\SDL\Frontend\Builder\BaseBuilder;
15
use Railt\SDL\Frontend\Definition\Definition;
16
use Railt\SDL\Frontend\Context\ContextInterface;
17
use Railt\SDL\IR\SymbolTable\ValueInterface;
18
use Railt\SDL\IR\Type\TypeNameInterface;
19
20
/**
21
 * Class TypeDefinitionHeaderBuilder
22
 */
23
class TypeDefinitionBuilder extends BaseBuilder
24
{
25
    /**
26
     * @param RuleInterface $rule
27
     * @return bool
28
     */
29
    public function match(RuleInterface $rule): bool
30
    {
31
        return $rule->getName() === 'TypeDefinition';
32
    }
33
34
    /**
35
     * @param ContextInterface $ctx
36
     * @param RuleInterface $rule
37
     * @return \Generator|Definition
38
     * @throws InvalidArgumentException
39
     */
40
    public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator
41
    {
42
        /** @var TypeNameInterface $name */
43
        $name = yield $rule->first('> #TypeName');
44
45
        $definition = new Definition($ctx, $name);
46
47
        foreach ($rule->find('> #GenericDefinitionArgument') as $argument) {
48
            yield from $name  = $this->getArgumentName($argument);
49
            yield from $value = $this->getArgumentValue($argument);
50
51
            $definition->addArgument((string)$name->getReturn(), $value->getReturn());
52
        }
53
54
        return $definition;
55
    }
56
57
    /**
58
     * @param RuleInterface $argument
59
     * @return \Generator|TypeNameInterface
60
     */
61
    private function getArgumentName(RuleInterface $argument): \Generator
62
    {
63
        /** @var TypeNameInterface $name */
64
        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...
65
    }
66
67
    /**
68
     * @param RuleInterface $argument
69
     * @return \Generator|TypeNameInterface
70
     */
71
    private function getArgumentValue(RuleInterface $argument): \Generator
72
    {
73
        /** @var ValueInterface|TypeNameInterface $value */
74
        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...
75
    }
76
}
77