Skip to content

Commit

Permalink
Static analysis: improve typing throughout (#241)
Browse files Browse the repository at this point in the history
* build: update dependencies

* build: update dependencies

* stan: improve types throughout
  • Loading branch information
g105b authored Oct 13, 2021
1 parent db929a7 commit 9288a53
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 287 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",

"require": {
"php": ">=7.4",
"php": ">=8.0",
"ext-PDO": "*",
"phpgt/config": "^v1.1.0",
"phpgt/cli": "^1.3"
Expand Down
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 14 additions & 22 deletions src/Connection/DefaultSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,18 @@ class DefaultSettings implements SettingsInterface {
const DEFAULT_CHARSET = "utf8mb4";
const DEFAULT_COLLATION = "utf8mb4_general_ci";

/** @var string */
protected $baseDirectory;
/** @var string */
protected $driver;
/** @var string */
protected $schema;
/** @var string */
protected $host;
/** @var int */
protected $port;
/** @var string */
protected $username;
/** @var string */
protected $password;
/** @var string */
protected $connectionName;
/** @var array */
protected $config = [];
/** @var string */
protected $charset;
/** @var string */
protected $collation;
protected string $baseDirectory;
protected string $driver;
protected string $schema;
protected string $host;
protected int $port;
protected string $username;
protected string $password;
protected string $connectionName;
/** @var array<string, string> */
protected array $config;
protected string $charset;
protected string $collation;

public function __construct() {
$this->baseDirectory = sys_get_temp_dir();
Expand All @@ -65,6 +55,7 @@ public function __construct() {
$this->username = self::DEFAULT_USERNAME;
$this->password = self::DEFAULT_PASSWORD;
$this->connectionName = self::DEFAULT_NAME;
$this->config = [];
}

public function getBaseDirectory():string {
Expand Down Expand Up @@ -99,6 +90,7 @@ public function getConnectionName():string {
return $this->connectionName;
}

/** @return array<string, string> */
public function getConnectionSettings():array {
// NOTE: It's not possible to test the 'port' values returned by this method
// because the DefaultSettings can only ever return the DEFAULT_DRIVER port
Expand Down
4 changes: 2 additions & 2 deletions src/Connection/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getConnection():Connection {
return $this->connection;
}

protected function connect() {
protected function connect():void {
$options = [
Connection::ATTR_ERRMODE => Connection::ERRMODE_EXCEPTION,
];
Expand All @@ -59,4 +59,4 @@ protected function connect() {
$options
);
}
}
}
5 changes: 2 additions & 3 deletions src/Connection/ImmutableSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function withConnectionName(string $connectionName):SettingsInterface {
}

public function withoutSchema():SettingsInterface {
$clone = $this->withSchema("");
return $clone;
return $this->withSchema("");
}

public function withCharset(string $charset):SettingsInterface {
Expand All @@ -107,4 +106,4 @@ public function withCollation(string $collation):SettingsInterface {
$clone->collation = $collation;
return $clone;
}
}
}
6 changes: 0 additions & 6 deletions src/Connection/NoConnectionException.php

This file was deleted.

44 changes: 18 additions & 26 deletions src/Connection/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,30 @@ class Settings implements SettingsInterface {

const SCHEMA_IN_MEMORY = ":memory:";

/** @var string */
protected $baseDirectory;
/** @var string */
protected $driver;
/** @var string */
protected $schema;
/** @var string */
protected $host;
/** @var int */
protected $port;
/** @var string */
protected $username;
/** @var string */
protected $password;
/** @var string */
protected $connectionName;
/** @var array */
protected $config = [];
/** @var string */
protected $collation;
/** @var string */
protected $charset;
protected string $baseDirectory;
protected string $driver;
protected ?string $schema;
protected string $host;
protected int $port;
protected string $username;
protected string $password;
protected string $connectionName;
/** @var array<string, string> */
protected array $config = [];
protected string $collation;
protected ?string $charset;

public function __construct(
string $baseDirectory,
string $driver,
string $schema = null,
?string $schema = null,
string $host = DefaultSettings::DEFAULT_HOST,
int $port = null,
string $username = DefaultSettings::DEFAULT_USERNAME,
string $password = DefaultSettings::DEFAULT_PASSWORD,
string $connectionName = DefaultSettings::DEFAULT_NAME,
string $collation = DefaultSettings::DEFAULT_COLLATION,
string $charset = null
?string $charset = null
) {
if(is_null($port)) {
$port = DefaultSettings::DEFAULT_PORT[$driver];
Expand All @@ -57,15 +47,16 @@ public function __construct(
$this->driver = $driver;
$this->schema = $schema;
$this->host = $host;
$this->port = $port;
$this->port = $port ?? 0;
$this->username = $username;
$this->password = $password;
$this->connectionName = $connectionName;
$this->collation = $collation;
$this->charset = $charset;
}

public function setConfig(array $config) {
/** @param array<string, string> $config */
public function setConfig(array $config):void {
$this->config = $config;
}

Expand Down Expand Up @@ -101,6 +92,7 @@ public function getConnectionName():string {
return $this->connectionName;
}

/** @return array<string, string> */
public function getConnectionSettings():array {
$currentSettings = [
"driver" => $this->getDriver(),
Expand Down
33 changes: 17 additions & 16 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class Database {
use Fetchable;

const COLLECTION_SEPARATOR_CHARACTERS = [".", "/", "\\"];
/** @var QueryCollectionFactory[] */
protected $queryCollectionFactoryArray;
/** @var Driver[] */
protected $driverArray;
/** @var Connection */
protected $currentConnectionName;
/** @var array<QueryCollectionFactory> */
protected array $queryCollectionFactoryArray;
/** @var array<Driver> */
protected array $driverArray;
protected Connection $currentConnectionName;

public function __construct(SettingsInterface...$connectionSettings) {
if(empty($connectionSettings)) {
Expand All @@ -37,25 +36,24 @@ public function __construct(SettingsInterface...$connectionSettings) {
$this->storeQueryCollectionFactoryFromSettings($connectionSettings);
}

public function insert(string $queryName, ...$bindings):int {
public function insert(string $queryName, mixed...$bindings):string {
$result = $this->query($queryName, $bindings);

return $result->lastInsertId();
}

public function delete(string $queryName, ...$bindings):int {
public function delete(string $queryName, mixed...$bindings):int {
$result = $this->query($queryName, $bindings);

return $result->affectedRows();
}

public function update(string $queryName, ...$bindings):int {
public function update(string $queryName, mixed...$bindings):int {
$result = $this->query($queryName, $bindings);

return $result->affectedRows();
}

public function query(string $fullQueryPath, ...$bindings):ResultSet {
public function query(string $fullQueryPath, mixed...$bindings):ResultSet {
$queryCollectionName = $queryFile = "";

foreach(self::COLLECTION_SEPARATOR_CHARACTERS as $char) {
if(!strstr($fullQueryPath, $char)) {
continue;
Expand Down Expand Up @@ -83,12 +81,13 @@ public function query(string $fullQueryPath, ...$bindings):ResultSet {
return $queryCollection->query($queryFile, $bindings);
}

public function setCurrentConnectionName(string $connectionName) {
public function setCurrentConnectionName(string $connectionName):void {
$this->currentConnectionName = $this->getNamedConnection(
$connectionName
);
}

/** @param array<string, mixed>|array<mixed> $bindings */
public function executeSql(
string $query,
array $bindings = [],
Expand Down Expand Up @@ -124,14 +123,16 @@ protected function getNamedConnection(string $connectionName):Connection {
return $driver->getConnection();
}

protected function storeConnectionDriverFromSettings(array $settingsArray) {
/** @param array<SettingsInterface> $settingsArray */
protected function storeConnectionDriverFromSettings(array $settingsArray):void {
foreach($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->driverArray[$connectionName] = new Driver($settings);
}
}

protected function storeQueryCollectionFactoryFromSettings(array $settingsArray) {
/** @param array<SettingsInterface> $settingsArray */
protected function storeQueryCollectionFactoryFromSettings(array $settingsArray):void {
foreach($settingsArray as $settings) {
$connectionName = $settings->getConnectionName();
$this->queryCollectionFactoryArray[$connectionName] =
Expand Down
Loading

0 comments on commit 9288a53

Please sign in to comment.