TypeComparator::compatible()   C
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 43
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 16
cts 16
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 5
nop 2
crap 8
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Service;
16
17
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType;
18
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType;
19
use Spaark\CompositeUtils\Model\Reflection\Type\AbstractType;
20
use Spaark\CompositeUtils\Model\Reflection\Type\ScalarType;
21
use Spaark\CompositeUtils\Model\Reflection\Type\NullType;
22
23
/**
24
 * Compares two AbstractTypes to check if they are compatible
25
 */
26
class TypeComparator
27
{
28
    /**
29
     * Compares two AbstractTypes, ensuring they are compatible
30
     *
31
     * @param AbstractType $parent
32
     * @param AbstractType $child
33
     * @return boolean
34
     */
35 31
    public function compatible
36
    (
37
        AbstractType $parent,
38
        AbstractType $child
39
    )
40
    : bool
41
    {
42
        if
43
        (
44 31
            ($child instanceof NullType && $parent->nullable) ||
45 31
            ($parent instanceof MixedType)
46
        )
47
        {
48 23
            return true;
49
        }
50 15
        elseif ($parent instanceof ScalarType)
51
        {
52 10
            return get_class($parent) === get_class($child);
53
        }
54 8
        elseif ($parent instanceof ObjectType)
55
        {
56
            if
57
            (
58 7
                $child instanceof ObjectType && 
59 6
                is_a
60
                (
61 6
                    $child->classname->__toString(),
62 6
                    $parent->classname->__toString(),
63 7
                    true
64
                )
65
            )
66
            {
67 6
                return true;
68
            }
69
70 2
            return false;
71
        }
72
73 1
        throw new \DomainException
74
        (
75 1
            'Unknown type: ' . get_class($parent)
76
        );
77
    }
78
}
79