DeleteAllVersionedData   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 9
eloc 23
c 5
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 24 7
A truncateTable() 0 5 2
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 DeleteAllVersionedData extends BuildTask
12
{
13
    protected $title = 'CAREFUL: delete all versioned data';
14
15
    protected $description = 'Delete versioned data!';
16
17
    public function run($request)
18
    {
19
        if (!Director::isLive()) {
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->truncateTable($table);
26
                        }
27
                    } else {
28
                        $table = $row['table'] ?? '';
29
                        if ($table) {
30
                            $this->truncateTable($table);
31
                        }
32
                    }
33
                }
34
            }
35
            FlushNowImplementor::do_flush('TRUNCATING ChangeSet');
36
            DB::query('TRUNCATE TABLE "ChangeSet";');
37
            FlushNowImplementor::do_flush('TRUNCATING ChangeSetItem');
38
            DB::query('TRUNCATE TABLE "ChangeSetItem";');
39
        } else {
40
            FlushNowImplementor::do_flush('You need to set the environment to TEST or DEV to run this task.');
41
        }
42
    }
43
44
45
    private function truncateTable(string $table)
46
    {
47
        if ('_Versions' === substr((string) $table, -1 * strlen('_Versions'))) {
48
            FlushNowImplementor::do_flush('TRUNCATING ' . $table);
49
            DB::query('TRUNCATE TABLE "' . $table . '";');
50
        }
51
    }
52
}
53