Completed
Pull Request — master (#1339)
by José
02:01
created

AlterInstructions   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 110
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addAlter() 0 4 1
A addPostStep() 0 4 1
A getAlterParts() 0 4 1
A getPostSteps() 0 4 1
A merge() 0 5 1
A execute() 0 18 4
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated * documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 */
25
namespace Phinx\Db\Util;
26
27
/**
28
 * Contains all the information for running an ALTER command for a table,
29
 * and any post-steps required after the fact.
30
 */
31
class AlterInstructions
32
{
33
34
    /**
35
     * @var string[] The SQL snippets to be added to an ALTER instruction
36
     */
37
    protected $alterParts = [];
38
39
    /**
40
     * @var mixed[] The SQL commands to be executed after the ALTER instruction
41
     */
42
    protected $postSteps = [];
43
44
    /**
45
     * Constructor
46
     *
47
     * @param array $alterParts SQL snipets to be added to a single ALTER instruction per table
48
     * @param array $postSteps SQL commands to be executed after the ALTER instruction
49
     */
50
    public function __construct(array $alterParts = [], array $postSteps = [])
51
    {
52
        $this->alterParts = $alterParts;
53
        $this->postSteps = $postSteps;
54
    }
55
56
    /**
57
     * Adds another parst for the single ALTER instruction
58
     *
59
     * @param string $part The SQL snipped to add as part of the ALTER instruction
60
     * @return void
61
     */
62
    public function addAlter($part)
63
    {
64
        $this->alterParts[] = $part;
65
    }
66
67
    /**
68
     * Adds a SQL command to be eecuted after the ALTER instruction.
69
     * This method allows a callable, with will get an empty array as state
70
     * for the first time and will pass the return value of the callable to
71
     * the next callable, if present.
72
     *
73
     * This allows to keep a single state across callbacks.
74
     *
75
     * @param string|callable $sql The SQL to run after, or a callable to execute
76
     * @return void
77
     */
78
    public function addPostStep($sql)
79
    {
80
        $this->postSteps[] = $sql;
81
    }
82
83
    /**
84
     * Returns the alter SQL snippets
85
     *
86
     * @return string[]
87
     */
88
    public function getAlterParts()
89
    {
90
        return $this->alterParts;
91
    }
92
93
    /**
94
     * Returns the SQL commands to run after the ALTER instrunction
95
     *
96
     * @return mixed[]
97
     */
98
    public function getPostSteps()
99
    {
100
        return $this->postSteps;
101
    }
102
103
    /**
104
     * Merges another AlterInstructions object to this one
105
     *
106
     * @param AlterInstructions $other The other collection of instructions to merge in
107
     * @return void
108
     */
109
    public function merge(AlterInstructions $other)
110
    {
111
        $this->alterParts = array_merge($this->alterParts, $other->getAlterParts());
112
        $this->postSteps = array_merge($this->postSteps, $other->getPostSteps());
113
    }
114
115
    /**
116
     * Executes the ALTER instruction and all of the post steps.
117
     *
118
     * @param string $alterTemplate The template for the alter instruction
119
     * @param callable $executor The function to be used to execute all instructions
120
     * @return void
121
     */
122
    public function execute($alterTemplate, callable $executor)
123
    {
124
        if ($this->alterParts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->alterParts of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
125
            $alter = sprintf($alterTemplate, implode(', ', $this->alterParts));
126
            $executor($alter);
127
        }
128
129
        $state = [];
130
131
        foreach ($this->postSteps as $instruction) {
132
            if (is_callable($instruction)) {
133
                $state = $instruction($state);
134
                continue;
135
            }
136
137
            $executor($instruction);
138
        }
139
    }
140
}
141