Completed
Push — master ( 20edd5...7d7f4e )
by smiley
02:32
created

DBQuery.php$0 ➔ database()   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\AlterInterface;
17
use chillerlan\Database\Query\CreateInterface;
18
use chillerlan\Database\Query\DropInterface;
19
use chillerlan\Database\Query\StatementAbstract;
20
use chillerlan\Database\Query\CreateDatabaseInterface;
21
use chillerlan\Database\Query\CreateTableInterface;
22
23
/**
24
 * @property \chillerlan\Database\Query\SelectInterface $select
25
 * @property \chillerlan\Database\Query\InsertInterface $insert
26
 * @property \chillerlan\Database\Query\UpdateInterface $update
27
 * @property \chillerlan\Database\Query\DeleteInterface $delete
28
 * @property \chillerlan\Database\Query\CreateInterface $create
29
 * @property \chillerlan\Database\Query\DropInterface   $drop
30
 * @property \chillerlan\Database\Query\AlterInterface  $alter
31
 */
32
class DBQuery{
33
34
	const DIALECTS = [
35
		'mysql'    => 'MySQL',
36
		'pgsql'    => 'Postgres',
37
		'sqlite'   => 'SQLite',
38
		'firebird' => 'Firebird',
39
	];
40
41
	/**
42
	 * @var \chillerlan\Database\Drivers\DBDriverInterface
43
	 */
44
	protected $DBDriver;
45
46
	/**
47
	 * @var string
48
	 */
49
	protected $dialect;
50
51
	/**
52
	 * DBQuery constructor.
53
	 *
54
	 * @param \chillerlan\Database\Drivers\DBDriverInterface $DBDriver
55
	 */
56
	public function __construct(DBDriverInterface $DBDriver){
57
		$this->DBDriver = $DBDriver;
58
59
		$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...
60
61
		mb_internal_encoding('UTF-8');
62
	}
63
64
	/**
65
	 * @param string $name
66
	 *
67
	 * @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...
68
	 */
69
	public function __get(string $name){
70
		$name = strtolower($name);
71
72
		if(in_array($name, ['select', 'insert', 'update', 'delete'])){
73
			$class = $this->dialect.'\\'.ucfirst($name);
74
			return new $class($this->DBDriver, $this->dialect);
75
		}
76
		elseif($name === 'create'){
77
78
			return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements CreateInterface{
79
80
				public function database(string $dbname = null):CreateDatabaseInterface{
81
					return $this->getStatementClass('CreateDatabase')->name($dbname);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface chillerlan\Database\Query\StatementInterface as the method name() does only exist in the following implementations of said interface: chillerlan\Database\Query\CreateDatabaseAbstract, chillerlan\Database\Query\CreateTableAbstract, chillerlan\Database\Quer...Firebird\CreateDatabase, chillerlan\Database\Quer...ts\Firebird\CreateTable, chillerlan\Database\Quer...ts\MySQL\CreateDatabase, chillerlan\Database\Quer...lects\MySQL\CreateTable, chillerlan\Database\Quer...Postgres\CreateDatabase, chillerlan\Database\Quer...ts\Postgres\CreateTable, chillerlan\Database\Quer...s\SQLite\CreateDatabase, chillerlan\Database\Quer...ects\SQLite\CreateTable.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
82
				}
83
84
				public function table(string $tablename = null):CreateTableInterface{
85
					return $this->getStatementClass('CreateTable')->name($tablename);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface chillerlan\Database\Query\StatementInterface as the method name() does only exist in the following implementations of said interface: chillerlan\Database\Query\CreateDatabaseAbstract, chillerlan\Database\Query\CreateTableAbstract, chillerlan\Database\Quer...Firebird\CreateDatabase, chillerlan\Database\Quer...ts\Firebird\CreateTable, chillerlan\Database\Quer...ts\MySQL\CreateDatabase, chillerlan\Database\Quer...lects\MySQL\CreateTable, chillerlan\Database\Quer...Postgres\CreateDatabase, chillerlan\Database\Quer...ts\Postgres\CreateTable, chillerlan\Database\Quer...s\SQLite\CreateDatabase, chillerlan\Database\Quer...ects\SQLite\CreateTable.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
				}
87
88
#				public function index():CreateInterface{}
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
#				public function view():CreateInterface{}
90
#				public function trigger():CreateInterface{}
91
92
			};
93
94
		}
95
		elseif($name === 'alter'){
96
97
			return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements AlterInterface{
98
99
			};
100
101
		}
102
		elseif($name === 'drop'){
103
104
			return new class($this->DBDriver, $this->dialect) extends StatementAbstract implements DropInterface{
105
106
			};
107
108
		}
109
110
		return null;
111
	}
112
113
}
114