Completed
Pull Request — master (#2)
by Tomáš
01:43
created

anonymous//src/Converter/QueryFieldConverter.php$0   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 66
loc 66
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A QueryFieldConverter.php$0 ➔ __construct() 5 5 1
A QueryFieldConverter.php$0 ➔ getName() 4 4 1
A QueryFieldConverter.php$0 ➔ getType() 4 4 1
A QueryFieldConverter.php$0 ➔ getDescription() 4 4 1
A QueryFieldConverter.php$0 ➔ getArgs() 4 4 1
A QueryFieldConverter.php$0 ➔ resolve() 4 4 1

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
declare(strict_types = 1);
4
5
namespace Portiny\GraphQL\Converter;
6
7
use GraphQL\Type\Definition\Type;
8
use Portiny\GraphQL\Contract\Field\QueryFieldInterface;
9
10
11 View Code Duplication
final class QueryFieldConverter
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...
12
{
13
14 3
	public static function toArray(QueryFieldInterface $queryField): array
15
	{
16
		return [
17 3
			$queryField->getName() => [
18 3
				'type' => $queryField->getType(),
19 3
				'description' => $queryField->getDescription(),
20 3
				'args' => $queryField->getArgs(),
21 3
				'resolve' => function ($root, $args, $context) use ($queryField) {
22 1
					return call_user_func_array([$queryField, 'resolve'], [$root, $args, $context]);
23 3
				}
24
			]
25
		];
26
	}
27
28
29
	public static function toObject(array $data): QueryFieldInterface
30
	{
31
		return (new class ($data) implements QueryFieldInterface
32
		{
33
34
			/**
35
			 * @var string
36
			 */
37
			private $name;
38
39
			/**
40
			 * @var array
41
			 */
42
			private $data;
43
44
45 1
			public function __construct(array $data)
46
			{
47 1
				$this->name = key($data);
48 1
				$this->data = reset($data);
49 1
			}
50
51
52
			/**
53
			 * {@inheritdoc}
54
			 */
55 1
			public function getName(): string
56
			{
57 1
				return $this->name;
58
			}
59
60
61
			/**
62
			 * {@inheritdoc}
63
			 */
64 1
			public function getType(): Type
65
			{
66 1
				return $this->data['type'];
67
			}
68
69
70
			/**
71
			 * {@inheritdoc}
72
			 */
73 1
			public function getDescription(): string
74
			{
75 1
				return $this->data['description'];
76
			}
77
78
79
			/**
80
			 * {@inheritdoc}
81
			 */
82 1
			public function getArgs(): array
83
			{
84 1
				return $this->data['args'];
85
			}
86
87
88
			/**
89
			 * {@inheritdoc}
90
			 */
91 1
			public function resolve(array $root, array $args, $context = NULL)
92
			{
93 1
				return $this->data['resolve']($root, $args, $context);
94
			}
95
96
		});
97
	}
98
99
}
100