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; |
||
9 | use Sunnysideup\Flush\FlushNowImplementor; |
||
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
|
|||
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 |
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.