Completed
Push — master ( 121747...dcf58a )
by Alexandr
02:37
created

Query   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 91
Duplicated Lines 10.99 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 70.37%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 10
loc 91
ccs 19
cts 27
cp 0.7037
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A getName() 0 4 1
A getFields() 0 4 1
A hasFields() 0 4 1
A setFields() 0 7 1
A getAlias() 0 4 1
B hasField() 0 16 6

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
 * Date: 23.11.15
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Parser\Ast;
9
10
use Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface;
11
use Youshido\GraphQL\Parser\Ast\Interfaces\FragmentInterface;
12
use Youshido\GraphQL\Parser\Location;
13
14
class Query extends AbstractAst implements FieldInterface
15
{
16
17
    use AstArgumentsTrait;
18
    use AstDirectivesTrait;
19
20
    /** @var string */
21
    protected $name;
22
23
    /** @var string */
24
    protected $alias;
25
26
    /** @var Field[]|Query[] */
27
    protected $fields = [];
28
29
    /**
30
     * Query constructor.
31
     *
32
     * @param string   $name
33
     * @param string   $alias
34
     * @param array    $arguments
35
     * @param array    $fields
36
     * @param array    $directives
37
     * @param Location $location
38
     */
39 91 View Code Duplication
    public function __construct($name, $alias = '', array $arguments, array $fields, array $directives, Location $location)
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...
40
    {
41 91
        parent::__construct($location);
42
43 91
        $this->name      = $name;
44 91
        $this->alias     = $alias;
45 91
        $this->setFields($fields);
46 91
        $this->setArguments($arguments);
47 91
        $this->setDirectives($directives);
48 91
    }
49
50 66
    public function getName()
51
    {
52 66
        return $this->name;
53
    }
54
55
    /**
56
     * @return Field[]|Query[]|FragmentInterface[]
57
     */
58 60
    public function getFields()
59
    {
60 60
        return array_values($this->fields);
61
    }
62
63
    /**
64
     * @return bool
65
     */
66 22
    public function hasFields()
67
    {
68 22
        return (bool)count($this->fields);
69
    }
70
71
    /**
72
     * @param Field[]|Query[] $fields
73
     */
74 91
    public function setFields($fields)
75
    {
76
        /**
77
         * we cannot store fields by name because of TypedFragments
78
         */
79 91
        $this->fields = $fields;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fields of type array<integer,object<You...phQL\Parser\Ast\Query>> is incompatible with the declared type array<integer,object<You...phQL\Parser\Ast\Query>> of property $fields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80 91
        }
81
82 66
    public function getAlias()
83
    {
84 66
        return $this->alias;
85
    }
86
87
    public function hasField($name, $deep = false)
88
    {
89
        foreach ($this->getFields() as $field) {
90
            if ($field->getName() == $name) {
91
                return true;
92
            }
93
94
            if ($deep && $field instanceof Query) {
95
                if ($field->hasField($name)) {
96
                    return true;
97
                }
98
            }
99
        }
100
101
        return false;
102
    }
103
104
}
105