Code Duplication    Length = 43-45 lines in 2 locations

eZ/Bundle/EzPublishCoreBundle/Command/TestInitDbCommand.php 1 location

@@ 83-127 (lines=45) @@
80
            'ezkeyword' => array(),
81
        );
82
        $handler = $this->getDatabaseHandler();
83
        foreach ($data as $table => $rows) {
84
            // Cleanup before inserting
85
            $deleteQuery = $handler->createDeleteQuery();
86
            $deleteQuery->deleteFrom($handler->quoteIdentifier($table));
87
            $stmt = $deleteQuery->prepare();
88
            $stmt->execute();
89
90
            // Check that at least one row exists
91
            if (!isset($rows[0])) {
92
                continue;
93
            }
94
95
            $q = $handler->createInsertQuery();
96
            $q->insertInto($handler->quoteIdentifier($table));
97
98
            // Contains the bound parameters
99
            $values = array();
100
101
            // Binding the parameters
102
            foreach ($rows[0] as $col => $val) {
103
                $q->set(
104
                    $handler->quoteIdentifier($col),
105
                    $q->bindParam($values[$col])
106
                );
107
            }
108
109
            $stmt = $q->prepare();
110
111
            foreach ($rows as $row) {
112
                try {
113
                    // This CANNOT be replaced by:
114
                    // $values = $row
115
                    // each $values[$col] is a PHP reference which should be
116
                    // kept for parameters binding to work
117
                    foreach ($row as $col => $val) {
118
                        $values[$col] = $val;
119
                    }
120
121
                    $stmt->execute();
122
                } catch (Exception $e) {
123
                    echo "$table ( ", implode(', ', $row), " )\n";
124
                    throw $e;
125
                }
126
            }
127
        }
128
129
        $this->applyStatements($this->getPostInsertStatements($dbType));
130
    }

eZ/Publish/API/Repository/Tests/SetupFactory/Legacy.php 1 location

@@ 181-223 (lines=43) @@
178
        $data['ezmedia'] = array();
179
        $data['ezkeyword'] = array();
180
181
        foreach ($data as $table => $rows) {
182
            // Cleanup before inserting (using TRUNCATE for speed, however not possible to rollback)
183
            $q = $dbPlatform->getTruncateTableSql($handler->quoteIdentifier($table));
184
            $connection->executeUpdate($q);
185
186
            // Check that at least one row exists
187
            if (!isset($rows[0])) {
188
                continue;
189
            }
190
191
            $q = $handler->createInsertQuery();
192
            $q->insertInto($handler->quoteIdentifier($table));
193
194
            // Contains the bound parameters
195
            $values = array();
196
197
            // Binding the parameters
198
            foreach ($rows[0] as $col => $val) {
199
                $q->set(
200
                    $handler->quoteIdentifier($col),
201
                    $q->bindParam($values[$col])
202
                );
203
            }
204
205
            $stmt = $q->prepare();
206
207
            foreach ($rows as $row) {
208
                try {
209
                    // This CANNOT be replaced by:
210
                    // $values = $row
211
                    // each $values[$col] is a PHP reference which should be
212
                    // kept for parameters binding to work
213
                    foreach ($row as $col => $val) {
214
                        $values[$col] = $val;
215
                    }
216
217
                    $stmt->execute();
218
                } catch (Exception $e) {
219
                    echo "$table ( ", implode(', ', $row), " )\n";
220
                    throw $e;
221
                }
222
            }
223
        }
224
225
        $this->applyStatements($this->getPostInsertStatements());
226
    }