Completed
Pull Request — master (#1)
by Rougin
02:29
created

CodeigniterModel::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
/**
6
 * Codeigniter Model
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 */
11
class CodeigniterModel extends \CI_Model
12
{
13
    use Traits\ModelTrait;
14
15
    /**
16
     * @var \Rougin\Wildfire\Wildfire
17
     */
18
    protected $wildfire;
19
20 12
    public function __construct()
21
    {
22 12
        parent::__construct();
23
24 12
        $this->wildfire = new Wildfire($this->db);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Rougin\Wildfire\CodeigniterModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
25 12
    }
26
27
    /**
28
     * Returns all of the models from the database.
29
     *
30
     * @return array
31
     */
32 3
    public function all()
33
    {
34 3
        return $this->findBy([]);
35
    }
36
37
    /**
38
     * Deletes the specified ID of the model from the database.
39
     *
40
     * @param  integer $id
41
     * @return void
42
     */
43
    public function delete($id)
44
    {
45
        return $this->wildfire->delete($this->getTableName(), $id);
0 ignored issues
show
Documentation Bug introduced by
The method delete does not exist on object<Rougin\Wildfire\Wildfire>? 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...
46
    }
47
48
    /**
49
     * Finds the specified model from the database.
50
     *
51
     * @param  integer $id
52
     * @return mixed
53
     */
54 3
    public function find($id)
55
    {
56 3
        return $this->wildfire->find($this->getTableName(), $id);
57
    }
58
59
    /**
60
     * Finds the specified model from the database by the given delimiters.
61
     *
62
     * @param  array $delimiters
63
     * @return mixed
64
     */
65 3
    public function findBy(array $delimiters)
66
    {
67 3
        $this->db->where($delimiters);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Rougin\Wildfire\CodeigniterModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
69 3
        return $this->get()->result();
70
    }
71
72
    /**
73
     * Returns all rows from the specified table.
74
     *
75
     * @return self
76
     */
77 3
    public function get()
78
    {
79 3
        return $this->wildfire->get($this->getTableName());
80
    }
81
82
    /**
83
     * Calls methods from this class in underscore case.
84
     *
85
     * @param  string $method
86
     * @param  mixed  $parameters
87
     * @return mixed
88
     */
89 View Code Duplication
    public function __call($method, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $method = camelize($method);
92
        $result = $this;
93
94
        if (method_exists($this, $method)) {
95
            $class = [$this, $method];
96
            
97
            $result = call_user_func_array($class, $parameters);
98
        }
99
100
        return $result;
101
    }
102
}
103