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

DirectiveBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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\Builder\Definitions;
11
12
use Railt\Parser\Ast\NodeInterface;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\SDL\Base\Definitions\BaseDirective;
15
use Railt\SDL\Reflection\Builder\Dependent\Argument\ArgumentsBuilder;
16
use Railt\SDL\Reflection\Builder\DocumentBuilder;
17
use Railt\SDL\Reflection\Builder\Process\Compilable;
18
use Railt\SDL\Reflection\Builder\Process\Compiler;
19
use Railt\SDL\Reflection\Validation\Uniqueness;
20
21
/**
22
 * Class DirectiveBuilder
23
 */
24
class DirectiveBuilder extends BaseDirective implements Compilable
25
{
26
    use Compiler;
27
    use ArgumentsBuilder;
28
29
    /**
30
     * DirectiveBuilder constructor.
31
     * @param NodeInterface $ast
32
     * @param DocumentBuilder $document
33
     * @throws \Railt\SDL\Exceptions\TypeConflictException
34
     */
35 3540
    public function __construct(NodeInterface $ast, DocumentBuilder $document)
36
    {
37 3540
        $this->boot($ast, $document);
38 3540
        $this->offset = $this->offsetPrefixedBy('directive');
39 3540
    }
40
41
    /**
42
     * @param NodeInterface|RuleInterface $ast
43
     * @return bool
44
     * @throws \OutOfBoundsException
45
     */
46 3540
    protected function onCompile(NodeInterface $ast): bool
47
    {
48 3540
        switch ($ast->getName()) {
49 3540
            case 'Target':
50 2436
                $validator = $this->getValidator(Uniqueness::class);
51
52 2436
                foreach ($ast->getChild(0)->getChildren() as $child) {
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 getChild() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\Delegate\IncludeDelegate, Railt\Compiler\Grammar\Delegate\RuleDelegate, Railt\Compiler\Grammar\Delegate\TokenDelegate, Railt\Parser\Ast\Rule.

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...
53 2436
                    $location = $child->getValue();
54
55 2436
                    $validator->validate($this->locations, $child->getValue(), static::LOCATION_TYPE_NAME);
56
57 2436
                    $this->locations[] = $location;
58
                }
59
        }
60
61 3540
        return false;
62
    }
63
}
64