Issues (590)

src/Relations/HasMany.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
 * HasMany
10
 *
11
 * @template L as object
12
 * @template R as object
13
 *
14
 * @extends OneOrMany<L, R>
15
 */
16
class HasMany extends OneOrMany
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $saveStrategy = self::SAVE_STRATEGY_REPLACE;
22
23
    /**
24
     * Store the relation query for optimisation purpose
25
     *
26
     * @var KeyValueQuery
27
     */
28
    private $relationQuery;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 25
    protected function getForeignInfos(): array
34
    {
35 25
        return [$this->distant, $this->distantKey];
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 65
    protected function relationQuery($keys, $constraints): ReadCommandInterface
42
    {
43
        // Constraints can be on relation attributes : builder must be used
44
        // @todo Handle "bulk select"
45 65
        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...
46 36
            return $this->query($keys, $constraints)->by($this->distantKey, true);
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

46
            return $this->query($keys, $constraints)->/** @scrutinizer ignore-call */ by($this->distantKey, true);
Loading history...
47
        }
48
49 55
        if ($this->relationQuery) {
50 23
            return $this->relationQuery->where($this->distantKey, $keys[0]);
51
        }
52
53 34
        $query = $this->distant->queries()->keyValue($this->distantKey, $keys[0]);
54
55 34
        if (!$query) {
0 ignored issues
show
$query is of type Bdf\Prime\Query\Contract...\KeyValueQueryInterface, thus it always evaluated to true.
Loading history...
56
            return $this->query($keys, $constraints)->by($this->distantKey, true);
57
        }
58
59 34
        return $this->relationQuery = $query->by($this->distantKey, true);
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

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