Issues (590)

src/Relations/HasOne.php (4 issues)

1
<?php
2
3
namespace Bdf\Prime\Relations;
4
5
use Bdf\Prime\Query\Custom\KeyValue\KeyValueQuery;
6
use Bdf\Prime\Query\ReadCommandInterface;
7
8
/**
9
 * HasOne
10
 *
11
 * @template L as object
12
 * @template R as object
13
 *
14
 * @extends OneOrMany<L, R>
15
 */
16
class HasOne extends OneOrMany
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $saveStrategy = self::SAVE_STRATEGY_ADD;
22
23
    /**
24
     * Store the relation query for optimisation purpose
25
     *
26
     * @var KeyValueQuery<\Bdf\Prime\Connection\ConnectionInterface, R>|null
27
     */
28
    private $relationQuery;
29
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 12
    protected function getForeignInfos(): array
35
    {
36 12
        return [$this->distant, $this->distantKey];
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 157
    protected function relationQuery($keys, $constraints): ReadCommandInterface
43
    {
44
        // Constraints can be on relation attributes : builder must be used
45
        // @todo Handle "bulk select"
46 157
        if (count($keys) !== 1 || $constraints || $this->constraints) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->constraints of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
47 27
            return $this->query($keys, $constraints)->by($this->distantKey);
0 ignored issues
show
The method by() does not exist on Bdf\Prime\Query\QueryInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Bdf\Prime\Query\SqlQueryInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

47
            return $this->query($keys, $constraints)->/** @scrutinizer ignore-call */ by($this->distantKey);
Loading history...
48
        }
49
50 136
        if ($this->relationQuery) {
51 60
            return $this->relationQuery->where($this->distantKey, $keys[0]);
52
        }
53
54 89
        $query = $this->distant->queries()->keyValue($this->distantKey, $keys[0]);
55
56 89
        if (!$query) {
0 ignored issues
show
$query is of type Bdf\Prime\Query\Contract...\KeyValueQueryInterface, thus it always evaluated to true.
Loading history...
57
            return $this->query($keys, $constraints)->by($this->distantKey);
58
        }
59
60 89
        return $this->relationQuery = $query->by($this->distantKey);
0 ignored issues
show
The method by() does not exist on Bdf\Prime\Query\Contract...\KeyValueQueryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\Contract...\KeyValueQueryInterface. ( Ignorable by Annotation )

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

60
        return $this->relationQuery = $query->/** @scrutinizer ignore-call */ by($this->distantKey);
Loading history...
61
    }
62
}
63