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

TypeInvocationBuilder   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 4 1
A reduce() 0 11 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\Value;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\SDL\Frontend\Builder\BaseBuilder;
14
use Railt\SDL\Frontend\Context\ContextInterface;
15
use Railt\SDL\IR\SymbolTable\TypeInvocationPrimitive;
16
use Railt\SDL\IR\SymbolTable\Value;
17
use Railt\SDL\IR\SymbolTable\ValueInterface;
18
use Railt\SDL\IR\Type;
19
use Railt\SDL\IR\Type\Name;
20
21
/**
22
 * Class TypeInvocationBuilder
23
 */
24
class TypeInvocationBuilder extends BaseBuilder
25
{
26
    /**
27
     * @param RuleInterface $rule
28
     * @return bool
29
     */
30
    public function match(RuleInterface $rule): bool
31
    {
32
        return $rule->getName() === 'TypeInvocation';
33
    }
34
35
    /**
36
     * @param ContextInterface $ctx
37
     * @param RuleInterface $rule
38
     * @return \Generator|ValueInterface
39
     */
40
    public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator
41
    {
42
        /** @var ValueInterface $value */
43
        $value = yield $rule->first('> #GenericInvocationName')->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...
44
45
        $name = Name::new((string)$value->getValue());
46
47
        // TODO Add arguments
48
49
        return new Value(new TypeInvocationPrimitive($name, $ctx), Type::type());
0 ignored issues
show
Bug introduced by
The method type() does not seem to exist on object<Railt\SDL\IR\Type>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
    }
51
}
52