Code Duplication    Length = 43-45 lines in 2 locations

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

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

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

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