Test Failed
Push — master ( d820d2...d59132 )
by Kirill
02:27
created

IncludeDirective::isBuiltin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Reflection\Stdlib\Directive;
11
12
use Railt\Reflection\Definition\Dependent\ArgumentDefinition;
13
use Railt\Reflection\Definition\Dependent\DirectiveLocation;
14
use Railt\Reflection\Definition\DirectiveDefinition;
15
use Railt\Reflection\Document;
16
17
/**
18
 * Class IncludeDirective
19
 */
20
final class IncludeDirective extends DirectiveDefinition
21
{
22
    /**
23
     * @var string[]
24
     */
25
    private const LOCATIONS = [
26
        DirectiveLocation::FIELD,
27
        DirectiveLocation::FRAGMENT_SPREAD,
28
        DirectiveLocation::INLINE_FRAGMENT,
29
    ];
30
31
    /**
32
     * @var string
33
     */
34
    public const TYPE_NAME = 'include';
35
36
    /**
37
     * @var string
38
     */
39
    public const TYPE_DESCRIPTION = <<<Description
40
The @include directive may be provided for fields, fragment spreads, and inline
41
fragments, and allows for conditional inclusion during execution as described
42
by the if argument.
43
Description;
44
45
    /**
46
     * BooleanScalar constructor.
47
     * @param Document $document
48
     */
49 9
    public function __construct(Document $document)
50
    {
51 9
        parent::__construct($document, self::TYPE_NAME);
52
53 9
        $this->withDescription(self::TYPE_DESCRIPTION)
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Reflection\Contrac...finition\TypeDefinition as the method withArgument() does only exist in the following implementations of said interface: Railt\Reflection\Definit...pendent\FieldDefinition, Railt\Reflection\Definition\DirectiveDefinition, Railt\Reflection\Stdlib\...ive\DeprecatedDirective, Railt\Reflection\Stdlib\Directive\IncludeDirective, Railt\Reflection\Stdlib\Directive\SkipDirective.

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...
54 9
            ->withArgument((new ArgumentDefinition($this, 'if', 'Boolean'))
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Reflection\Contracts\Definition as the method withModifiers() does only exist in the following implementations of said interface: Railt\Reflection\Definit...dent\ArgumentDefinition, Railt\Reflection\Definit...pendent\FieldDefinition, Railt\Reflection\Definit...nt\InputFieldDefinition.

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...
55 9
                ->withLine(67)
56 9
                ->withModifiers(ArgumentDefinition::IS_NOT_NULL))
57 9
            ->withLocation(...\array_map(function (string $location) use ($document): DirectiveLocation {
58 9
                return new DirectiveLocation($this, $location);
59 9
            }, self::LOCATIONS));
60 9
    }
61
62
    /**
63
     * @return int
64
     */
65
    public function getLine(): int
66
    {
67
        return 56;
68
    }
69
70
    /**
71
     * @return bool
72
     */
73
    public function isBuiltin(): bool
74
    {
75
        return true;
76
    }
77
}
78