Passed
Pull Request — master (#29)
by Sébastien
08:57
created

ForeignKeyRelation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 7
eloc 14
dl 0
loc 113
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDistantKeyValue() 0 4 1
A setDistantKeyValue() 0 4 1
A getLocalKeyValue() 0 15 3
A setLocalKeyValue() 0 3 1
A applyWhereKeys() 0 3 1
1
<?php
2
3
namespace Bdf\Prime\Relations\Util;
4
5
use Bdf\Prime\Query\QueryInterface;
6
use Bdf\Prime\Query\ReadCommandInterface;
7
use Bdf\Prime\Relations\AbstractRelation;
8
use Bdf\Prime\Relations\RelationInterface;
9
use Bdf\Prime\Repository\RepositoryInterface;
10
11
/**
12
 * Adds foreign key accessor helpers for relations based on single foreign key
13
 *
14
 * @psalm-require-extends AbstractRelation
15
 *
16
 * @template L as object
17
 * @template R as object
18
 */
19
trait ForeignKeyRelation
20
{
21
    /**
22
     * The local property for the relation
23
     *
24
     * @var string
25
     */
26
    protected $localKey;
27
28
    /**
29
     * The distant key
30
     *
31
     * @var string
32
     */
33
    protected $distantKey;
34
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @param Q $query
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\Relations\Util\Q was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
     * @param mixed $value
41
     * @return Q
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
42
     * @template Q as \Bdf\Prime\Query\Contract\Whereable&ReadCommandInterface
0 ignored issues
show
Coding Style introduced by
Tag @template cannot be grouped with parameter tags in a doc comment
Loading history...
43
     * @psalm-suppress InvalidReturnType
0 ignored issues
show
Coding Style introduced by
Tag @psalm-suppress cannot be grouped with parameter tags in a doc comment
Loading history...
44
     *
45
     * @see AbstractRelation::applyWhereKeys()
46
     */
47 77
    protected function applyWhereKeys(ReadCommandInterface $query, $value): ReadCommandInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
48
    {
49 77
        return $query->where($this->distantKey, $value);
0 ignored issues
show
Bug introduced by
The method where() does not exist on Bdf\Prime\Query\ReadCommandInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\ReadCommandInterface. ( Ignorable by Annotation )

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

49
        return $query->/** @scrutinizer ignore-call */ where($this->distantKey, $value);
Loading history...
50
    }
51
52
    /**
53
     * Get the local key value from an entity
54
     * If an array is given, get array of key value
55
     *
56
     * @param L|L[] $entity
57
     *
58
     * @return mixed
59
     */
60 36
    protected function getLocalKeyValue($entity)
61
    {
62 36
        $mapper = $this->localRepository()->mapper();
63
64 36
        if (!is_array($entity)) {
65 34
            return $mapper->extractOne($entity, $this->localKey);
66
        }
67
68 2
        $keys = [];
69
70 2
        foreach ($entity as $e) {
71 2
            $keys[] = $mapper->extractOne($e, $this->localKey);
72
        }
73
74 2
        return $keys;
75
    }
76
77
    /**
78
     * Set the local key value on an entity
79
     *
80
     * @param L $entity
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\Relations\Util\L was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
     * @param mixed  $id
82
     *
83
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
84
     */
85
    protected function setLocalKeyValue($entity, $id): void
0 ignored issues
show
introduced by
Type hint "L" missing for $entity
Loading history...
86
    {
87
        $this->localRepository()->mapper()->hydrateOne($entity, $this->localKey, $id);
88
    }
89
90
    /**
91
     * Get the local key value from an entity
92
     *
93
     * @param R $entity
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\Relations\Util\R was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
94
     *
95
     * @return mixed
96
     */
97 13
    protected function getDistantKeyValue($entity)
0 ignored issues
show
introduced by
Type hint "R" missing for $entity
Loading history...
98
    {
99
        /** @psalm-suppress InvalidArgument */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
100 13
        return $this->relationRepository()->mapper()->extractOne($entity, $this->distantKey);
101
    }
102
103
    /**
104
     * Get the distance key value on an entity
105
     *
106
     * @param R $entity
107
     * @param mixed $id
108
     *
109
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
110
     */
111
    protected function setDistantKeyValue($entity, $id): void
0 ignored issues
show
introduced by
Type hint "R" missing for $entity
Loading history...
112
    {
113
        /** @psalm-suppress InvalidArgument */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
114
        $this->relationRepository()->mapper()->hydrateOne($entity, $this->distantKey, $id);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     *
120
     * @see RelationInterface::relationRepository()
121
     * @return RepositoryInterface<R>
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceR" but found "RepositoryInterface<R>" for function return type
Loading history...
122
     */
123
    abstract public function relationRepository(): RepositoryInterface;
124
125
    /**
126
     * {@inheritdoc}
127
     *
128
     * @see RelationInterface::localRepository()
129
     * @return RepositoryInterface<L>
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceL" but found "RepositoryInterface<L>" for function return type
Loading history...
130
     */
131
    abstract public function localRepository(): RepositoryInterface;
132
}
133