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

FieldsBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 61.9 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 13
loc 21
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A compileFieldsBuilder() 13 13 3

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\Reflection\Builder\Dependent\Field;
11
12
use Railt\Parser\Ast\NodeInterface;
13
use Railt\SDL\Contracts\Definitions\TypeDefinition;
14
use Railt\SDL\Contracts\Dependent\Field\HasFields;
15
use Railt\SDL\Exceptions\TypeConflictException;
16
use Railt\SDL\Reflection\Builder\Dependent\FieldBuilder;
17
use Railt\SDL\Reflection\Builder\Process\Compiler;
18
19
/**
20
 * Trait FieldsBuilder
21
 *
22
 * @mixin Compiler
23
 */
24
trait FieldsBuilder
25
{
26
    /**
27
     * @param NodeInterface $ast
28
     * @return bool
29
     * @throws TypeConflictException
30
     */
31 3389 View Code Duplication
    protected function compileFieldsBuilder(NodeInterface $ast): bool
0 ignored issues
show
Duplication introduced by
This method 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...
32
    {
33
        /** @var TypeDefinition|HasFields $this */
34 3389
        if ($this instanceof HasFields && $ast->is('Field')) {
35 3089
            $field = new FieldBuilder($ast, $this->getDocument(), $this);
36
37 1982
            $this->fields = $this->unique($this->fields, $field);
0 ignored issues
show
Bug introduced by
Accessing fields on the interface Railt\SDL\Contracts\Dependent\Field\HasFields suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\SDL\Contracts\Dependent\Field\HasFields as the method unique() does only exist in the following implementations of said interface: Railt\SDL\Reflection\Bui...itions\InterfaceBuilder, Railt\SDL\Reflection\Bui...finitions\ObjectBuilder.

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
39 1982
            return true;
40
        }
41
42 3389
        return false;
43
    }
44
}
45