Model::joinGetAll()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace System;
4
5
abstract class Model
6
{
7
    /**
8
     * Application Object
9
     *
10
     * @var \System\Application
11
     */
12
    private $app;
13
14
    /**
15
     * Table of a model
16
     *
17
     * @var $table
18
     */
19
    protected $table;
20
21
    /**
22
     * Constructor
23
     *
24
     * @param \System\Application $app
25
     */
26
    public function __construct(Application $app)
27
    {
28
        $this->app = $app;
29
    }
30
31
    /**
32
     * Call shared Application Objects dynamically
33
     *
34
     * @param string $key
35
     * @return mixed
36
     */
37
    public function __get($key)
38
    {
39
        return $this->app->get($key);
40
    }
41
42
    /**
43
     * Call the methods from Database Object
44
     *
45
     * @property object $db
46
     * @param method $method
47
     * @param array $args
48
     */
49
    public function __call($method, $args)
50
    {
51
        return call_user_func_array([$this->app->db, $method], $args);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<System\Application>. 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...
52
    }
53
54
    /**
55
     * Get all the Rows
56
     *
57
     * @method orderBy
58
     * @method limit
59
     * @method fetchAll
60
     * @param array $order
61
     * @param int $limit
62
     * @param string $table
63
     */
64
    public function getAll(array $order = ['id', 'DESC'], int $limit = null, string $table = null)
65
    {
66
        return $this->orderBy($order[0], $order[1])->limit($limit)->fetchAll($table ? $table : $this->table);
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<System\Model>? 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...
67
    }
68
69
    /**
70
     * Get a Row
71
     *
72
     * @method where
73
     * @method fetch
74
     * @param string $value
75
     * @param string $coulmn
76
     */
77
    public function get(string $value, string $coulmn = 'id')
78
    {
79
        return $this->where($coulmn . ' = ?', $value)->fetch($this->table);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<System\Model>? 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...
80
    }
81
82
    /**
83
     * Check if row exists
84
     *
85
     * @method select
86
     * @method where
87
     * @method fetch
88
     * @param string $value
89
     * @param string $key
90
     */
91
    public function exists(string $value, string $key = 'id')
92
    {
93
        return (bool) $this->select($key)->where($key . ' = ? ', $value)->fetch($this->table);
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<System\Model>? 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...
94
    }
95
96
    /**
97
     * Drop a row
98
     *
99
     * @method where
100
     * @method delete
101
     * @param string $id
102
     */
103
    public function delete(string $id)
104
    {
105
        return $this->where('id = ?', $id)->delete($this->table);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<System\Model>? 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...
106
    }
107
108
    /**
109
     * Join
110
     *
111
     * @method select
112
     * @method from
113
     * @method join
114
     * @param string $select
115
     * @param string $joins
116
     * @param string $table
117
     */
118
    public function joinGetAll(string $select, string $joins, string $table = null)
119
    {
120
        return $this->db->select($select)->from($table ? $table : $this->table)->join($joins);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<System\Model>. 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...
121
    }
122
123
    /**
124
     * Get a row after Joining
125
     *
126
     * @method select
127
     * @method from
128
     * @method join
129
     * @method where
130
     * @param string $select
131
     * @param string $table
132
     * @param string $joins
133
     * @param string $table
134
     */
135
    public function joinGetRow(string $select, string $joins, string $where, string $table = null)
136
    {
137
        $table = $table ? $table : $this->table;
138
        return $this->db->select($select)->from($table)->join($joins)->where($table . '.id = ?', $where);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<System\Model>. 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...
139
    }
140
}
141