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

includes/Console/Command/Restore.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\Installer;
6
use function exec;
7
use function is_file;
8
9
/**
10
 * children class to execute the restore command
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Console
16
 * @author Henry Ruhs
17
 */
18
19
class Restore extends CommandAbstract
20
{
21
	/**
22
	 * array of the command
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_commandArray =
28
	[
29
		'restore' =>
30
		[
31
			'description' => 'Restore command',
32
			'argumentArray' =>
33
			[
34
				'database' =>
35
				[
36
					'description' => 'Restore the database',
37
					'optionArray' =>
38
					[
39
						'directory' =>
40
						[
41
							'description' => 'Required directory'
42
						],
43
						'file' =>
44
						[
45
							'description' => 'Required file'
46
						]
47
					]
48
				]
49
			]
50
		]
51
	];
52
53
	/**
54
	 * run the command
55
	 *
56
	 * @since 3.0.0
57
	 *
58
	 * @param string $mode name of the mode
59
	 *
60
	 * @return string|null
61
	 */
62
63 2
	public function run(string $mode = null) : ?string
64
	{
65 2
		$parser = new Parser($this->_request);
66 2
		$parser->init($mode);
67
68
		/* run command */
69
70 2
		$argumentKey = $parser->getArgument(1);
71 2
		$haltOnError = (bool)$parser->getOption('halt-on-error');
72 2
		if ($argumentKey === 'database')
73
		{
74 1
			return $this->_database($parser->getOptionArray()) ? $this->success() : $this->error($haltOnError);
75
		}
76 1
		return $this->getHelp();
77
	}
78
79
	/**
80
	 * restore the database
81
	 *
82
	 * @since 3.0.0
83
	 *
84
	 * @param array $optionArray
85
	 *
86
	 * @return bool
87
	 */
88
89 1
	protected function _database(array $optionArray = []) : bool
90
	{
91 1
		$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
92 1
		$installer->init();
93 1
		$dbType = $this->_config->get('dbType');
94 1
		$dbHost = $this->_config->get('dbHost');
95 1
		$dbName = $this->_config->get('dbName');
96 1
		$dbUser = $this->_config->get('dbUser');
97 1
		$dbPassword = $this->_config->get('dbPassword');
98
		$directory = $this->prompt('directory', $optionArray);
99
		$file = $this->prompt('file', $optionArray);
100
		$path = $directory . DIRECTORY_SEPARATOR . $file;
101 1
102 1
		/* restore */
103
104
		if (is_file($path))
105
		{
106 1
			if ($dbType === 'mysql' && $dbHost && $dbName && $dbUser && $dbPassword)
107
			{
108
				$command = 'mysql --host=' . $dbHost . ' --user=' . $dbUser . ' --password=' . $dbPassword . ' ' . $dbName . ' < ' . $path;
109
			}
110
			if ($dbType === 'pgsql' && $dbHost && $dbName && $dbUser && $dbPassword)
111
			{
112
				$command = 'psql postgres://' . $dbUser . ':' . $dbPassword . '@' . $dbHost . '/' . $dbName . ' < ' . $path;
113
			}
114
			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...
115
			{
116
				$command = 'sqlite3 ' . $dbHost . ' .dump < ' . $path;
117
			}
118
			if ($command)
119
			{
120
				$installer->rawDrop();
121
				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...
122
			}
123
			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...
124
		}
125 1
		return false;
126
	}
127
}
128