Issues (6)

src/Annotation/Relation/Inverse.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Annotated\Annotation\Relation;
13
14
use Cycle\ORM\Relation;
0 ignored issues
show
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...
15
use Doctrine\Common\Annotations\Annotation\Attribute;
16
use Doctrine\Common\Annotations\Annotation\Attributes;
17
use Doctrine\Common\Annotations\Annotation\Enum;
18
19
/**
20
 * @Annotation
21
 * @Target({"PROPERTY", "ANNOTATION"})
22
 * @Attributes({
23
 *      @Attribute("as", type="string", required=true),
24
 *      @Attribute("type", type="string", required=true),
25
 *      @Attribute("load", type="string"),
26
 * })
27
 */
28
final class Inverse
29
{
30
    /** @var string */
31
    private $as;
32
33
    /** @var string */
34
    private $type;
35
36
    /**
37
     * @Enum({"eager", "lazy", "promise"}
38
     * @var string
39
     */
40
    private $load;
41
42
    /**
43
     * @param array $values
44
     */
45
    public function __construct(array $values)
46
    {
47
        foreach ($values as $key => $value) {
48
            if ($key == 'fetch') {
49
                $key = 'load';
50
            }
51
52
            $this->$key = $value;
53
        }
54
    }
55
56
    /**
57
     * @return string|null
58
     */
59
    public function getType(): ?string
60
    {
61
        return $this->type;
62
    }
63
64
    /**
65
     * @return string|null
66
     */
67
    public function getName(): ?string
68
    {
69
        return $this->as;
70
    }
71
72
    /**
73
     * @return int|null
74
     */
75
    public function getLoadMethod(): ?int
76
    {
77
        switch ($this->load) {
78
            case 'eager':
79
            case Relation::LOAD_EAGER:
80
                return Relation::LOAD_EAGER;
81
            case 'promise':
82
            case 'lazy':
83
            case Relation::LOAD_PROMISE:
84
                return Relation::LOAD_PROMISE;
85
            default:
86
                return null;
87
        }
88
    }
89
}
90