AbstractTable::writeData()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 6
nop 1
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDbDump/ for the canonical source repository
6
 * @copyright   Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDbDump\Db\Dump\Table;
12
13
use SplFileObject as File;
14
use WebinoDbDump\Db\Dump\Dump;
15
use WebinoDbDump\Db\Dump\Adapter;
16
use WebinoDbDump\Exception;
17
18
/**
19
 * Base class for a dump utility platform table
20
 */
21
abstract class AbstractTable
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * @var Adapter
30
     */
31
    protected $adapter;
32
33
    /**
34
     * @var bool|null
35
     */
36
    protected $view;
37
38
    /**
39
     * @param string $name
40
     */
41
    public function __construct($name, Adapter $adapter)
42
    {
43
        $this->adapter = $adapter;
44
        $this->setName($name);
45
    }
46
47
    /**
48
     * @return \Zend\Db\ResultSet\ResultSet
49
     */
50
    abstract protected function showCreate();
51
52
    /**
53
     * @return \Zend\Db\ResultSet\ResultSet
54
     */
55
    abstract protected function showColumns();
56
57
    /**
58
     * @return \Zend\Db\ResultSet\ResultSet
59
     */
60
    abstract protected function select();
61
62
    /**
63
     * @return ColumnsInterface
64
     */
65
    abstract protected function createColumns();
66
67
    /**
68
     * @return ExtraInterface[]
69
     */
70
    abstract protected function createExtras();
71
72
    /**
73
     * @return string
74
     */
75
    abstract protected function create();
76
77
    /**
78
     * @return string
79
     */
80
    abstract protected function startInsert();
81
82
    /**
83
     * @return string
84
     */
85
    abstract protected function continueInsert();
86
87
    /**
88
     * @return string
89
     */
90
    abstract protected function finishInsert();
91
92
    /**
93
     * @param string $name
94
     * @return $this
95
     */
96
    protected function setName($name)
97
    {
98
        $this->name = $name;
99
        return $this;
100
    }
101
102
    /**
103
     * @return bool
104
     * @throws Exception\RuntimeException
105
     */
106
    protected function isView()
107
    {
108
        if (null === $this->view) {
109
            throw new Exception\RuntimeException('Don\'t know if is view');
110
        }
111
112
        return $this->view;
113
    }
114
115
    /**
116
     * @param bool $view
117
     * @return $this
118
     */
119
    protected function setView($view)
120
    {
121
        $this->view = (bool) $view;
122
        return $this;
123
    }
124
125
    /**
126
     * @return \Zend\Db\Adapter\Platform\PlatformInterface
127
     */
128
    protected function getPlatform()
129
    {
130
        return $this->adapter->getPlatform();
131
    }
132
133
    /**
134
     * @param File $file
135
     * @return $this
136
     */
137
    public function write(File $file)
138
    {
139
        $file->fwrite($this->create());
140
        $this->isView() or $this->writeData($file);
141
142
        foreach ($this->createExtras() as $extra) {
143
            $extra->writeIfAny($file);
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * @param File $file
151
     * @return $this
152
     */
153
    private function writeData(File $file)
154
    {
155
        $data = $this->select();
156
        if (!$data->count()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data->count() of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
157
            // nothing to write
158
            return $this;
159
        }
160
161
        $columns  = $this->createColumnsInternal();
162
        $maxIndex = $data->count() - 1;
163
        $sqlSize  = 0;
164
165
        foreach ($data as $index => $row) {
166
            $line = '';
167
168
            if (0 === $sqlSize) {
169
                $line .= $this->startInsert();
170
            }
171
172
            $line .= $columns->tableRowValues($row);
0 ignored issues
show
Bug introduced by
It seems like $row defined by $row on line 165 can also be of type array or null; however, WebinoDbDump\Db\Dump\Tab...rface::tableRowValues() does only seem to accept object<ArrayObject>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
173
174
            $sqlSize += strlen($line);
175
            if ($index >= $maxIndex
176
                || $sqlSize > Dump::MAX_SQL_SIZE
177
            ) {
178
                $sqlSize = 0;
179
                $line .= $this->finishInsert();
180
            } else {
181
                $line .= $this->continueInsert();
182
            }
183
184
            $file->fwrite($line . PHP_EOL);
185
        }
186
187
        return $this;
188
    }
189
190
    /**
191
     * @return ColumnsInterface
192
     */
193
    private function createColumnsInternal()
194
    {
195
        $columns = $this->createColumns();
196
        foreach ($this->showColumns() as $column) {
197
            $columns->addColumn($column);
0 ignored issues
show
Bug introduced by
It seems like $column defined by $column on line 196 can also be of type array or null; however, WebinoDbDump\Db\Dump\Tab...sInterface::addColumn() does only seem to accept object<ArrayObject>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
198
        }
199
200
        return $columns;
201
    }
202
}
203