QueryFieldConverter.php$0 ➔ toObject()   B
last analyzed

Complexity

Conditions 1

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 63
cc 1
rs 8.8072
ccs 14
cts 14
cp 1
crap 1

6 Methods

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

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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