Passed
Push — master ( 16a10b...244c8c )
by Anton
03:39
created

Inverse::getLoadMethod()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 0
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 Cycle\ORM\Relation;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Cycle\Annotated\Annotation\Relation\Relation. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use Spiral\Annotations\AbstractAnnotation;
13
use Spiral\Annotations\Parser;
14
15
final class Inverse extends AbstractAnnotation
16
{
17
    protected const NAME   = 'inverse';
18
    protected const SCHEMA = [
19
        'type'  => Parser::STRING,
20
        'name'  => Parser::STRING,
21
        'as'    => Parser::STRING, // alias to name
22
        'load'  => Parser::STRING,
23
        'fetch' => Parser::STRING, // alias to load
24
    ];
25
26
    /** @var string|null */
27
    protected $type;
28
29
    /** @var string|null */
30
    protected $name;
31
32
    /** @var string|null */
33
    protected $load;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function setAttribute(string $name, $value)
39
    {
40
        if ($name == "as") {
41
            $name = "name";
42
        }
43
44
        if ($name == "fetch") {
45
            $name = "load";
46
        }
47
48
        parent::setAttribute($name, $value);
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function isValid(): bool
55
    {
56
        return $this->getType() !== null && $this->getRelation() !== null;
57
    }
58
59
    /**
60
     * @return string|null
61
     */
62
    public function getType(): ?string
63
    {
64
        return $this->type;
65
    }
66
67
    /**
68
     * @return string|null
69
     */
70
    public function getRelation(): ?string
71
    {
72
        return $this->name;
73
    }
74
75
    /**
76
     * @return int|null
77
     */
78
    public function getLoadMethod(): ?int
79
    {
80
        switch ($this->load) {
81
            case 'eager':
82
            case Relation::LOAD_EAGER:
83
                return Relation::LOAD_EAGER;
84
            case 'promise':
85
            case 'lazy':
86
            case Relation::LOAD_PROMISE:
87
                return Relation::LOAD_PROMISE;
88
            default:
89
                return null;
90
        }
91
    }
92
}