Completed
Push — master ( 22fcd2...81cc2b )
by recca
02:25
created

Mysql::castArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Console\Command;
7
use Illuminate\Database\DatabaseManager;
8
use Recca0120\Terminal\Contracts\WebCommand;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class Mysql extends Command implements WebCommand
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'mysql';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'mysql console';
26
27
    /**
28
     * $connection.
29
     *
30
     * @var \Illuminate\Database\DatabaseManager
31
     */
32
    protected $databaseManager;
33
34
    /**
35
     * __construct.
36
     *
37
     * @param \Illuminate\Database\DatabaseManager $databaseManager
38
     */
39 1
    public function __construct(DatabaseManager $databaseManager)
40
    {
41 1
        parent::__construct();
42
43 1
        $this->databaseManager = $databaseManager;
44 1
    }
45
46
    /**
47
     * Handle the command.
48
     *
49
     * @throws \InvalidArgumentException
50
     */
51 1
    public function handle()
52
    {
53 1
        $query = $this->option('command');
54 1
        $connection = $this->databaseManager->connection();
55 1
        $rows = $this->castArray($connection->select($query));
0 ignored issues
show
Bug introduced by
It seems like $query defined by $this->option('command') on line 53 can also be of type array; however, Illuminate\Database\Connection::select() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56 1
        $headers = array_keys(Arr::get($rows, 0, []));
57 1
        $this->table($headers, $rows);
58 1
    }
59
60
    /**
61
     * castArray.
62
     *
63
     * @param stdClass[] $rows
64
     * @return void
65
     */
66
    protected function castArray($rows)
67
    {
68 1
        return array_map(function ($row) {
69 1
            return (array) $row;
70 1
        }, $rows);
71
    }
72
73
    /**
74
     * Get the console command options.
75
     *
76
     * @return array
77
     */
78 1
    protected function getOptions()
79
    {
80
        return [
81 1
            ['command', null, InputOption::VALUE_OPTIONAL],
82
        ];
83
    }
84
}
85