Test Failed
Push — master ( 3e22f3...90cc1b )
by Kirill
03:50
created

DirectiveNode::getDirectiveArguments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 10
ccs 0
cts 9
cp 0
crap 12
rs 9.9332
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\Compiler\Ast\Dependent;
11
12
use Railt\Parser\Ast\Rule;
13
use Railt\Reflection\Contracts\Invocation\TypeInvocation;
14
use Railt\Reflection\Document;
15
use Railt\Reflection\Invocation\DirectiveInvocation;
16
17
/**
18
 * Class DirectiveNode
19
 */
20
class DirectiveNode extends Rule
21
{
22
    /**
23
     * @param Document $document
24
     * @return TypeInvocation|DirectiveInvocation
25
     */
26
    public function getTypeInvocation(Document $document): TypeInvocation
27
    {
28
        $directive = new DirectiveInvocation($document, $this->getDirectiveName());
29
        $directive->withOffset($this->getOffset());
30
31
        return $directive;
32
    }
33
34
    /**
35
     * @return string
36
     */
37
    public function getDirectiveName(): string
38
    {
39
        return $this->first('TypeName', 1)->getTypeName();
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 getTypeName() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\D...DirectiveDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\EnumDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\InputDefinitionNode, Railt\SDL\Compiler\Ast\D...nputUnionDefinitionNode, Railt\SDL\Compiler\Ast\D...InterfaceDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ObjectDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ScalarDefinitionNode, Railt\SDL\Compiler\Ast\D...on\SchemaDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\TypeDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\UnionDefinitionNode, Railt\SDL\Compiler\Ast\TypeHintNode, Railt\SDL\Compiler\Ast\TypeNameNode.

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...
40
    }
41
42
    /**
43
     * @return iterable|DirectiveArgumentNode[]
44
     */
45
    public function getDirectiveArguments(): iterable
46
    {
47
        $arguments = $this->first('DirectiveArguments', 1);
48
49
        if ($arguments) {
50
            foreach ($arguments as $argument) {
0 ignored issues
show
Bug introduced by
The expression $arguments of type object<Railt\Parser\Ast\NodeInterface> is not traversable.
Loading history...
51
                yield $argument;
52
            }
53
        }
54
    }
55
}
56