Test Failed
Pull Request — master (#7)
by David
02:00
created

AbstractQueryProviderTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 87
Duplicated Lines 27.59 %

Importance

Changes 0
Metric Value
wmc 3
dl 24
loc 87
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ __construct() 0 5 1
A hp$0 ➔ mapClassToType() 0 8 2
A hp$0 ➔ mapClassToInputType() 0 8 2
A hp$1 ➔ hydrate() 0 4 1
A getTestObjectType() 12 12 2
A getInputTestObjectType() 12 12 2
B getTypeMapper() 0 40 3
A getHydrator() 0 12 2

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
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
use PHPUnit\Framework\TestCase;
7
use TheCodingMachine\GraphQL\Controllers\Fixtures\TestObject;
8
use Youshido\GraphQL\Type\InputObject\InputObjectType;
9
use Youshido\GraphQL\Type\InputTypeInterface;
10
use Youshido\GraphQL\Type\Object\ObjectType;
11
use Youshido\GraphQL\Type\Scalar\StringType;
12
use Youshido\GraphQL\Type\TypeInterface;
13
14
abstract class AbstractQueryProviderTest extends TestCase
15
{
16
    private $testObjectType;
17
    private $inputTestObjectType;
18
    private $typeMapper;
19
    private $hydrator;
20
21 View Code Duplication
    protected function getTestObjectType()
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...
22
    {
23
        if ($this->testObjectType === null) {
24
            $this->testObjectType = new ObjectType([
25
                'name'    => 'TestObject',
26
                'fields'  => [
27
                    'test'   => new StringType(),
28
                ],
29
            ]);
30
        }
31
        return $this->testObjectType;
32
    }
33
34 View Code Duplication
    protected function getInputTestObjectType()
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...
35
    {
36
        if ($this->inputTestObjectType === null) {
37
            $this->inputTestObjectType = new InputObjectType([
38
                'name'    => 'TestObject',
39
                'fields'  => [
40
                    'test'   => new StringType(),
41
                ],
42
            ]);
43
        }
44
        return $this->inputTestObjectType;
45
    }
46
47
    protected function getTypeMapper()
48
    {
49
        if ($this->typeMapper === null) {
50
            $this->typeMapper = new class($this->getTestObjectType(), $this->getInputTestObjectType()) implements TypeMapperInterface {
51
                /**
52
                 * @var ObjectType
53
                 */
54
                private $testObjectType;
55
                /**
56
                 * @var InputObjectType
57
                 */
58
                private $inputTestObjectType;
59
60
                public function __construct(ObjectType $testObjectType, InputObjectType $inputTestObjectType)
61
                {
62
                    $this->testObjectType = $testObjectType;
63
                    $this->inputTestObjectType = $inputTestObjectType;
64
                }
65
66
                public function mapClassToType(string $className): TypeInterface
67
                {
68
                    if ($className === TestObject::class) {
69
                        return $this->testObjectType;
70
                    } else {
71
                        throw new \RuntimeException('Unexpected type');
72
                    }
73
                }
74
75
                public function mapClassToInputType(string $className): InputTypeInterface
76
                {
77
                    if ($className === TestObject::class) {
78
                        return $this->inputTestObjectType;
79
                    } else {
80
                        throw new \RuntimeException('Unexpected type');
81
                    }
82
                }
83
            };
84
        }
85
        return $this->typeMapper;
86
    }
87
88
    protected function getHydrator()
89
    {
90
        if ($this->hydrator === null) {
91
            $this->hydrator = new class implements HydratorInterface {
92
                public function hydrate(array $data, TypeInterface $type)
93
                {
94
                    return new TestObject($data['test']);
95
                }
96
            };
97
        }
98
        return $this->hydrator;
99
    }
100
}
101