Completed
Push — master ( be1dc2...d56e9e )
by Vitaly
06:38
created

dbQuery::own_limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php declare(strict_types = 1);
2
namespace samson\activerecord;
3
4
use samsonframework\orm\ArgumentInterface;
5
use samsonframework\orm\Condition;
6
use samsonframework\orm\ConditionInterface;
7
use samsonframework\orm\QueryHandler;
8
use samsonframework\orm\QueryInterface;
9
10
/**
11
 * This class will be removed in next major release.
12
 * @author     Vitaly Iegorov <[email protected]>
13
 * @deprecated Should be removed ASAP in favor of generated classes or DI
14
 *
15
 */
16
class dbQuery extends QueryHandler
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
17
{
18
    /** Virtual field for base table */
19
    public $own_virtual_fields = array();
20
    /** Virtual fields */
21
    public $virtual_fields = array();
22
    public $empty = false;
23
    protected $class_name;
24
    /**
25
     * @var QueryInterface
26
     * @\samsonframework\containerannotation\Injectable
27
     */
28
    protected $query;
29
    /** @var bool True to show requests */
30
    protected $debug = false;
31
32
    /** Constructor */
33
    public function __construct()
34
    {
35
        $this->database = $GLOBALS['__core']->getContainer()->getDatabase();
0 ignored issues
show
Bug introduced by
The property database does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->sqlBuilder = $GLOBALS['__core']->getContainer()->getSqlBuilder();
0 ignored issues
show
Bug introduced by
The property sqlBuilder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
    
39
    /**
40
     * @param string $metadata
41
     *
42
     * @deprecated Use entity()
43
     * @return QueryInterface|string
44
     */
45
    public function className(string $metadata = null)
46
    {
47
        if (func_num_args() === 0) {
48
            return $this->metadata->className;
0 ignored issues
show
Bug introduced by
The property metadata does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
        } else {
50
            return $this->entity($metadata);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samson\activerecord\dbQuery as the method entity() does only exist in the following sub-classes of samson\activerecord\dbQuery: samsonframework\orm\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
51
        }
52
    }
53
54
55
    /** @deprecated Use QueryInterface implementation */
56
    public function own_limit($st, $en = 0)
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::own_limit" is not in camel caps format
Loading history...
57
    {
58
        $this->query->limit($st, $en);
59
60
        return $this;
61
    }
62
63
    /** @deprecated Use QueryInterface implementation */
64
    public function own_group_by($params)
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::own_group_by" is not in camel caps format
Loading history...
65
    {
66
        $this->query->groupBy($this->class_name, $params);
0 ignored issues
show
Unused Code introduced by
The call to QueryInterface::groupBy() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
67
68
        return $this;
69
    }
70
71
    /** @deprecated Use QueryInterface implementation */
72
    public function own_order_by($field, $direction = 'ASC')
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::own_order_by" is not in camel caps format
Loading history...
73
    {
74
        $this->query->orderBy($this->class_name, $field, $direction);
0 ignored issues
show
Unused Code introduced by
The call to QueryInterface::orderBy() has too many arguments starting with $direction.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
75
76
        return $this;
77
    }
78
79
    /** @deprecated Use QueryInterface implementation */
80
    public function flush()
81
    {
82
83
    }
84
85
    /** @see idbQuery::random() */
86
    public function random(& $return_value = null)
87
    {
88
        // Add random ordering
89
        $this->order_by('', 'RAND()');
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::order_by() has been deprecated with message: Use groupBy()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
91
        // Correctly perform db request for multiple data
92
        return func_num_args() ? $this->exec($return_value) : $this->exec();
0 ignored issues
show
Unused Code introduced by
The call to dbQuery::exec() has too many arguments starting with $return_value.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Deprecated Code introduced by
The method samson\activerecord\dbQuery::exec() has been deprecated with message: Use self::find()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
93
    }
94
95
    /**
96
     * @param        $columnName
97
     * @param string $sorting
98
     *
99
     * @deprecated Use groupBy()
100
     * @return QueryInterface|static
101
     */
102
    public function order_by($columnName, $sorting = 'ASC')
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::order_by" is not in camel caps format
Loading history...
103
    {
104
        return $this->orderBy($this->metadata->tableName, $columnName, $sorting);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samson\activerecord\dbQuery as the method orderBy() does only exist in the following sub-classes of samson\activerecord\dbQuery: samsonframework\orm\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
105
    }
106
107
    /**
108
     * Execute current query and receive collection of RecordInterface objects from database.
109
     * @deprecated Use self::find()
110
     * @return RecordInterface[] Database entities collection
111
     */
112
    public function exec() : array
113
    {
114
        return $this->find();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samson\activerecord\dbQuery as the method find() does only exist in the following sub-classes of samson\activerecord\dbQuery: samsonframework\orm\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
115
    }
116
117
    /** @deprecated Use QueryInterface implementation */
118
    public function or_($relation = 'OR')
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::or_" is not in camel caps format
Loading history...
119
    {
120
        // Получим либо переданную группу условий, либо создадим новую, потом добавим её в массив групп условий запроса
121
        $cond_group = new Condition($relation);
122
123
        // Установим текущую группу условий с которой работает запрос
124
        $this->cConditionGroup = &$cond_group;
0 ignored issues
show
Bug introduced by
The property cConditionGroup does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
125
126
        // Добавим нову группу условий в коллекцию групп
127
        $this->condition->arguments[] = $cond_group;
0 ignored issues
show
Bug introduced by
The property condition does not seem to exist. Did you mean cConditionGroup?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
128
129
        // Вернем себя для цепирования
130
        return $this;
131
    }
132
133
    /** @deprecated Use QueryInterface implementation */
134
    public function debug($value = true)
135
    {
136
        db()->debug($this->debug = $value);
0 ignored issues
show
Deprecated Code introduced by
The function db() has been deprecated with message: Use dependency injection or generated class

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
137
138
        return $this;
139
    }
140
141
    /** @deprecated Use QueryInterface implementation */
142
    public function fieldsNew($fieldName, & $return = null)
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $return is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144
        return call_user_func_array(array($this, 'fields'), func_get_args());
145
    }
146
147
    /** @deprecated Use QueryInterface implementation */
148
    public function group_by($field)
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::group_by" is not in camel caps format
Loading history...
149
    {
150
        $this->query->groupBy($this->metadata->tablename, $field);
0 ignored issues
show
Unused Code introduced by
The call to QueryInterface::groupBy() has too many arguments starting with $field.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
151
152
        // Вернем себя для цепирования
153
        return $this;
154
    }
155
156
    /** @deprecated Use QueryInterface implementation */
157
    public function add_field($field, $alias = null, $own = true)
0 ignored issues
show
Coding Style introduced by
Method name "dbQuery::add_field" is not in camel caps format
Loading history...
158
    {
159
        // Если передан псевдоним для поля, то подставим его
160
        if (isset($alias)) {
161
            $field = $field . ' as ' . $alias;
162
        } else {
163
            $alias = $field;
164
        }
165
166
        // Добавим виртуальное поле
167
        if ($own) {
168
            $this->own_virtual_fields[$alias] = $field;
169
        } else {
170
            $this->virtual_fields[$alias] = $field;
171
        }
172
173
        // Вернем себя для цепирования
174
        return $this;
175
    }
176
177
    /** @deprecated Use QueryInterface implementation */
178
    public function innerCount($field = '*')
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
179
    {
180
        return $this->query->count();
181
    }
182
183
    /** @deprecated Use QueryInterface implementation */
184
    public function id($value)
185
    {
186
        $this->query->primary($value);
187
188
        return $this;
189
    }
190
191
    /**
192
     * Add condition to current query.
193
     * This method supports receives three possible types for $fieldName,
194
     * this is deprecated logic and this should be changed to use separate methods
195
     * for each argument type.
196
     *
197
     * @param string|ConditionInterface|ArgumentInterface $fieldName  Entity field name
198
     * @param string                                      $fieldValue Value
199
     * @param string                                      $relation   Relation between field name and its value
200
     *
201
     * @deprecated Use QueryInterface implementation
202
     * @return self Chaining
203
     */
204
    public function cond($fieldName, $fieldValue = null, $relation = '=')
205
    {
206
        // If empty array is passed
207
        if (is_string($fieldName)) {
208
            return $this->where($fieldName, $fieldValue, $relation);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samson\activerecord\dbQuery as the method where() does only exist in the following sub-classes of samson\activerecord\dbQuery: samsonframework\orm\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
209
        } elseif (is_array($fieldValue) && !sizeof($fieldValue)) {
210
            $this->empty = true;
211
            return $this;
212
        } elseif ($fieldName instanceof ConditionInterface) {
213
            $this->whereCondition($fieldName);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samson\activerecord\dbQuery as the method whereCondition() does only exist in the following sub-classes of samson\activerecord\dbQuery: samsonframework\orm\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
214
        } elseif ($fieldName instanceof ArgumentInterface) {
215
            $this->whereCondition((new Condition())->addArgument($fieldName));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samson\activerecord\dbQuery as the method whereCondition() does only exist in the following sub-classes of samson\activerecord\dbQuery: samsonframework\orm\Query. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
216
        }
217
218
        return $this;
219
    }
220
}
221