chillerlan /
php-database
| 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\Settings\SettingsContainerInterface; |
||
| 19 | use Psr\Log\{ |
||
| 20 | LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger |
||
| 21 | }; |
||
| 22 | use Psr\SimpleCache\CacheInterface; |
||
| 23 | |||
| 24 | abstract class DatabaseAbstract implements LoggerAwareInterface{ |
||
| 25 | use LoggerAwareTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \chillerlan\Database\DatabaseOptions |
||
| 29 | */ |
||
| 30 | protected $options; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \Psr\SimpleCache\CacheInterface |
||
| 34 | */ |
||
| 35 | protected $cache; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \chillerlan\Database\Drivers\DriverInterface |
||
| 39 | */ |
||
| 40 | protected $driver; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \chillerlan\Database\Query\QueryBuilder |
||
| 44 | */ |
||
| 45 | protected $query; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Database constructor. |
||
| 49 | * |
||
| 50 | * @param \chillerlan\Settings\SettingsContainerInterface $options |
||
| 51 | * @param \Psr\SimpleCache\CacheInterface|null $cache |
||
| 52 | * @param \Psr\Log\LoggerInterface|null $logger |
||
| 53 | * |
||
| 54 | * @throws \chillerlan\Database\DatabaseException |
||
| 55 | */ |
||
| 56 | public function __construct(SettingsContainerInterface $options, CacheInterface $cache = null, LoggerInterface $logger = null){ |
||
| 57 | $this->options = $options; |
||
|
0 ignored issues
–
show
|
|||
| 58 | $this->cache = $cache; |
||
| 59 | |||
| 60 | // set a default logger |
||
| 61 | $this->logger = $logger ?? new NullLogger; |
||
| 62 | $this->driver = new $this->options->driver($this->options, $this->cache, $this->logger); |
||
| 63 | |||
| 64 | if(!$this->driver instanceof DriverInterface){ |
||
| 65 | throw new DatabaseException('invalid driver interface'); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->query = new QueryBuilder($this->driver, $this->logger); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param \Psr\Log\LoggerInterface $logger |
||
| 73 | * |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | public function setLogger(LoggerInterface $logger):void{ |
||
| 77 | $this->logger = $logger; |
||
| 78 | $this->driver->setLogger($logger); |
||
| 79 | $this->query->setLogger($logger); |
||
| 80 | } |
||
| 81 | |||
| 82 | } |
||
| 83 |
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.