Completed
Push — develop ( 682a70...91eb81 )
by Timothy
01:28
created

Driver::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 4
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Query
4
 *
5
 * SQL Query Builder / Database Abstraction Layer
6
 *
7
 * PHP version 7.1
8
 *
9
 * @package     Query
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2012 - 2018 Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @link        https://git.timshomepage.net/aviat4ion/Query
14
 */
15
namespace Query\Drivers\Pgsql;
16
17
use Query\Drivers\AbstractDriver;
18
use Query\Drivers\DriverInterface;
19
20
/**
21
 * PostgreSQL specific class
22
 */
23
class Driver extends AbstractDriver implements DriverInterface {
24
25
	/**
26
	 * Connect to a PosgreSQL database
27
	 *
28
	 * @codeCoverageIgnore
29
	 * @param string $dsn
30
	 * @param string $username
31
	 * @param string $password
32
	 * @param array  $options
33
	 */
34
	public function __construct($dsn, $username=NULL, $password=NULL, array $options=[])
35
	{
36
		if (strpos($dsn, 'pgsql') === FALSE)
37
		{
38
			$dsn = 'pgsql:'.$dsn;
39
		}
40
41
		parent::__construct($dsn, $username, $password, $options);
42
	}
43
44
	// --------------------------------------------------------------------------
45
46
	/**
47
	 * Get a list of schemas for the current connection
48
	 *
49
	 * @return array
50
	 */
51
	public function getSchemas(): ?array
52
	{
53
		$sql = <<<SQL
54
			SELECT DISTINCT "schemaname" FROM "pg_tables"
55
			WHERE "schemaname" NOT LIKE 'pg\_%'
56
			AND "schemaname" != 'information_schema'
57
SQL;
58
59
		return $this->driverQuery($sql);
60
	}
61
62
	// --------------------------------------------------------------------------
63
64
	/**
65
	 * Retrieve foreign keys for the table
66
	 *
67
	 * @param string $table
68
	 * @return array
69
	 */
70
	public function getFks($table): array
71
	{
72
		$valueMap = [
73
			'c' => 'CASCADE',
74
			'r' => 'RESTRICT',
75
		];
76
77
		$keys = parent::getFks($table);
78
79
		foreach($keys as &$key)
0 ignored issues
show
Bug introduced by
The expression $keys of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
		{
81
			foreach(['update', 'delete'] AS $type)
82
			{
83
				if ( ! isset($valueMap[$key[$type]])) continue;
84
85
86
				$key[$type] = $valueMap[$key[$type]];
87
			}
88
		}
89
90
		return $keys;
91
	}
92
}