Skip to content

Commit

Permalink
Merge pull request #87 from nextras/phpstan
Browse files Browse the repository at this point in the history
Phpstan update to 0.12
  • Loading branch information
hrach authored Mar 22, 2020
2 parents 92c03b7 + 7913464 commit 2bd33e4
Show file tree
Hide file tree
Showing 26 changed files with 314 additions and 145 deletions.
5 changes: 2 additions & 3 deletions .phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ includes:
- phar://%rootDir%/phpstan.phar/conf/bleedingEdge.neon

parameters:
ignoreErrors:
- '#(sqlsrv|SQLSRV).+ not found\.#i'

# never type is not supported: https://github.com/phpstan/phpstan/issues/2297
earlyTerminatingMethodCalls:
Nextras\Dbal\SqlProcessor:
- throwInvalidValueTypeException
- throwWrongModifierException
Nextras\Dbal\Drivers\Sqlsrv\SqlsrvDriver:
- throwErrors
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- 7.4

services:
- mysql
Expand Down Expand Up @@ -45,7 +45,7 @@ before_script:

script:
- ./tests/run.sh -s $NTESTER_FLAGS ./tests/cases
- if [ "$TRAVIS_PHP_VERSION" == "7.2" ]; then vendor/bin/phpstan.phar analyse -l 7 -c .phpstan.neon src; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.2" ]; then composer phpstan; fi

