Completed
Push — master ( c1a156...8cadde )
by Kirill
08:14
created

ArgumentSystem::resolve()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 19
loc 19
ccs 11
cts 12
cp 0.9167
crap 4.0092
rs 9.6333
c 0
b 0
f 0
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\Compiler\System\Provider;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\Reflection\Contracts\Definition;
14
use Railt\Reflection\Definition\Behaviour\HasArguments;
15
use Railt\SDL\Ast\ProvidesArgumentNodes;
16
use Railt\SDL\Compiler\System\System;
17
use Railt\SDL\Exception\TypeConflictException;
18
19
/**
20
 * Class ArgumentSystem
21
 */
22 View Code Duplication
class ArgumentSystem extends System
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * @param Definition|HasArguments $definition
26
     * @param RuleInterface $ast
27
     */
28 119
    public function resolve(Definition $definition, RuleInterface $ast): void
29
    {
30 119
        if ($ast instanceof ProvidesArgumentNodes) {
31 6
            foreach ($ast->getArgumentNodes() as $child) {
32 6
                $this->deferred(function () use ($definition, $child) {
33
                    /** @var Definition\Dependent\ArgumentDefinition $argument */
34 6
                    $argument = $this->process->build($child, $definition);
35
36 6
                    $this->linker(function () use ($definition, $argument) {
37 6
                        if ($definition->hasArgument($argument->getName())) {
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 hasArgument() does only exist in the following implementations of said interface: Railt\Reflection\Definit...pendent\FieldDefinition, Railt\Reflection\Definition\DirectiveDefinition, Railt\Reflection\Invocation\DirectiveInvocation, Railt\Reflection\Invocation\InputInvocation, 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...
38
                            throw $this->redeclareException($argument);
39
                        }
40
41 6
                        $definition->withArgument($argument);
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 withArgument() does only exist in the following implementations of said interface: Railt\Reflection\Definit...pendent\FieldDefinition, Railt\Reflection\Definition\DirectiveDefinition, Railt\Reflection\Invocation\DirectiveInvocation, Railt\Reflection\Invocation\InputInvocation, 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...
42 6
                    });
43 6
                });
44
            }
45
        }
46 119
    }
47
}
48