Completed
Pull Request — master (#204)
by Ryan
11:34
created

CharacterInterface   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 28.89 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 10
dl 13
loc 45
rs 10
c 0
b 0
f 0

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
 * Copyright (c) 2015–2018 Alexandr Viniychuk <http://youshido.com>.
4
 * Copyright (c) 2015–2018 Portey Vasil <https://github.com/portey>.
5
 * Copyright (c) 2018 Ryan Parman <https://github.com/skyzyx>.
6
 * Copyright (c) 2018 Ashley Hutson <https://github.com/asheliahut>.
7
 * Copyright (c) 2015–2018 Contributors.
8
 *
9
 * http://opensource.org/licenses/MIT
10
 */
11
12
declare(strict_types=1);
13
/*
14
 * This file is a part of GraphQL project.
15
 *
16
 * @author Alexandr Viniychuk <[email protected]>
17
 * created: 12/6/15 11:15 PM
18
 */
19
20
namespace Youshido\Tests\StarWars\Schema;
21
22
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
23
use Youshido\GraphQL\Type\ListType\ListType;
24
use Youshido\GraphQL\Type\NonNullType;
25
use Youshido\GraphQL\Type\Scalar\IdType;
26
use Youshido\GraphQL\Type\Scalar\StringType;
27
28
class CharacterInterface extends AbstractInterfaceType
29
{
30
    public function build($config): void
31
    {
32
        $config
33
            ->addField('id', new NonNullType(new IdType()))
34
            ->addField('name', new NonNullType(new StringType()))
35
            ->addField('friends', [
36
                'type'    => new ListType(new self()),
37
                'resolve' => static function ($value) {
38
                    return $value['friends'];
39
                },
40
            ])
41
            ->addField('appearsIn', new ListType(new EpisodeEnum()));
42
    }
43
44
    public function getDescription()
45
    {
46
        return 'A character in the Star Wars Trilogy';
47
    }
48
49
    public function getName()
50
    {
51
        return 'Character';
52
    }
53
54
    public function resolveType($object)
55
    {
56
        $humans = StarWarsData::humans();
57
        $droids = StarWarsData::droids();
58
59
        $id = $object['id'] ?? $object;
60
61
        if (isset($humans[$id])) {
62
            return new HumanType();
63
        }
64
65
        if (isset($droids[$id])) {
66
            return new DroidType();
67
        }
68
    }
69
}
70