Issues (590)

src/Query/AbstractQuery.php (4 issues)

1
<?php
2
3
namespace Bdf\Prime\Query;
4
5
use Bdf\Prime\Query\Contract\Limitable;
6
use Bdf\Prime\Query\Contract\Paginable;
7
use Bdf\Prime\Query\Contract\ReadOperation;
8
use Bdf\Prime\Query\Extension\CompilableTrait;
9
use Bdf\Prime\Query\Extension\ProjectionableTrait;
10
use Bdf\Prime\Query\Extension\SimpleWhereTrait;
11
12
/**
13
 * Abstract query class
14
 * Define standard behaviors for OrmCompiler
15
 *
16
 * @template C as \Bdf\Prime\Connection\ConnectionInterface
17
 * @template R as object|array
18
 *
19
 * @extends AbstractReadCommand<C, R>
20
 * @implements QueryInterface<C, R>
21
 */
22
abstract class AbstractQuery extends AbstractReadCommand implements QueryInterface
23
{
24
    use CompilableTrait;
25
    use ProjectionableTrait;
26
    use SimpleWhereTrait;
27
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function set(string $column, $value, $type = null)
33
    {
34 2
        return $this->setValue($column, $value, $type);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 4
    public function setValue(string $column, $value, $type = null)
41
    {
42 4
        $this->statements['values']['data'][$column] = $value;
43 4
        $this->statements['values']['types'][$column] = $type;
44
45 4
        return $this;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    #[ReadOperation]
52 5
    public function find(array $criteria, $attributes = null)
53
    {
54 5
        if ($attributes !== null) {
55 1
            $this->select($attributes);
56
        }
57
58 5
        $this->where($criteria);
59
60
        if (
61 5
            $this instanceof Paginable
62 5
            && $this instanceof Limitable
63 5
            && $this->hasPagination()
0 ignored issues
show
The method hasPagination() does not exist on Bdf\Prime\Query\AbstractQuery. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

63
            && $this->/** @scrutinizer ignore-call */ hasPagination()
Loading history...
64
        ) {
65
            /** @var Paginable<R>&Limitable $this */
66 1
            return $this->paginate($this->getLimit(), $this->getPage());
0 ignored issues
show
The method getLimit() does not exist on Bdf\Prime\Query\Contract\Paginable. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\Contract\Paginable. ( Ignorable by Annotation )

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

66
            return $this->paginate($this->/** @scrutinizer ignore-call */ getLimit(), $this->getPage());
Loading history...
The method getPage() does not exist on Bdf\Prime\Query\Contract\Paginable. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Query\Contract\Paginable. ( Ignorable by Annotation )

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

66
            return $this->paginate($this->getLimit(), $this->/** @scrutinizer ignore-call */ getPage());
Loading history...
67
        }
68
69 4
        return $this->all();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->all() returns the type iterable which is incompatible with the return type mandated by Bdf\Prime\Query\QueryInterface::find() of Bdf\Prime\Query\R[]&Bdf\...ion\CollectionInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    #[ReadOperation]
76 13
    public function findOne(array $criteria, $attributes = null)
77
    {
78 13
        return $this->where($criteria)->first($attributes);
79
    }
80
}
81