Passed
Push — master ( 3d645a...097498 )
by Tomáš
05:27
created

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

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
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 57
loc 57
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A MutationFieldConverter.php$0 ➔ __construct() 0 4 1
A MutationFieldConverter.php$0 ➔ getDescription() 0 3 1
A MutationFieldConverter.php$0 ➔ getArgs() 0 3 1
A MutationFieldConverter.php$0 ➔ getName() 0 3 1
A MutationFieldConverter.php$0 ➔ resolve() 0 3 1
A MutationFieldConverter.php$0 ➔ getType() 0 3 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\Mutation\MutationFieldInterface;
9
10
class MutationFieldConverter
11
{
12 4
	public static function toArray(MutationFieldInterface $mutationField): array
13
	{
14
		return [
15 4
			$mutationField->getName() => [
16 4
				'type' => $mutationField->getType(),
17 4
				'description' => $mutationField->getDescription(),
18 4
				'args' => $mutationField->getArgs(),
19
				'resolve' => function ($root, $args, $context) use ($mutationField) {
20 2
					return call_user_func_array([$mutationField, 'resolve'], [$root, $args, $context]);
21 4
				},
22
			],
23
		];
24
	}
25
26
	public static function toObject(array $data): MutationFieldInterface
27
	{
28
		return new class($data) implements MutationFieldInterface {
29
			/**
30
			 * @var string
31
			 */
32
			private $name;
33
34
			/**
35
			 * @var array
36
			 */
37
			private $data = [];
38
39 1
			public function __construct(array $data)
40
			{
41 1
				$this->name = (string) key($data);
42 1
				$this->data = (array) reset($data);
43 1
			}
44
45
			/**
46
			 * {@inheritdoc}
47
			 */
48 1
			public function getName(): string
49
			{
50 1
				return $this->name;
51
			}
52
53
			/**
54
			 * {@inheritdoc}
55
			 */
56 1
			public function getDescription(): string
57
			{
58 1
				return $this->data['description'];
59
			}
60
61
			/**
62
			 * {@inheritdoc}
63
			 */
64 1
			public function getArgs(): array
65
			{
66 1
				return $this->data['args'];
67
			}
68
69
			/**
70
			 * {@inheritdoc}
71
			 */
72 1
			public function getType(): Type
73
			{
74 1
				return $this->data['type'];
75
			}
76
77
			/**
78
			 * {@inheritdoc}
79
			 */
80 1
			public function resolve(array $root, array $args, $context = null)
81
			{
82 1
				return $this->data['resolve']($root, $args, $context);
83
			}
84
		};
85
	}
86
}
87