Completed
Push — master ( 814e9e...2525ff )
by Filipe
03:26
created

EntityRepository::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
ccs 0
cts 23
cp 0
rs 8.9713
cc 2
eloc 16
nc 2
nop 1
crap 6
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\EntityMapperInterface;
15
use Slick\Orm\RepositoryInterface;
16
17
/**
18
 * Class EntityRepository
19
 *
20
 * @package Slick\Orm\Repository
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class EntityRepository extends AbstractRepository implements
24
    RepositoryInterface
25
{
26
27
    /**
28
     * Gets an entity by its id
29
     *
30
     * @param mixed $entityId
31
     *
32
     * @return EntityInterface|null
33
     */
34
    public function get($entityId)
35
    {
36
        $entity = $this->getIdentityMap()->get($entityId, false);
37
        if ($entity === false) {
38
            $entity = $this->load($entity);
39
        }
40
        return $entity;
41
    }
42
43
    /**
44
     * Loads entity from database
45
     *
46
     * @param $entityId
47
     *
48
     * @return null|EntityInterface
49
     */
50
    protected function load($entityId)
51
    {
52
        $table = $this->getEntityDescriptor()->getTableName();
53
        $primaryKey = $this->getEntityDescriptor()
54
            ->getPrimaryKey()
55
            ->getField();
56
57
        $data = Sql::createSql($this->getAdapter())
58
            ->select($this->getEntityDescriptor()->getTableName())
59
            ->where(
60
                [
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...
61
                    "{$table}.{$primaryKey} = :id" => [
62
                        ':id' => $entityId
63
                    ]
64
                ]
65
            )
66
            ->first();
67
        $entity = null;
68
        if ($data) {
69
            $entity = $this->getEntityMapper()->createFrom($data);
70
            $this->getIdentityMap()->set($entity);
0 ignored issues
show
Bug introduced by
It seems like $entity defined by $this->getEntityMapper()->createFrom($data) on line 69 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...
71
        }
72
        return $entity;
73
    }
74
}