Test Failed
Push — master ( 6bac61...6350a6 )
by Kirill
03:02
created

DefinitionSystem::match()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
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\System;
11
12
use Railt\Io\Readable;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\SDL\Frontend\AST\ProvidesName;
15
use Railt\SDL\Frontend\AST\ProvidesType;
16
use Railt\SDL\Frontend\IR\Opcode;
17
use Railt\SDL\Frontend\IR\Prototype;
18
19
/**
20
 * Class DefinitionSystem
21
 */
22
class DefinitionSystem implements SystemInterface
23
{
24
    /**
25
     * @param RuleInterface $ast
26
     * @return bool
27
     */
28
    public function match(RuleInterface $ast): bool
29
    {
30
        return $ast instanceof ProvidesType;
31
    }
32
33
    /**
34
     * @param Readable $file
35
     * @param RuleInterface|ProvidesType|ProvidesName $ast
36
     * @return \Generator|mixed
37
     */
38
    public function apply(Readable $file, RuleInterface $ast)
39
    {
40
        yield new Prototype($ast->getOffset(), Opcode::C_TYPE_DEFINITION, [
0 ignored issues
show
Documentation introduced by
array($ast->getType(), $ast->getFullName()) is of type array<integer,?,{"0":"?","1":"?"}>, but the function expects a object<Railt\SDL\Frontend\IR\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
            $ast->getType(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getType() does only exist in the following implementations of said interface: Railt\SDL\Frontend\AST\D...DirectiveDefinitionNode, Railt\SDL\Frontend\AST\D...tion\EnumDefinitionNode, Railt\SDL\Frontend\AST\D...ion\InputDefinitionNode, Railt\SDL\Frontend\AST\D...InterfaceDefinitionNode, Railt\SDL\Frontend\AST\D...on\ObjectDefinitionNode, Railt\SDL\Frontend\AST\D...on\ScalarDefinitionNode, Railt\SDL\Frontend\AST\D...on\SchemaDefinitionNode, Railt\SDL\Frontend\AST\D...tion\TypeDefinitionNode, Railt\SDL\Frontend\AST\D...ion\UnionDefinitionNode, Railt\SDL\Frontend\AST\D...\ArgumentDefinitionNode, Railt\SDL\Frontend\AST\D...ndentTypeDefinitionNode, Railt\SDL\Frontend\AST\D...EnumValueDefinitionNode, Railt\SDL\Frontend\AST\D...ent\FieldDefinitionNode, Railt\SDL\Frontend\AST\D...nputFieldDefinitionNode, Railt\SDL\Frontend\AST\I...DirectiveInvocationNode, Railt\SDL\Frontend\AST\I...ion\InputInvocationNode.

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...
42
            $ast->getFullName()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getFullName() does only exist in the following implementations of said interface: Railt\SDL\Frontend\AST\Common\TypeHintNode, Railt\SDL\Frontend\AST\Common\TypeNameNode, Railt\SDL\Frontend\AST\D...DirectiveDefinitionNode, Railt\SDL\Frontend\AST\D...tion\EnumDefinitionNode, Railt\SDL\Frontend\AST\D...ion\InputDefinitionNode, Railt\SDL\Frontend\AST\D...InterfaceDefinitionNode, Railt\SDL\Frontend\AST\D...on\ObjectDefinitionNode, Railt\SDL\Frontend\AST\D...on\ScalarDefinitionNode, Railt\SDL\Frontend\AST\D...on\SchemaDefinitionNode, Railt\SDL\Frontend\AST\D...tion\TypeDefinitionNode, Railt\SDL\Frontend\AST\D...ion\UnionDefinitionNode, Railt\SDL\Frontend\AST\D...\ArgumentDefinitionNode, Railt\SDL\Frontend\AST\D...ndentTypeDefinitionNode, Railt\SDL\Frontend\AST\D...EnumValueDefinitionNode, Railt\SDL\Frontend\AST\D...ent\FieldDefinitionNode, Railt\SDL\Frontend\AST\D...nputFieldDefinitionNode, Railt\SDL\Frontend\AST\I...\ArgumentInvocationNode, Railt\SDL\Frontend\AST\I...DirectiveInvocationNode.

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...
43
        ]);
44
    }
45
}
46