Completed
Push — master ( 347d1d...c059c0 )
by Rougin
03:20
created

CodeigniterModel::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Rougin\Credo;
4
5
/**
6
 * Codeigniter Model
7
 *
8
 * @package Credo
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \CI_DB $db
12
 * @method   \Doctrine\ORM\EntityRepository getRepository(string $entityName)
13
 * @method   void remove(object $entity)
14
 * @method   void flush(null|object|array $entity)
15
 */
16
class CodeigniterModel extends \CI_Model
17
{
18
    /**
19
     * @var \Rougin\Credo\Credo
20
     */
21
    protected $credo;
22
23
    /**
24
     * @var \Doctrine\ORM\EntityRepository
25
     */
26
    protected $repository;
27
28 3
    public function __construct()
29
    {
30 3
        parent::__construct();
31
32 3
        $this->credo      = new Credo($this->db);
0 ignored issues
show
Documentation introduced by
$this->db is of type object<CI_DB>, but the function expects a object<Rougin\Credo\CI_DB>|null.

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...
33 3
        $this->repository = $this->credo->getRepository(get_class($this));
0 ignored issues
show
Documentation Bug introduced by
The method getRepository does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
34 3
    }
35
36
    /**
37
     * Returns all of the models from the database.
38
     *
39
     * @return array
40
     */
41 3
    public function all()
42
    {
43 3
        return $this->find_by([]);
44
    }
45
46
    /**
47
     * Deletes the specified ID of the model from the database.
48
     *
49
     * @param  integer $id
50
     * @return void
51
     */
52 3
    public function delete($id)
53
    {
54 3
        $item = $this->find($id);
55
56 3
        $this->credo->remove($item);
0 ignored issues
show
Documentation Bug introduced by
The method remove does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
57 3
        $this->credo->flush();
0 ignored issues
show
Documentation Bug introduced by
The method flush does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58 3
    }
59
60
    /**
61
     * Finds an entity by its primary key / identifier.
62
     *
63
     * @param  mixed    $id
64
     * @param  int|null $lockMode
65
     * @param  int|null $lockVersion
66
     * @return object|null
67
     */
68 9
    public function find($id, $lockMode = null, $lockVersion = null)
69
    {
70 9
        return $this->repository->find($id, $lockMode, $lockVersion);
71
    }
72
73
    /**
74
     * Finds models by a set of criteria.
75
     *
76
     * @param  array        $criteria
77
     * @param  array|null   $orderBy
78
     * @param  integer|null $limit
79
     * @param  integer|null $offset
80
     * @return array
81
     */
82 3
    public function find_by(array $criteria, array $orderBy = null, $limit = null, $offset = null)
83
    {
84 3
        return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
85
    }
86
87
    /**
88
     * Inserts a new row into the table.
89
     *
90
     * @param  array $data
91
     * @return integer
92
     */
93 3
    public function insert(array $data)
94
    {
95 3
        $factory  = $this->credo->getMetadataFactory();
0 ignored issues
show
Documentation Bug introduced by
The method getMetadataFactory does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96 3
        $metadata = $factory->getMetadataFor(get_class($this));
97
98 3
        $this->db->insert($metadata->getTableName(), $data);
99
100 3
        $lastId = $this->db->insert_id();
0 ignored issues
show
Bug introduced by
The method insert_id() does not exist on CI_DB. Did you maybe mean insert()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
101
102 3
        $this->credo->refresh($this->find($lastId));
0 ignored issues
show
Documentation Bug introduced by
The method refresh does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
103
104 3
        return $lastId;
105
    }
106
107
    /**
108
     * Updates the selected row from the table.
109
     *
110
     * @param  integer $id
111
     * @param  array   $data
112
     * @return boolean
113
     */
114 3
    public function update($id, array $data)
115
    {
116 3
        $factory  = $this->credo->getMetadataFactory();
0 ignored issues
show
Documentation Bug introduced by
The method getMetadataFactory does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
117 3
        $metadata = $factory->getMetadataFor(get_class($this));
118
119 3
        $this->db->where($metadata->getSingleIdentifierColumnName(), $id);
120 3
        $this->db->set($data);
121
122 3
        $result = $this->db->update($metadata->getTableName());
123
124 3
        $this->credo->refresh($this->find($id));
0 ignored issues
show
Documentation Bug introduced by
The method refresh does not exist on object<Rougin\Credo\Credo>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
125
126 3
        return $result;
127
    }
128
}
129