Completed
Push — master ( adecaf...4dd8bf )
by Filipe
02:35
created

EntityRepository::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Repository;
11
12
use Slick\Database\Sql;
13
use Slick\Orm\EntityInterface;
14
use Slick\Orm\RepositoryInterface;
15
16
/**
17
 * Class EntityRepository
18
 *
19
 * @package Slick\Orm\Repository
20
 * @author  Filipe Silva <[email protected]>
21
 */
22
class EntityRepository extends AbstractRepository implements
23
    RepositoryInterface
24
{
25
26
    /**
27
     * Finds entities
28
     *
29
     * @return QueryObject
30
     *
31
     * @see Slick\Database\Sql\Select
32
     */
33
    public function find()
34
    {
35
        return new QueryObject($this);
36
    }
37
38
    /**
39
     * Gets an entity by its id
40
     *
41
     * @param mixed $entityId
42
     *
43
     * @return EntityInterface|null
44
     */
45 4
    public function get($entityId)
46
    {
47 4
        $entity = $this->getIdentityMap()->get($entityId, false);
48 4
        if ($entity === false) {
49 2
            $entity = $this->load($entityId);
50 1
        }
51 4
        return $entity;
52
    }
53
54
    /**
55
     * Loads entity from database
56
     *
57
     * @param $entityId
58
     *
59
     * @return null|EntityInterface
60
     */
61 2
    protected function load($entityId)
62
    {
63 2
        $table = $this->getEntityDescriptor()->getTableName();
64 2
        $primaryKey = $this->getEntityDescriptor()
65 2
            ->getPrimaryKey()
66 2
            ->getField();
67
68 2
        $data = Sql::createSql($this->getAdapter())
69 2
            ->select($this->getEntityDescriptor()->getTableName())
70 2
            ->where(
71
                [
0 ignored issues
show
Documentation introduced by
array("{$table}.{$primar...ay(':id' => $entityId)) is of type array<string|integer,array<string,?,{":id":"?"}>>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72 1
                    "{$table}.{$primaryKey} = :id" => [
73 1
                        ':id' => $entityId
74 1
                    ]
75 1
                ]
76 1
            )
77 2
            ->first();
78 2
        $entity = null;
79 2
        if ($data) {
80 2
            $entity = $this->getEntityMapper()->createFrom($data);
81 2
            $this->getIdentityMap()->set($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by $this->getEntityMapper()->createFrom($data) on line 80 can also be of type array<integer,object<Sli...EntityMapperInterface>> or object<Slick\Orm\Entity\EntityCollection>; however, Slick\Orm\Repository\IdentityMapInterface::set() does only seem to accept object<Slick\Orm\EntityInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82 1
        }
83 2
        return $entity;
84
    }
85
}