Completed
Push — master ( 5ac91e...39483e )
by Kirill
38:30
created

Reason   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getTypeDefinition() 0 4 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\Standard\Directives\Deprecation;
11
12
use Railt\SDL\Base\Dependent\BaseArgument;
13
use Railt\SDL\Contracts\Definitions\DirectiveDefinition;
14
use Railt\SDL\Contracts\Definitions\TypeDefinition;
15
use Railt\SDL\Contracts\Document;
16
use Railt\SDL\Standard\Directives\Deprecation;
17
use Railt\SDL\Standard\GraphQLDocument;
18
19
/**
20
 * Class Reason
21
 */
22
class Reason extends BaseArgument
23
{
24
    private const ARGUMENT_TYPE = 'String';
25
26
    private const ARGUMENT_DEFAULT_VALUE = 'No longer supported';
27
28
    private const ARGUMENT_DESCRIPTION = 'You can either supply a reason argument ' .
29
        'with a string value or not supply one and receive a "No longer supported" ' .
30
        'message when introspected';
31
32
    /**
33
     * Reason constructor.
34
     * @param Document|GraphQLDocument $document
35
     * @param DirectiveDefinition $type
36
     */
37 283
    public function __construct(Document $document, DirectiveDefinition $type)
38
    {
39 283
        $this->parent      = $type;
40 283
        $this->document    = $document;
41 283
        $this->name        = Deprecation::REASON_ARGUMENT;
42 283
        $this->description = self::ARGUMENT_DESCRIPTION;
43
44 283
        $this->defaultValue    = self::ARGUMENT_DEFAULT_VALUE;
45 283
        $this->hasDefaultValue = true;
46 283
    }
47
48
    /**
49
     * @return TypeDefinition
50
     */
51 12
    public function getTypeDefinition(): TypeDefinition
52
    {
53 12
        return $this->document->getDictionary()->get(self::ARGUMENT_TYPE, $this);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\SDL\Contracts\Document as the method getDictionary() does only exist in the following implementations of said interface: Railt\SDL\Standard\GraphQLDocument.

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
    }
55
}
56