Completed
Push — master ( 5dcaed...1cb772 )
by Filipe
02:47
created

EntityRepository::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
ccs 12
cts 12
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
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\Repository\QueryObject\QueryObject;
15
use Slick\Orm\Repository\QueryObject\QueryObjectInterface;
16
use Slick\Orm\RepositoryInterface;
17
18
/**
19
 * Class EntityRepository
20
 *
21
 * @package Slick\Orm\Repository
22
 * @author  Filipe Silva <[email protected]>
23
 */
24
class EntityRepository extends AbstractRepository implements
25
    RepositoryInterface
26
{
27
28
    /**
29
     * Finds entities
30
     *
31
     * @return QueryObjectInterface
32
     *
33
     * @see Slick\Database\Sql\Select
34
     */
35 4
    public function find()
36
    {
37 4
        return new QueryObject($this);
38
    }
39
40
    /**
41
     * Gets an entity by its id
42
     *
43
     * @param mixed $entityId
44
     *
45
     * @return EntityInterface|null
46
     */
47 4
    public function get($entityId)
48
    {
49 4
        $entity = $this->getIdentityMap()->get($entityId, false);
50 4
        if ($entity === false) {
51 2
            $entity = $this->load($entityId);
52
        }
53 2
        return $entity;
54
    }
55
56
    /**
57
     * Loads entity from database
58
     *
59
     * @param $entityId
60
     *
61
     * @return null|EntityInterface
62
     */
63 2
    protected function load($entityId)
64
    {
65 2
        $table = $this->getEntityDescriptor()->getTableName();
66 2
        $primaryKey = $this->getEntityDescriptor()
67 2
            ->getPrimaryKey()
68 2
            ->getField();
69 2
        return $this->find()
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 2
                    "{$table}.{$primaryKey} = :id" => [
73
                        ':id' => $entityId
74 2
                    ]
75 2
                ]
76 2
            )
77 2
            ->first();
78
    }
79
}