Completed
Push — master ( 858dca...453367 )
by Henry
06:29
created

includes/Console/Command/Backup.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Console\Command;
3
4
use Redaxscript\Console\Parser;
5
use Redaxscript\Dater;
6
use function exec;
7
use function is_dir;
8
use function is_string;
9
use function mkdir;
10
11
/**
12
 * children class to execute the backup command
13
 *
14
 * @since 3.0.0
15
 *
16
 * @package Redaxscript
17
 * @category Console
18
 * @author Henry Ruhs
19
 */
20
21
class Backup extends CommandAbstract
22
{
23
	/**
24
	 * array of the command
25
	 *
26
	 * @var array
27
	 */
28
29
	protected $_commandArray =
30
	[
31
		'backup' =>
32
		[
33
			'description' => 'Backup command',
34
			'argumentArray' =>
35
			[
36
				'database' =>
37
				[
38
					'description' => 'Backup the database',
39
					'optionArray' =>
40
					[
41
						'directory' =>
42
						[
43
							'description' => 'Required directory'
44
						]
45
					]
46
				]
47
			]
48
		]
49
	];
50
51
	/**
52
	 * run the command
53
	 *
54
	 * @since 3.0.0
55
	 *
56
	 * @param string $mode name of the mode
57
	 *
58
	 * @return string|null
59
	 */
60
61
	public function run(string $mode = null) : ?string
62
	{
63
		$parser = new Parser($this->_request);
64 2
		$parser->init($mode);
65
66 2
		/* run command */
67 2
68
		$argumentKey = $parser->getArgument(1);
69
		$haltOnError = (bool)$parser->getOption('halt-on-error');
70
		if ($argumentKey === 'database')
71 2
		{
72 2
			return $this->_database($parser->getOptionArray()) ? $this->success() : $this->error($haltOnError);
73 2
		}
74
		return $this->getHelp();
75 1
	}
76
77 1
	/**
78
	 * backup the database
79
	 *
80
	 * @since 3.0.0
81
	 *
82
	 * @param array $optionArray
83
	 *
84
	 * @return bool
85
	 */
86
87
	protected function _database(array $optionArray = []) : bool
88
	{
89
		$dater = new Dater();
90 1
		$dater->init();
91
		$dbType = $this->_config->get('dbType');
92 1
		$dbHost = $this->_config->get('dbHost');
93 1
		$dbName = $this->_config->get('dbName');
94 1
		$dbUser = $this->_config->get('dbUser');
95 1
		$dbPassword = $this->_config->get('dbPassword');
96 1
		$directory = $this->prompt('directory', $optionArray);
97 1
		$file = $dbName ? $dbName . '_' . $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType : $dater->formatDate() . '_' . $dater->formatTime() . '.' . $dbType;
98 1
		$path = $directory . DIRECTORY_SEPARATOR . $file;
99 1
100 1
		/* backup */
101
102
		if (is_dir($directory) || is_string($directory) && mkdir($directory))
103
		{
104 1
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
105 1
			{
106
				$command = 'mysqldump --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName . ' > ' . $path;
107
			}
108
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
109 1
			{
110
				$command = 'pg_dump postgres://' . $dbUser . ':' . $dbPassword . '@' . $dbHost . '/' . $dbName . ' > ' . $path;
111
			}
112
			if ($dbType === 'sqlite' && $dbHost)
0 ignored issues
show
Bug Best Practice introduced by
The expression $dbHost of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
113
			{
114
				$command = 'sqlite3 ' . $dbHost . ' .dump > ' . $path;
115
			}
116
			if ($command)
117
			{
118
				exec($command, $outputArray, $status);
0 ignored issues
show
The variable $command does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
119
			}
120
			return $status === 0;
0 ignored issues
show
The variable $status does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
121
		}
122
		return false;
123
	}
124
}
125