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

InputInvocationBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 85
ccs 20
cts 22
cp 0.9091
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A onCompile() 0 9 1
A getParent() 0 4 1
A __sleep() 0 7 1
A getTypeDefinition() 0 12 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\SDL\Reflection\Builder\Invocations;
11
12
use Railt\Parser\Ast\NodeInterface;
13
use Railt\Parser\Ast\RuleInterface;
14
use Railt\SDL\Base\Invocations\BaseInputInvocation;
15
use Railt\SDL\Contracts\Definitions\InputDefinition;
16
use Railt\SDL\Contracts\Definitions\TypeDefinition;
17
use Railt\SDL\Contracts\Dependent\ArgumentDefinition;
18
use Railt\SDL\Reflection\Builder\DocumentBuilder;
19
use Railt\SDL\Reflection\Builder\Process\Compilable;
20
use Railt\SDL\Reflection\Builder\Process\Compiler;
21
22
/**
23
 * Class InputInvocationBuilder
24
 * @property ArgumentDefinition $parent
25
 */
26
class InputInvocationBuilder extends BaseInputInvocation implements Compilable
27
{
28
    use Compiler;
29
30
    /**
31
     * @var array
32
     */
33
    protected $path = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $parentType;
39
40
    /**
41
     * InputInvocationBuilder constructor.
42
     * @param NodeInterface $ast
43
     * @param DocumentBuilder $document
44
     * @param string $parentType
45
     * @param array $path
46
     */
47 798
    public function __construct(NodeInterface $ast, DocumentBuilder $document, string $parentType, array $path)
48
    {
49 798
        $this->path       = $path;
50 798
        $this->parentType = $parentType;
51 798
        $this->boot($ast, $document);
52 798
    }
53
54
    /**
55
     * @return string
56
     */
57 36
    public function getName(): string
58
    {
59 36
        return $this->parentType;
60
    }
61
62
    /**
63
     * @param NodeInterface|RuleInterface $ast
64
     * @return bool
65
     */
66 222
    protected function onCompile(NodeInterface $ast): bool
67
    {
68 222
        $key   = (string)$ast->getChild(0)->getChild(0)->getValue();
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...
69 222
        $value = $ast->getChild(1)->getChild(0);
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...
70
71 222
        $this->arguments[$key] = $this->parseValue($value, $this->parentType, \array_merge($this->path, [$key]));
72
73 222
        return true;
74
    }
75
76
    /**
77
     * @return TypeDefinition
78
     */
79 282
    public function getParent(): TypeDefinition
80
    {
81 282
        return $this->load($this->parentType);
82
    }
83
84
    /**
85
     * @return array
86
     */
87 68
    public function __sleep(): array
88
    {
89 68
        return \array_merge(parent::__sleep(), [
90 68
            'path',
91
            'parentType',
92
        ]);
93
    }
94
95
    /**
96
     * @return null|TypeDefinition
97
     */
98
    public function getTypeDefinition(): ?TypeDefinition
99
    {
100 282
        $reduce = function (?InputDefinition $carry, $item): ?TypeDefinition {
101
            /** @var ArgumentDefinition|null $argument */
102
            $argument = $carry->getArgument($item);
103
            // TODO $argument can be null. Add validation?
104
105
            return $argument ? $argument->getTypeDefinition() : null;
106 282
        };
107
108 282
        return \array_reduce($this->path, $reduce, $this->getParent());
109
    }
110
}
111