Skip to content

Commit

Permalink
Merge branch 'phpstan9'
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Dec 14, 2017
2 parents ed8587f + de14b67 commit e6d582a
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
ignoreErrors:
- '#(sqlsrv|SQLSRV).+ not found\.#i'
15 changes: 9 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- 7.0
- 7.1
- 7.2

matrix:
fast_finish: true
Expand All @@ -21,8 +22,8 @@ before_script:
# Create php.ini & databases.ini
- cp ./tests/php.unix-sample.ini ./tests/php.ini

- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then cat ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini >> ./tests/php.ini; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then NTESTER_FLAGS="--coverage ./coverage.xml --coverage-src ./src"; else TESTER_FLAGS=""; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.2" ]; then cat ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini >> ./tests/php.ini; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.2" ]; then NTESTER_FLAGS="--coverage ./coverage.xml --coverage-src ./src"; else TESTER_FLAGS=""; fi
- cp ./tests/databases.sample.ini ./tests/databases.ini

# Create MySQL & Postgre database
Expand All @@ -37,12 +38,14 @@ before_script:
- phpenv config-rm xdebug.ini
- travis_retry composer update --no-interaction

script: ./tests/run.sh -s $NTESTER_FLAGS ./tests/cases
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

after_script:
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then
wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
&& php coveralls.phar --verbose --config tests/.coveralls.yml
- if [ "$TRAVIS_PHP_VERSION" == "7.2" ]; then
wget https://github.com/satooshi/php-coveralls/releases/download/v2.0.0/php-coveralls.phar
&& php php-coveralls.phar --verbose --config tests/.coveralls.yml
|| true;
fi

Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"php": ">=7.0"
},
"require-dev": {
"nette/tester": "~1.7",
"nette/caching": "~2.2",
"mockery/mockery": "~1.0@dev",
"tracy/tracy": "~2.3",
"nette/di": "~2.3"
"nette/tester": "~2.0.0",
"nette/caching": "~2.2",
"nette/di": "~2.3",
"phpstan/phpstan-shim": "~0.9.1",
"tracy/tracy": "~2.3"
},
"autoload": {
"psr-4": { "Nextras\\Dbal\\": "src/" },
Expand Down
14 changes: 10 additions & 4 deletions src/Bridges/NetteTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ public function getPanel()
{
$count = $this->count;
$queries = $this->queries;
$queries = array_map(function($row) {
$queries = array_map(function ($row) {
try {
$row[4] = $this->doExplain ? $this->connection->getDriver()->query('EXPLAIN ' . $row['1'])->fetchAll() : null;
$row[4] = null;
if ($this->doExplain) {
$explain = $this->connection->getDriver()->query('EXPLAIN ' . $row['1']);
if ($explain !== null) {
$row[4] = $explain->fetchAll();
}
}
} catch (\Throwable $e) {
$row[4] = null;
$row[3] = null; // rows count is also irrelevant
Expand All @@ -109,8 +115,8 @@ public static function highlight($sql)
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|IGNORE|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|[RI]?LIKE|REGEXP|TRUE|FALSE';

$sql = " $sql ";
$sql = htmlSpecialChars($sql, ENT_IGNORE, 'UTF-8');
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", function($matches) {
$sql = htmlspecialchars($sql, ENT_IGNORE, 'UTF-8');
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", function ($matches) {
if (!empty($matches[1])) { // comment
return '<em style="color:gray">' . $matches[1] . '</em>';
} elseif (!empty($matches[2])) { // most important keywords
Expand Down
17 changes: 16 additions & 1 deletion src/Drivers/IDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ interface IDriver

/**
* Connects the driver to database.
* @internal
*/
public function connect(array $params, callable $loggedQueryCallback);


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

Expand All @@ -54,21 +56,24 @@ public function getResourceHandle();


/**
* Runs query and returns the result. Returns null if query does not select data.
* Runs query and returns a result. Returns a null if the query does not select any data.
* @internal
* @return Result|null
*/
public function query(string $query);


/**
* Returns the last inserted id.
* @internal
* @return mixed
*/
public function getLastInsertedId(string $sequenceName = null);


/**
* Returns number of affected rows.
* @internal
*/
public function getAffectedRows(): int;

Expand All @@ -93,36 +98,44 @@ public function getServerVersion(): string;

/**
* Pings server.
* @internal
*/
public function ping(): bool;


/**
* @internal
*/
public function setTransactionIsolationLevel(int $level);


/**
* Begins a transaction.
* @internal
* @throws DriverException
*/
public function beginTransaction();


/**
* Commits the current transaction.
* @internal
* @throws DriverException
*/
public function commitTransaction();


/**
* Rollbacks the current transaction.
* @internal
* @throws DriverException
*/
public function rollbackTransaction();


/**
* Creates a savepoint.
* @internal
* @throws DriverException
* @return void
*/
Expand All @@ -131,13 +144,15 @@ public function createSavepoint(string $name);

/**
* Releases the savepoint.
* @internal
* @throws DriverException
*/
public function releaseSavepoint(string $name);


/**
* Rollbacks the savepoint.
* @internal
* @throws DriverException
*/
public function rollbackSavepoint(string $name);
Expand Down
17 changes: 15 additions & 2 deletions src/Drivers/Mysqli/MysqliDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class MysqliDriver implements IDriver
{
/** @var mysqli */
/** @var mysqli|null */
private $connection;

/** @var DateTimeZone Timezone for database connection. */
Expand Down Expand Up @@ -86,14 +86,19 @@ public function isConnected(): bool
}


public function getResourceHandle(): mysqli
/**
* @return mysqli|null
*/
public function getResourceHandle()
{
return $this->connection;
}


public function query(string $query)
{
assert($this->connection !== null);

$time = microtime(true);
$result = @$this->connection->query($query);
$this->timeTaken = microtime(true) - $time;
Expand All @@ -117,12 +122,14 @@ public function query(string $query)

public function getLastInsertedId(string $sequenceName = null)
{
assert($this->connection !== null);
return $this->connection->insert_id;
}


public function getAffectedRows(): int
{
assert($this->connection !== null);
return $this->connection->affected_rows;
}

Expand All @@ -141,6 +148,7 @@ public function createPlatform(Connection $connection): IPlatform

public function getServerVersion(): string
{
assert($this->connection !== null);
$version = $this->connection->server_version;
$majorVersion = floor($version / 10000);
$minorVersion = floor(($version - $majorVersion * 10000) / 100);
Expand All @@ -151,6 +159,7 @@ public function getServerVersion(): string

public function ping(): bool
{
assert($this->connection !== null);
return $this->connection->ping();
}

Expand Down Expand Up @@ -208,6 +217,8 @@ public function rollbackSavepoint(string $name)

protected function processInitialSettings(array $params)
{
assert($this->connection !== null);

if (isset($params['charset'])) {
$charset = $params['charset'];
} elseif (($version = $this->getServerVersion()) && version_compare($version, '5.5.3', '>=')) {
Expand Down Expand Up @@ -259,6 +270,7 @@ public function convertToPhp(string $value, $nativeType)

public function convertStringToSql(string $value): string
{
assert($this->connection !== null);
return "'" . $this->connection->escape_string($value) . "'";
}

Expand Down Expand Up @@ -326,6 +338,7 @@ public function convertDateIntervalToSql(\DateInterval $value): string

public function convertBlobToSql(string $value): string
{
assert($this->connection !== null);
return "_binary'" . $this->connection->escape_string($value) . "'";
}

Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Pgsql/PgsqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class PgsqlDriver implements IDriver
{
/** @var resource */
/** @var resource|null */
private $connection;

/** @var DateTimeZone Timezone for database connection. */
Expand Down
4 changes: 2 additions & 2 deletions src/Drivers/Sqlsrv/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class SqlsrvDriver implements IDriver
{
/** @var resource */
/** @var resource|null */
private $connection;

/** @var callable */
Expand Down Expand Up @@ -76,7 +76,7 @@ public function connect(array $params, callable $onQueryCallback)
}
}

if (isset($connectionInfo['ReturnDatesAsStrings'])) {
if (isset($connectionOptions['ReturnDatesAsStrings'])) {
throw new NotSupportedException("SqlsrvDriver does not allow to modify 'ReturnDatesAsStrings' parameter.");
}
$connectionOptions['ReturnDatesAsStrings'] = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Sqlsrv/SqlsrvResultAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SqlsrvResultAdapter implements IResultAdapter
SqlsrvResultTypes::TYPE_DATETIMEOFFSET => self::TYPE_DRIVER_SPECIFIC,
];

/** @var int */
/** @var int|null */
private $index;

/** @var resource */
Expand Down
9 changes: 3 additions & 6 deletions src/QueryBuilder/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class QueryBuilder
/** @var array|null */
private $join;

/** @var array|null */
/** @var string|null */
private $where;

/** @var array|null */
private $group;

/** @var array|null */
/** @var string|null */
private $having;

/** @var array|null */
Expand All @@ -62,7 +62,7 @@ class QueryBuilder
/** @var array|null */
private $limit;

/** @var string */
/** @var string|null */
private $generatedSql;


Expand Down Expand Up @@ -263,9 +263,6 @@ public function andWhere(string $expression, ...$args): self

/**
* Adds expression with OR to WHERE clause.
* @param string $expression
* @param mixed ...$arg
* @return self
*/
public function orWhere(string $expression, ...$args): self
{
Expand Down

0 comments on commit e6d582a

Please sign in to comment.