Completed
Push — master ( 51d1e2...c6c305 )
by Freek
01:45
created

Sqlite   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dumpToFile() 0 14 2
A getDumpCommand() 0 11 1
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Symfony\Component\Process\Process;
7
8
class Sqlite extends DbDumper
9
{
10
    /**
11
     * Dump the contents of the database to a given file.
12
     *
13
     * @param string $dumpFile
14
     *
15
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
16
     */
17
    public function dumpToFile(string $dumpFile)
18
    {
19
        $command = $this->getDumpCommand($dumpFile);
20
21
        $process = new Process($command);
22
23
        if (! is_null($this->timeout)) {
24
            $process->setTimeout($this->timeout);
25
        }
26
27
        $process->run();
28
29
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
30
    }
31
32
    /**
33
     * Get the command that should be performed to dump the database.
34
     *
35
     * @param string $dumpFile
36
     *
37
     * @return string
38
     */
39
    public function getDumpCommand(string $dumpFile): string
40
    {
41
        $command[] = 'echo $\'BEGIN IMMEDIATE;\n.dump\'';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$command was never initialized. Although not strictly required by PHP, it is generally a good practice to add $command = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
42
        $command[] = '|';
43
        $command[] = "\"{$this->dumpBinaryPath}sqlite3\" --bail";
44
        $command[] = "\"$this->dbName\"";
45
        $command[] = '>';
46
        $command[] = "\"{$dumpFile}\"";
47
48
        return implode(' ', $command);
49
    }
50
}
51