Completed
Push — master ( 1e376d...b0060a )
by Changwan
05:50
created

ReferenceExpression   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 74.29%

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 26
cts 35
cp 0.7429
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C toSql() 0 30 11
A getBindings() 0 4 1
1
<?php
2
namespace Wandu\Database\Query\Expression;
3
4
use Wandu\Database\Contracts\ExpressionInterface;
5
use Wandu\Database\Support\Attributes;
6
use Wandu\Database\Support\Helper;
7
8
/**
9
 * @see http://dev.mysql.com/doc/refman/5.7/en/create-table.html
10
 *
11
 * ReferenceExpression:
12
 *     REFERENCES tbl_name (index_col_name,...)
13
 *         [ON DELETE reference_option]
14
 *         [ON UPDATE reference_option]
15
 * reference_option:
16
 *     RESTRICT | CASCADE | SET NULL | NO ACTION
17
 *
18
 * @method \Wandu\Database\Query\Expression\ReferenceExpression onUpdate(int $option)
19
 * @method \Wandu\Database\Query\Expression\ReferenceExpression onDelete(int $option)
20
 */
21
class ReferenceExpression implements ExpressionInterface
22
{
23
    use Attributes;
24
    
25
    const OPTION_NO_ACTION = 0;
26
    const OPTION_RESTRICT = 1;
27
    const OPTION_CASCADE = 2;
28
    const OPTION_SET_NULL = 3;
29
30
    /** @var string */
31
    protected $table;
32
33
    /** @var array */
34
    protected $columns;
35
36
    /**
37
     * @param string $table
38
     * @param array $columns
39
     * @param array $attributes
40
     */
41 2
    public function __construct($table, array  $columns, array $attributes = [])
42
    {
43 2
        $this->table = $table;
44 2
        $this->columns = $columns;
45 2
        $this->attributes = $attributes;
46 2
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function toSql()
52
    {
53 2
        $sqlToReturn = "REFERENCES `{$this->table}`";
54 2
        $sqlToReturn .= '(' . Helper::arrayImplode(', ', $this->columns, '`', '`') . ')';
55 2
        if (isset($this->attributes['on_delete'])) {
56 1
            switch ($this->attributes['on_delete']) {
57 1
                case static::OPTION_NO_ACTION:
58
                    $sqlToReturn .= ' ON DELETE NO ACTION'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
59 1
                case static::OPTION_RESTRICT:
60
                    $sqlToReturn .= ' ON DELETE RESTRICT'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
61 1
                case static::OPTION_CASCADE:
62
                    $sqlToReturn .= ' ON DELETE CASCADE'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
63 1
                case static::OPTION_SET_NULL:
64 1
                    $sqlToReturn .= ' ON DELETE SET NULL'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
65 1
            }
66 1
        }
67 2
        if (isset($this->attributes['on_update'])) {
68 1
            switch ($this->attributes['on_update']) {
69 1
                case static::OPTION_NO_ACTION:
70
                    $sqlToReturn .= ' ON UPDATE NO ACTION'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71 1
                case static::OPTION_RESTRICT:
72
                    $sqlToReturn .= ' ON UPDATE RESTRICT'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73 1
                case static::OPTION_CASCADE:
74 1
                    $sqlToReturn .= ' ON UPDATE CASCADE'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
75
                case static::OPTION_SET_NULL:
76
                    $sqlToReturn .= ' ON UPDATE SET NULL'; break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
77 1
            }
78 1
        }
79 2
        return $sqlToReturn; 
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getBindings()
86
    {
87
        return [];
88
    }
89
}
90