Completed
Push — master ( 8089c4...ec27e4 )
by smiley
02:29
created

DBQuery.php$0 ➔ view()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 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
use chillerlan\Database\Query\CreateInterface;
17
use chillerlan\Database\Query\StatementAbstract;
18
use chillerlan\Database\Query\CreateDatabaseInterface;
19
use chillerlan\Database\Query\CreateTableInterface;
20
/**
21
 * @property \chillerlan\Database\Query\SelectInterface $select
22
 * @property \chillerlan\Database\Query\InsertInterface $insert
23
 * @property \chillerlan\Database\Query\UpdateInterface $update
24
 * @property \chillerlan\Database\Query\DeleteInterface $delete
25
 * @property \chillerlan\Database\Query\CreateInterface $create
26
 */
27
class DBQuery{
28
29
	const DIALECTS = [
30
		'mysql'    => 'MySQL',
31
		'pgsql'    => 'Postgres',
32
		'sqlite'   => 'SQLite',
33
		'firebird' => 'Firebird',
34
	];
35
36
	/**
37
	 * @var \chillerlan\Database\Drivers\DBDriverInterface
38
	 */
39
	protected $DBDriver;
40
41
	/**
42
	 * @var string
43
	 */
44
	protected $dialect;
45
46
	/**
47
	 * DBQuery constructor.
48
	 *
49
	 * @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver
50
	 */
51
	public function __construct(DBDriverInterface $DBDriver){
52
		$this->DBDriver = $DBDriver;
53
54
		$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...
55
	}
56
57
	/**
58
	 * @param string $name
59
	 *
60
	 * @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...
61
	 */
62
	public function __get(string $name){
63
		$name = strtolower($name);
64
65
		if(in_array($name, ['select', 'insert', 'update', 'delete'])){
66
			$class = $this->dialect.'\\'.ucfirst($name);
67
			return new $class($this->DBDriver, $this->dialect);
68
		}
69
		elseif($name === 'create'){
70
71
			return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements CreateInterface{
72
73
				public function database(string $dbname = null):CreateDatabaseInterface{
74
					return $this->getStatementClass('CreateDatabase')->name($dbname);
75
				}
76
77
				public function table(string $tablename = null):CreateTableInterface{
78
					return $this->getStatementClass('CreateTable')->name($tablename);
79
				}
80
81
				public function index():CreateInterface{
82
					// TODO: Implement index() method.
83
				}
84
85
				public function view():CreateInterface{
86
					// TODO: Implement view() method.
87
				}
88
89
				public function trigger():CreateInterface{
90
					// TODO: Implement trigger() method.
91
				}
92
			};
93
94
		}
95
96
		return null;
97
	}
98
99
}
100