Completed
Push — master ( 296743...12eab9 )
by Kirill
36:17
created

DirectiveDefinitionValidator::isSDLLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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\Reflection\Validation\Definitions;
11
12
use Railt\SDL\Contracts\Definitions\Definition;
13
use Railt\SDL\Contracts\Definitions\Directive\Location;
14
use Railt\SDL\Contracts\Definitions\DirectiveDefinition;
15
use Railt\SDL\Exceptions\TypeConflictException;
16
17
/**
18
 * Class DirectiveDefinitionValidator
19
 */
20
class DirectiveDefinitionValidator extends BaseDefinitionValidator
21
{
22
    /**
23
     * @param Definition $definition
24
     * @return bool
25
     */
26 6548
    public function match(Definition $definition): bool
27
    {
28 6548
        return $definition instanceof DirectiveDefinition;
29
    }
30
31
    /**
32
     * @param Definition|DirectiveDefinition $definition
33
     * @return void
34
     */
35 2436
    public function validate(Definition $definition): void
36
    {
37 2436
        $this->getCallStack()->push($definition);
38
39 2436
        $locations = $definition->getLocations();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\SDL\Contracts\Definitions\Definition as the method getLocations() does only exist in the following implementations of said interface: Railt\SDL\Base\Definitions\BaseDirective, Railt\SDL\Reflection\Bui...itions\DirectiveBuilder, Railt\SDL\Standard\Directives\Deprecation.

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 2436
        foreach ($locations as $location) {
42 2436
            $isValidLocation = $this->isSDLLocation($location) || $this->isQueryLocation($location);
43
44 2436
            if (! $isValidLocation) {
45 6
                $error = \vsprintf('Trying to define directive %s, but %s location is invalid', [
46 6
                    $definition,
47 6
                    $location,
48
                ]);
49 2436
                throw new TypeConflictException($error, $this->getCallStack());
50
            }
51
        }
52
53 2430
        $this->getCallStack()->pop();
54 2430
    }
55
56
    /**
57
     * @param string $location
58
     * @return bool
59
     */
60 2436
    private function isSDLLocation(string $location): bool
61
    {
62 2436
        return \in_array($location, Location::TARGET_GRAPHQL_SDL, true);
63
    }
64
65
    /**
66
     * @param string $location
67
     * @return bool
68
     */
69 6
    private function isQueryLocation(string $location): bool
70
    {
71 6
        return \in_array($location, Location::TARGET_GRAPHQL_QUERY, true);
72
    }
73
}
74