Completed
Push — master ( d52ee7...e7842c )
by Alexandr
03:54
created

SchemaValidator::assertFieldsIdentical()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 14
rs 9.2
ccs 11
cts 11
cp 1
cc 4
eloc 8
nc 8
nop 3
crap 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 27
    public function validate(AbstractSchema $schema)
26
    {
27 27
        if (!$schema->getQueryType()->hasFields()) {
28 2
            throw new ConfigurationException('Schema has to have fields');
29
        }
30 25
        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 25
            if ($field->getType() instanceof AbstractObjectType) {
32 23
                $this->assertInterfaceImplementationCorrect($field->getType());
33 20
            }
34 22
        }
35 22
    }
36
37
    /**
38
     * @param AbstractObjectType $type
39
     *
40
     * @throws ConfigurationException
41
     */
42 23
    protected function assertInterfaceImplementationCorrect(AbstractObjectType $type)
43
    {
44 23
        if (!$type->getInterfaces()) {
45 10
            return;
46
        }
47
48 15
        foreach ($type->getInterfaces() as $interface) {
49 15
            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 15
                $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 12
            }
52 12
        }
53 12
    }
54
55
    /**
56
     * @param Field                 $intField
57
     * @param Field                 $objField
58
     * @param AbstractInterfaceType $interface
59
     *
60
     * @return bool
61
     *
62
     * @throws ConfigurationException
63
     */
64 15
    protected function assertFieldsIdentical($intField, $objField, AbstractInterfaceType $interface)
65
    {
66 15
        $isValid = true;
67 15
        if ($intField->getType()->isCompositeType() !== $objField->getType()->isCompositeType()) {
68 1
            $isValid = false;
69 1
        }
70 15
        if ($intField->getType()->getNamedType()->getName() != $objField->getType()->getNamedType()->getName()) {
71 2
            $isValid = false;
72 2
        }
73
74 15
        if (!$isValid) {
75 3
            throw new ConfigurationException(sprintf('Implementation of %s is invalid for the field %s', $interface->getName(), $objField->getName()));
76
        }
77 12
    }
78
}
79