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

ReferenceExpression::toSql()   C

Complexity

Conditions 11
Paths 36

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 12.8905

Importance

Changes 0
Metric Value
cc 11
eloc 24
nc 36
nop 0
dl 0
loc 30
ccs 21
cts 28
cp 0.75
crap 12.8905
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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