Issues (994)

src/DB/backup.php (5 issues)

1
<?php
2
3
namespace DB;
4
5
use PDO;
0 ignored issues
show
This use statement conflicts with another class in this namespace, DB\PDO. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
class backup
8
{
9
  /**
10
   * \DB\pdo instance.
11
   *
12
   * @var \DB\pdo
13
   */
14
  private $pdo;
15
16
  public function __construct(pdo $pdo)
17
  {
18
    $this->pdo = $pdo;
0 ignored issues
show
Documentation Bug introduced by
It seems like $pdo of type PDO is incompatible with the declared type DB\pdo of property $pdo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
19
  }
20
21
  public function backup()
22
  {
23
    $DBH = $this->pdo->pdo();
24
    $show = $DBH->query('SHOW TABLES');
25
    $tables = [];
26
    while ($row = $show->fetch(PDO::FETCH_NUM)) {
27
      $tables[] = $row[0];
28
    }
29
    $table_creation = '';
30
    foreach ($tables as $table) {
31
      $result = $DBH->query("SELECT * FROM $table");
32
      $num_fields = $result->columnCount();
0 ignored issues
show
The assignment to $num_fields is dead and can be removed.
Loading history...
33
      $num_rows = $result->rowCount();
0 ignored issues
show
The assignment to $num_rows is dead and can be removed.
Loading history...
34
35
      $pstm2 = $DBH->query("SHOW CREATE TABLE $table");
36
      $row2 = $pstm2->fetch(PDO::FETCH_NUM);
37
      $ifnotexists = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $row2[1]);
38
      $table_creation .= "\n\n" . $ifnotexists . ";\n\n";
39
    }
40
    header('Content-Type: application/json');
41
    echo $table_creation;
42
    exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
43
  }
44
}
45