Completed
Push — master ( 0ca878...f08c1d )
by smiley
02:26
created

DBQuery::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
/**
3
 * Class DBQuery
4
 *
5
 * @filesource   DBQuery.php
6
 * @created      03.06.2017
7
 * @package      chillerlan\Database
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database;
14
15
use chillerlan\Database\Drivers\DBDriverInterface;
16
17
/**
18
 * @property \chillerlan\Database\Query\SelectInterface $select
19
 * @property \chillerlan\Database\Query\InsertInterface $insert
20
 * @property \chillerlan\Database\Query\UpdateInterface $update
21
 * @property \chillerlan\Database\Query\DeleteInterface $delete
22
 * @property \chillerlan\Database\Query\CreateInterface $create
23
 */
24
class DBQuery{
25
26
	const DIALECTS = [
27
		'mysql'    => 'MySQL',
28
		'pgsql'    => 'Postgres',
29
		'sqlite'   => 'SQLite',
30
		'firebird' => 'Firebird',
31
	];
32
33
	/**
34
	 * @var \chillerlan\Database\Drivers\DBDriverInterface
35
	 */
36
	protected $DBDriver;
37
38
	/**
39
	 * @var string
40
	 */
41
	protected $dialect;
42
43
	/**
44
	 * DBQuery constructor.
45
	 *
46
	 * @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver
47
	 */
48
	public function __construct(DBDriverInterface $DBDriver){
49
		$this->DBDriver = $DBDriver;
50
51
		$this->dialect = __NAMESPACE__.'\\Query\\Dialects\\'.self::DIALECTS[$this->DBDriver->dialect];
0 ignored issues
show
Bug introduced by
Accessing dialect on the interface chillerlan\Database\Drivers\DBDriverInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
52
	}
53
54
	/**
55
	 * @param string $name
56
	 *
57
	 * @return \chillerlan\Database\Query\StatementInterface|null
0 ignored issues
show
Documentation introduced by
Should the return type not be object|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
58
	 */
59
	public function __get(string $name){
60
		$name = strtolower($name);
61
62
		if(in_array($name, ['select', 'insert', 'update', 'delete', 'create'])){
63
			$class = $this->dialect.'\\'.ucfirst($name);
64
			return new $class($this->DBDriver, $this->dialect);
65
		}
66
67
		return null;
68
	}
69
70
}
71