Completed
Push — master ( f8702a...461e07 )
by Alexandr
04:08
created

ConfigCallTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 10.34 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 82.35%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 6
c 3
b 1
f 0
lcom 0
cbo 0
dl 3
loc 29
rs 10
ccs 14
cts 17
cp 0.8235

1 Method

Rating   Name   Duplication   Size   Complexity  
B __call() 3 25 6

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
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 5/1/16 11:19 AM
7
*/
8
9
namespace Youshido\GraphQL\Type\Config\Traits;
10
11
12
use Youshido\GraphQL\Type\AbstractType;
13
use Youshido\GraphQL\Type\Config\Config;
14
use Youshido\GraphQL\Type\Field\Field;
15
16
/**
17
 * Class ConfigCallTrait
18
 * @package Youshido\GraphQL\Type\Config\Traits
19
 *
20
 * @method string getDescription()
21
 * @method string getKind()
22
 * @method AbstractType getNamedType()
23
 * @method $this setType($type)
24
 * @method Field getField($field)
25
 * @method bool hasField($field)
26
 * @method bool hasFields()
27
 * @method bool isDeprecated()
28
 * @method string getDeprecationReason()
29
 * @property Config $config
30
 *
31
 */
32
trait ConfigCallTrait
33
{
34 21
    public function __call($method, $arguments)
35
    {
36 21
        $propertyName     = false;
37 21
        $passAlongMethods = ['hasField', 'addField', 'addFields', 'removeField', 'getFields', 'hasFields', 'getField', 'getNamedType'];
38
39 21
        if (in_array($method, $passAlongMethods)) {
40 19
            $this->checkBuild();
0 ignored issues
show
Documentation Bug introduced by
The method checkBuild does not exist on object<Youshido\GraphQL\...Traits\ConfigCallTrait>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
42 19
            return call_user_func_array([$this->config, $method], $arguments);
43 14 View Code Duplication
        } elseif (substr($method, 0, 3) == 'get') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44 12
            $propertyName = lcfirst(substr($method, 3));
45 6
        } elseif (substr($method, 0, 3) == 'set') {
46 6
            $propertyName = lcfirst(substr($method, 3));
47 6
            $this->config->set($propertyName, $arguments[0]);
48
49 6
            return $this;
50
        } elseif (substr($method, 0, 2) == 'is') {
51
            $propertyName = lcfirst(substr($method, 2));
52
        }
53 12
        if (in_array($propertyName, ['name', 'description', 'deprecationReason', 'isDeprecated', 'field', 'type'])) {
54 12
            return $this->config->get($propertyName);
55
        }
56
57
        throw new \Exception('Call to undefined method ' . $method);
58
    }
59
60
}
61