Passed
Push — master ( 4be7e9...eef0b9 )
by Anton
01:32
created

Relation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 34
dl 0
loc 108
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setAttribute() 0 8 2
A getInverseType() 0 3 1
A getName() 0 3 1
A isInversed() 0 3 2
A getTarget() 0 3 1
A getSchema() 0 5 1
A getOptions() 0 3 1
A getInverseName() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\Annotated\Annotation\Relation;
10
11
use Spiral\Annotations\AnnotationInterface;
12
use Spiral\Annotations\Parser;
13
14
abstract class Relation implements RelationInterface, AnnotationInterface
15
{
16
    protected const NAME    = '';
17
    protected const OPTIONS = [
18
        'cascade'         => Parser::BOOL,
19
        'nullable'        => Parser::BOOL,
20
        'innerKey'        => Parser::STRING,
21
        'outerKey'        => Parser::STRING,
22
        'morphKey'        => Parser::STRING,
23
        'morphKeyLength'  => Parser::INTEGER,
24
        'though'          => Parser::STRING,
25
        'thoughInnerKey'  => Parser::STRING,
26
        'thoughOuterKey'  => Parser::STRING,
27
        'thoughConstrain' => Parser::STRING,
28
        'thoughWhere'     => [Parser::MIXED],
29
        'where'           => [Parser::MIXED],
30
        'fkCreate'        => Parser::BOOL,
31
        'fkAction'        => Parser::STRING,
32
        'indexCreate'     => Parser::BOOL,
33
    ];
34
35
    /** @var string|null */
36
    protected $target;
37
38
    /** @var Inversed|null */
39
    protected $inversed;
40
41
    /** @var array */
42
    protected $options = [];
43
44
    /**
45
     * Public and unique node name.
46
     *
47
     * @return string
48
     */
49
    public function getName(): string
50
    {
51
        return static::NAME;
52
    }
53
54
    /**
55
     * Return Node schema in a form of [name => Node|SCALAR|[Node]].
56
     *
57
     * @return array
58
     */
59
    public function getSchema(): array
60
    {
61
        return static::OPTIONS + [
62
                'target'   => Parser::STRING,
63
                'inversed' => Inversed::class
64
            ];
65
    }
66
67
    /**
68
     * Set node attribute value.
69
     *
70
     * @param string $name
71
     * @param mixed  $value
72
     */
73
    public function setAttribute(string $name, $value)
74
    {
75
        if (in_array($name, ['target', 'inversed'])) {
76
            $this->{$name} = $value;
77
            return;
78
        }
79
80
        $this->options[$name] = $value;
81
    }
82
83
    /**
84
     * @return string|null
85
     */
86
    public function getTarget(): ?string
87
    {
88
        return $this->target;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getOptions(): array
95
    {
96
        return $this->options;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function isInversed(): bool
103
    {
104
        return $this->inversed !== null && $this->inversed->isValid();
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getInverseType(): string
111
    {
112
        return $this->inversed->getType();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->inversed->getType() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
The method getType() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        return $this->inversed->/** @scrutinizer ignore-call */ getType();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
113
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getInverseName(): string
120
    {
121
        return $this->inversed->getRelation();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->inversed->getRelation() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
122
    }
123
}