Completed
Pull Request — master (#37)
by Sebastian
03:52
created

SchemaValidator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 96.55%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 0
cbo 5
dl 0
loc 62
ccs 28
cts 29
cp 0.9655
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertFieldsIdentical() 0 14 4
A validate() 0 11 4
A assertInterfaceImplementationCorrect() 0 12 4
1
<?php
2
/*
3
* This file is a part of GraphQL project.
4
*
5
* @author Alexandr Viniychuk <[email protected]>
6
* created: 4/30/16 9:11 PM
7
*/
8
9
namespace Youshido\GraphQL\Validator\SchemaValidator;
10
11
use Youshido\GraphQL\Field\Field;
12
use Youshido\GraphQL\Schema\AbstractSchema;
13
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType;
14
use Youshido\GraphQL\Type\Object\AbstractObjectType;
15
use Youshido\GraphQL\Validator\Exception\ConfigurationException;
16
17
class SchemaValidator
18
{
19
20
    /**
21
     * @param AbstractSchema $schema
22
     *
23
     * @throws ConfigurationException
24
     */
25 5
    public function validate(AbstractSchema $schema)
26
    {
27 5
        if (!$schema->getQueryType()->hasFields()) {
28 1
            throw new ConfigurationException('Schema has to have fields');
29
        }
30 4
        foreach ($schema->getQueryType()->getConfig()->getFields() as $field) {
0 ignored issues
show
Documentation Bug introduced by
The method getFields does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? 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...
31 4
            if ($field->getType() instanceof AbstractObjectType) {
32 4
                $this->assertInterfaceImplementationCorrect($field->getType());
33 1
            }
34 1
        }
35 1
    }
36
37
    /**
38
     * @param AbstractObjectType $type
39
     *
40
     * @throws ConfigurationException
41
     */
42 4
    protected function assertInterfaceImplementationCorrect(AbstractObjectType $type)
43
    {
44 4
        if (!$type->getInterfaces()) {
45
            return;
46
        }
47
48 4
        foreach ($type->getInterfaces() as $interface) {
49 4
            foreach ($interface->getConfig()->getFields() as $intField) {
0 ignored issues
show
Documentation Bug introduced by
The method getFields does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? 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...
50 4
                $this->assertFieldsIdentical($intField, $type->getConfig()->getField($intField->getName()), $interface);
0 ignored issues
show
Documentation Bug introduced by
The method getField does not exist on object<Youshido\GraphQL\Config\AbstractConfig>? 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...
51 1
            }
52 1
        }
53 1
    }
54
55
    /**
56
     * @param Field                 $intField
57
     * @param Field                 $objField
58
     * @param AbstractInterfaceType $interface
59
     *
60
     * @return bool
61
     *
62
     * @throws ConfigurationException
63
     */
64 4
    protected function assertFieldsIdentical($intField, $objField, AbstractInterfaceType $interface)
65
    {
66 4
        $isValid = true;
67 4
        if ($intField->getType()->isCompositeType() !== $objField->getType()->isCompositeType()) {
68 1
            $isValid = false;
69 1
        }
70 4
        if ($intField->getType()->getNamedType()->getName() != $objField->getType()->getNamedType()->getName()) {
71 2
            $isValid = false;
72 2
        }
73
74 4
        if (!$isValid) {
75 3
            throw new ConfigurationException(sprintf('Implementation of %s is invalid for the field %s', $interface->getName(), $objField->getName()));
76
        }
77 1
    }
78
}
79