Completed
Push — master ( 0eeb04...4002ae )
by Oscar
02:31
created

UpdateDeleteTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 61
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A limit() 0 14 3
A offset() 0 10 2
A hasCompiledOption() 0 8 2
1
<?php
2
3
namespace SimpleCrud\Queries\Sqlite;
4
5
use SimpleCrud\SimpleCrudException;
6
use SimpleCrud\Queries\Mysql;
7
use PDO;
8
9
/**
10
 * Manages a database select count query in Mysql databases.
11
 *
12
 * @property \SimpleCrud\Table $table
13
 */
14
trait UpdateDeleteTrait
15
{
16
    protected static $options;
17
18
    /**
19
     * Adds a LIMIT clause.
20
     *
21
     * @param int  $limit
22
     * @param bool $force
23
     *
24
     * @return self
25
     */
26
    public function limit($limit, $force = false)
27
    {
28
        if (!$this->hasCompiledOption('ENABLE_UPDATE_DELETE_LIMIT')) {
29
            if (!$force) {
30
                return $this;
31
            }
32
33
            throw new SimpleCrudException('Unable to add LIMIT because ENABLE_UPDATE_DELETE_LIMIT compiled option is disabled');
34
        }
35
36
        $this->limit = $limit;
0 ignored issues
show
Bug introduced by
The property limit 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
        return $this;
39
    }
40
41
    /**
42
     * Adds an offset to the LIMIT clause.
43
     *
44
     * @param int $offset
45
     *
46
     * @return self
47
     */
48
    public function offset($offset)
49
    {
50
        if (!$this->hasCompiledOption('ENABLE_UPDATE_DELETE_LIMIT')) {
51
            throw new SimpleCrudException('Unable to add LIMIT offset because ENABLE_UPDATE_DELETE_LIMIT compiled option is disabled');
52
        }
53
54
        $this->offset = $offset;
0 ignored issues
show
Bug introduced by
The property offset 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...
55
56
        return $this;
57
    }
58
59
    /**
60
     * Check whether the sqlite has a compiled option.
61
     *
62
     * @param string $name
63
     *
64
     * @return bool
65
     */
66
    public function hasCompiledOption($name)
67
    {
68
        if (self::$options === null) {
69
            self::$options = $this->table->getDatabase()->execute('pragma compile_options')->fetchAll(PDO::FETCH_COLUMN);
70
        }
71
72
        return in_array($name, self::$options);
73
    }
74
}
75