Completed
Push — master ( 8afd90...4bf010 )
by Kirill
03:48
created

InterfaceBuilder   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
dl 34
loc 34
ccs 12
cts 15
cp 0.8
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 25 25 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Builder\Definition;
11
12
use Railt\Parser\Ast\RuleInterface;
13
use Railt\Reflection\Contracts\Definition;
14
use Railt\Reflection\Definition\ObjectDefinition;
15
use Railt\SDL\Compiler\Ast\Definition\InterfaceDefinitionNode;
16
use Railt\SDL\Compiler\Builder\Builder;
17
18
/**
19
 * Class InterfaceBuilder
20
 */
21 View Code Duplication
class InterfaceBuilder extends Builder
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...
22
{
23
    /**
24
     * @param RuleInterface|InterfaceDefinitionNode $rule
25
     * @param Definition $parent
26
     * @return Definition
27
     * @throws \Railt\Io\Exception\ExternalFileException
28
     */
29 1
    public function build(RuleInterface $rule, Definition $parent): Definition
30
    {
31 1
        $interface = new ObjectDefinition($parent->getDocument(), $rule->getTypeName());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getTypeName() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\D...DirectiveDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\EnumDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\InputDefinitionNode, Railt\SDL\Compiler\Ast\D...nputUnionDefinitionNode, Railt\SDL\Compiler\Ast\D...InterfaceDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ObjectDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ScalarDefinitionNode, Railt\SDL\Compiler\Ast\D...on\SchemaDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\TypeDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\UnionDefinitionNode, Railt\SDL\Compiler\Ast\TypeHintNode, Railt\SDL\Compiler\Ast\TypeNameNode.

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...
Compatibility introduced by
$parent->getDocument() of type object<Railt\Reflection\Contracts\Document> is not a sub-type of object<Railt\Reflection\Document>. It seems like you assume a concrete implementation of the interface Railt\Reflection\Contracts\Document to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32
33 1
        $interface->withOffset($rule->getOffset());
34 1
        $interface->withDescription($rule->getDescription());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getDescription() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\D...DirectiveDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\EnumDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\InputDefinitionNode, Railt\SDL\Compiler\Ast\D...nputUnionDefinitionNode, Railt\SDL\Compiler\Ast\D...InterfaceDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ObjectDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ScalarDefinitionNode, Railt\SDL\Compiler\Ast\D...on\SchemaDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\TypeDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\UnionDefinitionNode, Railt\SDL\Compiler\Ast\D...\ArgumentDefinitionNode, Railt\SDL\Compiler\Ast\D...EnumValueDefinitionNode, Railt\SDL\Compiler\Ast\D...ent\FieldDefinitionNode, Railt\SDL\Compiler\Ast\D...nputFieldDefinitionNode.

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...
35
36 1
        foreach ($rule->getFields() as $ast) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getFields() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\D...InterfaceDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ObjectDefinitionNode.

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...
37
            $interface->withField($this->dependent($ast, $interface));
38
        }
39
40 1
        $this->when->runtime(function () use ($rule, $interface) {
41 1
            foreach ($rule->getDirectives() as $ast) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getDirectives() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\D...DirectiveDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\EnumDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\InputDefinitionNode, Railt\SDL\Compiler\Ast\D...nputUnionDefinitionNode, Railt\SDL\Compiler\Ast\D...InterfaceDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ObjectDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ScalarDefinitionNode, Railt\SDL\Compiler\Ast\D...on\SchemaDefinitionNode, Railt\SDL\Compiler\Ast\D...tion\TypeDefinitionNode, Railt\SDL\Compiler\Ast\D...ion\UnionDefinitionNode, Railt\SDL\Compiler\Ast\D...\ArgumentDefinitionNode, Railt\SDL\Compiler\Ast\D...EnumValueDefinitionNode, Railt\SDL\Compiler\Ast\D...ent\FieldDefinitionNode, Railt\SDL\Compiler\Ast\D...nputFieldDefinitionNode.

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
                $interface->withDirective($this->dependent($ast, $interface));
43
            }
44 1
        });
45
46 1
        $this->when->resolving(function () use ($rule, $interface) {
47 1
            foreach ($rule->getImplementations() 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\RuleInterface as the method getImplementations() does only exist in the following implementations of said interface: Railt\SDL\Compiler\Ast\D...InterfaceDefinitionNode, Railt\SDL\Compiler\Ast\D...on\ObjectDefinitionNode.

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...
48
                $interface->withInterface($child->getTypeName());
0 ignored issues
show
Bug introduced by
The method withInterface() does not seem to exist on object<Railt\Reflection\...ition\ObjectDefinition>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
            }
50 1
        });
51
52 1
        return $interface;
53
    }
54
}
55