Completed
Push — master ( 180a00...5febbb )
by Alexandr
03:11
created

AbstractField   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 19.18 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 16
Bugs 6 Features 1
Metric Value
wmc 15
lcom 1
cbo 6
dl 14
loc 73
ccs 30
cts 30
cp 1
rs 10
c 16
b 6
f 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 14 14 3
getType() 0 1 ?
A build() 0 3 1
A setType() 0 4 1
C resolve() 0 23 8
A isDeprecated() 0 4 1
A getDeprecationReason() 0 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
 * Date: 13.05.16
4
 *
5
 * @author Portey Vasil <[email protected]>
6
 */
7
8
namespace Youshido\GraphQL\Field;
9
10
use Youshido\GraphQL\Config\Field\FieldConfig;
11
use Youshido\GraphQL\Execution\ResolveInfo;
12
use Youshido\GraphQL\Type\Object\AbstractObjectType;
13
use Youshido\GraphQL\Type\Traits\AutoNameTrait;
14
use Youshido\GraphQL\Type\Traits\FieldsArgumentsAwareObjectTrait;
15
use Youshido\GraphQL\Type\TypeFactory;
16
use Youshido\GraphQL\Type\TypeMap;
17
use Youshido\GraphQL\Type\TypeService;
18
19
abstract class AbstractField implements FieldInterface
20
{
21
22
    use FieldsArgumentsAwareObjectTrait, AutoNameTrait;
23
24
    protected $isFinal = false;
25
26
    private $resolveCache = null;
27 65
28 View Code Duplication
    public function __construct(array $config = [])
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...
29 65
    {
30 31
        if (empty($config['type'])) {
31 31
            $config['type'] = $this->getType();
32 31
            $config['name'] = $this->getName();
33
        }
34 65
35 57
        if (TypeService::isScalarType($config['type'])) {
36 57
            $config['type'] = TypeFactory::getScalarType($config['type']);
37
        }
38 65
39 63
        $this->config = new FieldConfig($config, $this, $this->isFinal);
40 63
        $this->build($this->config);
41
    }
42
43
    /**
44
     * @return AbstractObjectType
45
     */
46
    abstract public function getType();
47 62
48
    public function build(FieldConfig $config)
49 62
    {
50
    }
51 1
52
    public function setType($type)
53 1
    {
54 1
        $this->getConfig()->set('type', $type);
55
    }
56
57
    public function resolve($value, array $args, ResolveInfo $info)
58
    {
59
        if ($this->resolveCache === null) {
60
            $this->resolveCache = $this->getConfig()->getResolveFunction();
61
62 3
            if (!$this->resolveCache) {
63
                $this->resolveCache = false;
64 3
            }
65
        }
66
        if ($this->resolveCache) {
67 19
            return ($this->resolveCache)($value, $args, $info);
68
        } else {
69 19
            if (is_array($value) && array_key_exists($this->getName(), $value)) {
70 19
                return $value[$this->getName()];
71
            } elseif (is_object($value)) {
72 19
                return TypeService::getPropertyValue($value, $this->getName());
73 19
            } elseif ($this->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) {
74 19
                return null;
75 19
            } else {
76
                throw new \Exception(sprintf('Property "%s" not found in resolve result', $this->getName()));
77 19
            }
78
        }
79
    }
80 3
81
82 3
    public function isDeprecated()
83
    {
84
        return $this->getConfigValue('isDeprecated');
85 3
    }
86
87 3
    public function getDeprecationReason()
88
    {
89
        return $this->getConfigValue('deprecationReason');
90
    }
91
}
92