Completed
Push — master ( 117114...c02abd )
by smiley
04:21
created

src/DatabaseAbstract.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Class DatabaseAbstract
4
 *
5
 * @filesource   DatabaseAbstract.php
6
 * @created      20.01.2018
7
 * @package      chillerlan\Database
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database;
14
15
use chillerlan\Database\{
16
	Drivers\DriverInterface, Query\QueryBuilder
17
};
18
use chillerlan\Traits\{
19
	ClassLoader, ImmutableSettingsInterface
20
};
21
use Psr\Log\{
22
	LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger
23
};
24
use Psr\SimpleCache\CacheInterface;
25
26
abstract class DatabaseAbstract implements LoggerAwareInterface{
27
	use ClassLoader, LoggerAwareTrait;
28
29
	/**
30
	 * @var \chillerlan\Database\DatabaseOptions
31
	 */
32
	protected $options;
33
34
	/**
35
	 * @var \Psr\SimpleCache\CacheInterface
36
	 */
37
	protected $cache;
38
39
	/**
40
	 * @var \chillerlan\Database\Drivers\DriverInterface
41
	 */
42
	protected $driver;
43
44
	/**
45
	 * @var \chillerlan\Database\Query\QueryBuilder
46
	 */
47
	protected $query;
48
49
	/**
50
	 * Database constructor.
51
	 *
52
	 * @param \chillerlan\Traits\ImmutableSettingsInterface $options
53
	 * @param \Psr\SimpleCache\CacheInterface|null $cache
54
	 * @param \Psr\Log\LoggerInterface|null        $logger
55
	 */
56
	public function __construct(ImmutableSettingsInterface $options, CacheInterface $cache = null, LoggerInterface $logger = null){
57
		$this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
$options is of type object<chillerlan\Traits...tableSettingsInterface>, but the property $options was declared to be of type object<chillerlan\Database\DatabaseOptions>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
58
		$this->cache   = $cache;
59
60
		// set a default logger
61
		$this->logger  = $logger ?? new NullLogger;
62
63
		$this->driver  = $this->loadClass($this->options->driver, DriverInterface::class, $this->options, $this->cache, $this->logger);
0 ignored issues
show
Accessing driver on the interface chillerlan\Traits\ImmutableSettingsInterface 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...
64
		$this->query   = new QueryBuilder($this->driver, $this->logger);
65
	}
66
67
	/**
68
	 * @param \Psr\Log\LoggerInterface $logger
69
	 *
70
	 * @return void
71
	 */
72
	public function setLogger(LoggerInterface $logger):void{
73
		$this->logger = $logger;
74
		$this->driver->setLogger($logger);
75
		$this->query->setLogger($logger);
76
	}
77
78
}
79