after_script:
- if [ "$TRAVIS_PHP_VERSION" == "7.2" ]; then
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"nette/utils": "~3.0",
"nette/finder": "~2.5",
"nette/neon": "~3.0",
"phpstan/phpstan-shim": "0.11.12",
"phpstan/phpstan": "0.12.17",
"tracy/tracy": "~2.7"
},
"autoload": {
"psr-4": { "Nextras\\Dbal\\": "src/" },
"classmap": ["src/exceptions.php"]
},
"scripts": {
"phpstan": "phpstan analyse -l 7 -c .phpstan.neon src"
},
"extra": {
"branch-alias": {
"dev-master": "4.0-dev"
Expand Down
7 changes: 5 additions & 2 deletions src/Bridges/NetteDI/DbalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@

class DbalExtension extends CompilerExtension
{
public function loadConfiguration()
public function loadConfiguration(): void
{
$config = $this->getConfig();
\assert(is_array($config));
$this->setupConnection($config);
}


protected function setupConnection(array $config)
/**
* @phpstan-param array<mixed> $config
*/
protected function setupConnection(array $config): void
{
$builder = $this->getContainerBuilder();

Expand Down
5 changes: 4 additions & 1 deletion src/Bridges/NetteTracy/BluescreenQueryPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

class BluescreenQueryPanel
{
public static function renderBluescreenPanel($exception)
/**
* @phpstan-return array{tab: string, panel: string}|null
*/
public static function renderBluescreenPanel(?\Throwable $exception): ?array
{
if (!$exception instanceof QueryException || !($query = $exception->getSqlQuery())) {
return null;
Expand Down
11 changes: 7 additions & 4 deletions src/Bridges/NetteTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class ConnectionPanel implements IBarPanel
/** @var float */
private $totalTime;

/** @var array */
/**
* @var array
* @phpstan-var array<array{Connection, string, float, ?int}>
*/
private $queries = [];

/** @var Connection */
Expand All @@ -37,7 +40,7 @@ class ConnectionPanel implements IBarPanel
private $doExplain;


public static function install(Connection $connection, bool $doExplain = true)
public static function install(Connection $connection, bool $doExplain = true): void
{
$doExplain = $doExplain && $connection->getPlatform()->isSupported(IPlatform::SUPPORT_QUERY_EXPLAIN);
Debugger::getBar()->addPanel(new ConnectionPanel($connection, $doExplain));
Expand All @@ -52,7 +55,7 @@ public function __construct(Connection $connection, bool $doExplain)
}


public function logQuery(Connection $connection, string $sql, float $elapsedTime, Result $result = null, DriverException $exception = null)
public function logQuery(Connection $connection, string $sql, float $elapsedTime, Result $result = null, DriverException $exception = null): void
{
$this->count++;
if ($this->count > $this->maxQueries) {
Expand Down Expand Up @@ -106,7 +109,7 @@ public function getPanel(): ?string
}


public static function highlight($sql)
public static function highlight(string $sql): string
{
static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|SHOW|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|COMMIT|ROLLBACK|(?:RELEASE\s+|ROLLBACK\s+TO\s+)?SAVEPOINT';
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|[RI]?LIKE|REGEXP|TRUE|FALSE';
Expand Down
27 changes: 20 additions & 7 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@

class Connection implements IConnection
{
/** @var callable[]: function(Connection $connection) */
/**
* @var callable[]: function(Connection $connection)
* @phpstan-var array<callable(Connection):void>
*/
public $onConnect = [];

/** @var callable[]: function(Connection $connection) */
/**
* @var callable[]: function(Connection $connection)
* @phpstan-var array<callable(Connection):void>
*/
public $onDisconnect = [];

/** @var callable[]: function(Connection $connection, string $query, float $time, ?Result $result, ?DriverException $exception) */
/**
* @var callable[]: function(Connection $connection, string $query, float $time, ?Result $result, ?DriverException $exception)
* @phpstan-var array<callable(Connection, string, float, ?Result, ?DriverException): void>
*/
public $onQuery = [];

/** @var array */
/**
* @var array
* @phpstan-var array<string, mixed>
*/
private $config;

/** @var IDriver */
Expand All @@ -49,6 +61,7 @@ class Connection implements IConnection

/**
* @param array $config see drivers for supported options
* @phpstan-param array<string, mixed> $config
*/
public function __construct(array $config)
{
Expand Down Expand Up @@ -180,7 +193,7 @@ public function createQueryBuilder(): QueryBuilder
}


public function setTransactionIsolationLevel(int $level)
public function setTransactionIsolationLevel(int $level): void
{
$this->driver->setTransactionIsolationLevel($level);
}
Expand Down Expand Up @@ -357,9 +370,9 @@ private function createSqlProcessor(): SqlProcessor


/**
* @return void
* @phpstan-param array<mixed> $args
*/
private function fireEvent(string $event, array $args)
private function fireEvent(string $event, array $args): void
{
foreach ($this->$event as $callback) {
call_user_func_array($callback, $args);
Expand Down
10 changes: 7 additions & 3 deletions src/Drivers/IDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ interface IDriver

/**
* Connects the driver to database.
* @phpstan-param array<string, mixed> $params
* @internal
*/
public function connect(array $params, callable $loggedQueryCallback);
public function connect(array $params, callable $loggedQueryCallback): void;


/**
* Disconnects from the database.
* @internal
*/
public function disconnect();
public function disconnect(): void;


/**
Expand Down Expand Up @@ -105,7 +106,7 @@ public function ping(): bool;
/**
* @internal
*/
public function setTransactionIsolationLevel(int $level);
public function setTransactionIsolationLevel(int $level): void;


/**
Expand Down Expand Up @@ -167,6 +168,9 @@ public function convertToPhp(string $value, $nativeType);
public function convertStringToSql(string $value): string;


/**
* @param mixed $value
*/
public function convertJsonToSql($value): string;


Expand Down
9 changes: 5 additions & 4 deletions src/Drivers/IResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ interface IResultAdapter
/**
* @throws InvalidStateException
*/
public function seek(int $index);
public function seek(int $index): void;


/**
* @return array|null
* @phpstan-return array<mixed>|null
*/
public function fetch();
public function fetch(): ?array;


/**
* Returns rowset set column types, array of [type, nativeType]
* Returns row's column types, array of [type, nativeType]
* @phpstan-return array<string, array{int, mixed}>
*/
public function getTypes(): array;

Expand Down
19 changes: 11 additions & 8 deletions src/Drivers/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ public function __destruct()
}


public function connect(array $params, callable $onQueryCallback)
public function connect(array $params, callable $onQueryCallback): void
{
$this->onQueryCallback = $onQueryCallback;

$host = $params['host'] ?? ini_get('mysqli.default_host');
$port = $params['port'] ?? (int) (ini_get('mysqli.default_port') ?: 3306);
$dbname = $params['database'] ?? '';
$socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket') ?? '';
$socket = ($params['unix_socket'] ?? ini_get('mysqli.default_socket')) ?: '';
$flags = $params['flags'] ?? 0;

$this->connection = new mysqli();

if (!@$this->connection->real_connect($host, $params['username'], (string) $params['password'], $dbname, $port, $socket, $flags)) {
throw $this->createException(
$this->connection->connect_error,
$this->connection->connect_errno,
(int) $this->connection->connect_errno,
@$this->connection->sqlstate ?: 'HY000'
);
}
Expand All @@ -71,7 +71,7 @@ public function connect(array $params, callable $onQueryCallback)
}


public function disconnect()
public function disconnect(): void
{
if ($this->connection) {
$this->connection->close();
Expand Down Expand Up @@ -164,7 +164,7 @@ public function ping(): bool
}


public function setTransactionIsolationLevel(int $level)
public function setTransactionIsolationLevel(int $level): void
{
static $levels = [
Connection::TRANSACTION_READ_UNCOMMITTED => 'READ UNCOMMITTED',
Expand Down Expand Up @@ -215,7 +215,10 @@ public function rollbackSavepoint(string $name): void
}


protected function processInitialSettings(array $params)
/**
* @phpstan-param array<string, mixed> $params
*/
protected function processInitialSettings(array $params): void
{
assert($this->connection !== null);

Expand Down Expand Up @@ -367,7 +370,7 @@ public function modifyLimitQuery(string $query, ?int $limit, ?int $offset): stri
* This method is based on Doctrine\DBAL project.
* @link www.doctrine-project.org
*/
protected function createException($error, int $errorNo, $sqlState, $query = null)
protected function createException(string $error, int $errorNo, string $sqlState, ?string $query = null): \Exception
{
if (in_array($errorNo, [1216, 1217, 1451, 1452, 1701], true)) {
return new ForeignKeyConstraintViolationException($error, $errorNo, $sqlState, null, $query);
Expand All @@ -390,7 +393,7 @@ protected function createException($error, int $errorNo, $sqlState, $query = nul
}


protected function loggedQuery(string $sql)
protected function loggedQuery(string $sql): Result
{
try {
$result = $this->query($sql);
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/Mysqli/MysqliEmptyResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

class MysqliEmptyResultAdapter implements IResultAdapter
{
public function seek(int $index)
public function seek(int $index): void
{
throw new InvalidStateException("Unable to seek in row set to {$index} index.");
}


public function fetch()
public function fetch(): ?array
{
return null;
}
Expand Down
16 changes: 11 additions & 5 deletions src/Drivers/Mysqli/MysqliResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class MysqliResultAdapter implements IResultAdapter
{
/** @var array */
/** @var array<int, int> */
protected static $types = [
MYSQLI_TYPE_TIME => self::TYPE_DRIVER_SPECIFIC,
MYSQLI_TYPE_DATE => self::TYPE_DATETIME,
Expand All @@ -39,10 +39,16 @@ class MysqliResultAdapter implements IResultAdapter
MYSQLI_TYPE_STRING => self::TYPE_AS_IS,
];

/** @var mysqli_result */
/**
* @var mysqli_result
* @phpstan-var mysqli_result<array<mixed>>
*/
private $result;


/**
* @phpstan-param mysqli_result<array<mixed>> $result
*/
public function __construct(mysqli_result $result)
{
$this->result = $result;
Expand All @@ -58,15 +64,15 @@ public function __destruct()
}


public function seek(int $index)
public function seek(int $index): void
{
if ($this->result->num_rows !== 0 && !$this->result->data_seek($index)) {
throw new InvalidStateException("Unable to seek in row set to {$index} index.");
}
}


public function fetch()
public function fetch(): ?array
{
return $this->result->fetch_assoc();
}
Expand All @@ -79,7 +85,7 @@ public function getTypes(): array

for ($i = 0; $i < $count; $i++) {
$field = (array) $this->result->fetch_field_direct($i);
$types[$field['name']] = [
$types[(string) $field['name']] = [
0 => self::$types[$field['type']] ?? self::TYPE_AS_IS,
1 => $field['type'],
];
Expand Down
Loading

0 comments on commit 2bd33e4

Please sign in to comment.