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

SimpleTableJoinRelation::join()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 16
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Bdf\Prime\Relations\Util;
4
5
use Bdf\Prime\Query\Contract\EntityJoinable;
6
use Bdf\Prime\Query\JoinClause;
7
use Bdf\Prime\Query\QueryInterface;
8
use Bdf\Prime\Query\ReadCommandInterface;
9
use Bdf\Prime\Relations\AbstractRelation;
10
use Bdf\Prime\Repository\EntityRepository;
11
use Bdf\Prime\Repository\RepositoryInterface;
12
13
/**
14
 * Configure relation with simple join (one level) relation
15
 *
16
 * @property EntityRepository $distant protected
17
 * @property string $attributeAim protected
18
 */
19
trait SimpleTableJoinRelation
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public function relationRepository(): RepositoryInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
25
    {
26 2
        return $this->distant;
27
    }
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $owner should have a doc-comment as per coding-style.
Loading history...
30
     * {@inheritdoc}
31
     */
32 2
    public function link($owner): ReadCommandInterface
33
    {
34 2
        return $this->query($this->getLocalKeyValue($owner));
35
    }
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
38
     * {@inheritdoc}
39
     */
40 2
    public function join(EntityJoinable $query, string $alias): void
41
    {
42
        // @fixme ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "?"
Loading history...
43
//        if ($alias === null) {
0 ignored issues
show
introduced by
Comment indentation error, expected only 1 spaces
Loading history...
Coding Style introduced by
Expected 1 space before comment text but found 8; use block comment if you need indentation
Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 0
Loading history...
44
//            $alias = $this->attributeAim;
0 ignored issues
show
Coding Style introduced by
Expected 1 space before comment text but found 12; use block comment if you need indentation
Loading history...
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 0
Loading history...
introduced by
Comment indentation error, expected only 8 spaces
Loading history...
45
//        }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected at least 8 spaces, found 0
Loading history...
introduced by
There must be no blank line following an inline comment
Loading history...
Coding Style introduced by
Expected 1 space before comment text but found 8; use block comment if you need indentation
Loading history...
46
47 2
        $query->joinEntity(
48 2
            $this->distant->entityName(),
49
            function (JoinClause $clause) use($alias, $query) { $this->buildJoinClause($clause, $query, $alias); },
0 ignored issues
show
Coding Style introduced by
Closing brace of nested function must be on a new line
Loading history...
introduced by
There should be no white space before a closing "}"
Loading history...
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
introduced by
There should be no white space after an opening "{"
Loading history...
Coding Style introduced by
Expected 1 space after USE keyword; found 0
Loading history...
50 2
            null,
51 2
            $alias
52
        );
53
54
        // apply relation constraints
0 ignored issues
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
55 2
        $this->applyConstraints($query, [], '$'.$alias);
56 2
    }
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $discriminator should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
59
     * {@inheritdoc}
60
     */
61 1
    public function joinRepositories(EntityJoinable $query, string $alias, $discriminator = null): array
0 ignored issues
show
Coding Style introduced by
The method parameter $discriminator is never used
Loading history...
Unused Code introduced by
The parameter $query is not used and could be removed. ( Ignorable by Annotation )

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

61
    public function joinRepositories(/** @scrutinizer ignore-unused */ EntityJoinable $query, string $alias, $discriminator = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
The method parameter $query is never used
Loading history...
Unused Code introduced by
The parameter $discriminator is not used and could be removed. ( Ignorable by Annotation )

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

61
    public function joinRepositories(EntityJoinable $query, string $alias, /** @scrutinizer ignore-unused */ $discriminator = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        return [
64 1
            $alias => $this->relationRepository()
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: )
Loading history...
65
        ];
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $constraints should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
69
     * @see AbstractRelation::query()
70
     */
71
    abstract protected function query($value, $constraints = []): ReadCommandInterface;
72
73
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $context should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $constraints should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
74
     * @see AbstractRelation::applyConstraints()
75
     */
76
    abstract protected function applyConstraints(ReadCommandInterface $query, $constraints = [], $context = null): ReadCommandInterface;
77
78
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $constraints should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $context should have a doc-comment as per coding-style.
Loading history...
79
     * @see AbstractRelation::applyContext()
80
     */
81
    abstract protected function applyContext(?string $context, $constraints);
82
83
    /**
84
     * Extract local (owner) entity key(s)
85
     *
86
     * If an array of entity is provide, an array of keys must be returned like this (pseudo-code) :
87
     *
88
     * > function getLocalKeyValue (object $entity)     -> extractKey($entity)
89
     * > function getLocalKeyValue (object[] $entities) -> $entities->map(getLocalKeyValue(object))
90
     *
91
     * @param object|object[] $entity The entity, or array of entity
92
     *
93
     * @return mixed Can return any values that defined AbstractRelation::applyWhereKeys() supports
94
     */
95
    abstract protected function getLocalKeyValue($entity);
96
97
    /**
98
     * Configure the join clause
99
     *
100
     * Ex:
101
     *
102
     * <code>
103
     * protected function buildJoinClause(JoinClause $clause, QueryInterface $query, $alias)
104
     * {
105
     *     // $alias>$key : Check value of attribute $key into distant table ($alias is the alias of the distant table)
106
     *     // $this->getLocalAlias($query).$key : Get attribute $key on the local table (getLocalAlias resolve the local table alias)
107
     *     // new Attribute(...) : Compare with the attribute value instead of litteral string one
108
     *     $clause->on($alias.'>'.$this->distantKey, '=', new Attribute($this->getLocalAlias($query).$this->localKey));
109
     * }
110
     * </code>
111
     *
112
     * @param JoinClause $clause The clause to build
113
     * @param QueryInterface $query The base query
114
     * @param string $alias The distant table alias
115
     *
116
     * @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...
117
     */
118
    abstract protected function buildJoinClause(JoinClause $clause, $query, $alias);
0 ignored issues
show
introduced by
Type hint "QueryInterface" missing for $query
Loading history...
119
}
120