Inverse::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 13
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Annotated\Annotation\Relation;
6
7
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...
8
use Doctrine\Common\Annotations\Annotation\Enum;
9
use JetBrains\PhpStorm\ExpectedValues;
10
use Spiral\Attributes\NamedArgumentConstructor;
11
12
/**
13
 * @Annotation
14
 * @NamedArgumentConstructor
15
 * @Target({"PROPERTY", "ANNOTATION"})
16
 */
17
#[\Attribute(\Attribute::TARGET_PROPERTY), NamedArgumentConstructor]
18
final class Inverse
19
{
20
    /**
21
     * @param non-empty-string $as Columns name that will represent relation
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
22
     * @param non-empty-string $type Relation type.
23
     * @param int|non-empty-string|null $load Relation load approach.
24
     */
25 144
    public function __construct(
26
        private string $as,
27
        /**
28
         * @Enum({"hasOne", "belongsTo", "embedded", "hasMany", "manyToMany", "refersTo"}
29
         */
30
        #[ExpectedValues(values: ['hasOne', 'belongsTo', 'embedded', 'hasMany', 'manyToMany', 'refersTo'])]
31
        private string $type,
32
        /**
33
         * @Enum({"eager", "lazy", "promise"}
34
         */
35
        #[ExpectedValues(values: ['eager', 'lazy', 'promise', Relation::LOAD_EAGER, Relation::LOAD_PROMISE])]
36
        private string|int|null $load = null,
37
    ) {
38 144
    }
39
40 144
    public function getType(): string
41
    {
42 144
        return $this->type;
43
    }
44
45 144
    public function getName(): string
46
    {
47 144
        return $this->as;
48
    }
49
50 144
    public function getLoadMethod(): ?int
51
    {
52 144
        return match ($this->load) {
53
            'eager', Relation::LOAD_EAGER => Relation::LOAD_EAGER,
54 12
            'promise', 'lazy', Relation::LOAD_PROMISE => Relation::LOAD_PROMISE,
55 144
            default => null
56
        };
57
    }
58
}
59