DeleteAllTablesTask::deleteTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Sunnysideup\DeleteAllTables;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Dev\BuildTask;
7
use SilverStripe\ORM\DB;
8
use Sunnysideup\Flush\FlushNow;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\Flush\FlushNow was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Sunnysideup\Flush\FlushNowImplementor;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\Flush\FlushNowImplementor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class DeleteAllTablesTask extends BuildTask
12
{
13
    protected $title = 'CAREFUL: delete all tables';
14
15
    protected $description = 'Delete all tables in the database - no backup - so please be super careful!';
16
17
    public function run($request)
18
    {
19
        if (Director::isDev() || Director::is_cli()) {
20
            $rows = DB::query('SHOW TABLES;');
21
            foreach ($rows as $row) {
22
                if ($row) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $row of type array 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...
23
                    if (is_array($row)) {
24
                        foreach ($row as $db => $table) {
25
                            $this->deleteTable($table);
26
                        }
27
                    } else {
28
                        $table = $row['table'] ?? '';
29
                        if ($table) {
30
                            $this->deleteTable($table);
31
                        }
32
                    }
33
                }
34
            }
35
        }
36
    }
37
38
    private function deleteTable(string $table)
39
    {
40
        FlushNowImplementor::do_flush('DELETING ' . $table);
41
        DB::query('DROP TABLE IF EXISTS "' . $table . '";');
42
    }
43
}
44