Skip to content

Commit

Permalink
added PHP 7.1 scalar and return type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 26, 2017
1 parent 03cb6dc commit 13bc0e9
Show file tree
Hide file tree
Showing 34 changed files with 327 additions and 680 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/DatabaseDI/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DatabaseExtension extends Nette\DI\CompilerExtension
private $debugMode;


public function __construct($debugMode = FALSE)
public function __construct(bool $debugMode = FALSE)
{
$this->debugMode = $debugMode;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Bridges/DatabaseTracy/ConnectionPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static function renderException($e)
}


public function getTab()
public function getTab(): string
{
$name = $this->name;
$count = $this->count;
Expand All @@ -110,11 +110,11 @@ public function getTab()
}


public function getPanel()
public function getPanel(): ?string
{
$this->disabled = TRUE;
if (!$this->count) {
return;
return NULL;
}

$name = $this->name;
Expand Down
36 changes: 9 additions & 27 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ public function getSupplementalDriver()

/**
* @param string sequence object
* @return string
*/
public function getInsertId($name = NULL)
public function getInsertId(string $name = NULL): string
{
try {
$res = $this->getPdo()->lastInsertId($name);
Expand All @@ -134,9 +133,8 @@ public function getInsertId($name = NULL)
/**
* @param string string to be quoted
* @param int data type hint
* @return string
*/
public function quote($string, $type = PDO::PARAM_STR)
public function quote(string $string, int $type = PDO::PARAM_STR): string
{
try {
return $this->getPdo()->quote($string, $type);
Expand Down Expand Up @@ -169,10 +167,8 @@ public function rollBack()

/**
* Generates and executes SQL query.
* @param string
* @return ResultSet
*/
public function query($sql, ...$params)
public function query(string $sql, ...$params): ResultSet
{
[$sql, $params] = $this->preprocess($sql, ...$params);
try {
Expand All @@ -186,11 +182,7 @@ public function query($sql, ...$params)
}


/**
* @param string
* @return ResultSet
*/
public function queryArgs($sql, array $params)
public function queryArgs(string $sql, array $params): ResultSet
{
return $this->query($sql, ...$params);
}
Expand All @@ -213,52 +205,42 @@ public function preprocess($sql, ...$params)

/**
* Shortcut for query()->fetch()
* @param string
* @return Row|NULL
*/
public function fetch($sql, ...$params)
public function fetch(string $sql, ...$params): ?Row
{
return $this->query($sql, ...$params)->fetch();
}


/**
* Shortcut for query()->fetchField()
* @param string
* @return mixed
*/
public function fetchField($sql, ...$params)
public function fetchField(string $sql, ...$params)
{
return $this->query($sql, ...$params)->fetchField();
}


/**
* Shortcut for query()->fetchPairs()
* @param string
* @return array
*/
public function fetchPairs($sql, ...$params)
public function fetchPairs(string $sql, ...$params): array
{
return $this->query($sql, ...$params)->fetchPairs();
}


/**
* Shortcut for query()->fetchAll()
* @param string
* @return array
*/
public function fetchAll($sql, ...$params)
public function fetchAll(string $sql, ...$params): array
{
return $this->query($sql, ...$params)->fetchAll();
}


/**
* @return SqlLiteral
*/
public static function literal($value, ...$params)
public static function literal($value, ...$params): SqlLiteral
{
return new SqlLiteral($value, $params);
}
Expand Down
39 changes: 9 additions & 30 deletions src/Database/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,29 @@ public function rollBack()

/**
* @param string sequence object
* @return string
*/
public function getInsertId($name = NULL)
public function getInsertId(string $name = NULL): string
{
return $this->connection->getInsertId($name);
}


/**
* Generates and executes SQL query.
* @param string
* @return ResultSet
*/
public function query($sql, ...$params)
public function query(string $sql, ...$params): ResultSet
{
return $this->connection->query($sql, ...$params);
}


/**
* @param string
* @return ResultSet
*/
public function queryArgs($sql, array $params)
public function queryArgs(string $sql, array $params): ResultSet
{
return $this->connection->query($sql, ...$params);
}


/**
* @param string
* @return Table\Selection
*/
public function table($table)
public function table(string $table): Table\Selection
{
return new Table\Selection($this, $this->conventions, $table, $this->cacheStorage);
}
Expand Down Expand Up @@ -130,52 +119,42 @@ public function getConventions()

/**
* Shortcut for query()->fetch()
* @param string
* @return Row|NULL
*/
public function fetch($sql, ...$params)
public function fetch(string $sql, ...$params): ?Row
{
return $this->connection->query($sql, ...$params)->fetch();
}


/**
* Shortcut for query()->fetchField()
* @param string
* @return mixed
*/
public function fetchField($sql, ...$params)
public function fetchField(string $sql, ...$params)
{
return $this->connection->query($sql, ...$params)->fetchField();
}


/**
* Shortcut for query()->fetchPairs()
* @param string
* @return array
*/
public function fetchPairs($sql, ...$params)
public function fetchPairs(string $sql, ...$params): array
{
return $this->connection->query($sql, ...$params)->fetchPairs();
}


/**
* Shortcut for query()->fetchAll()
* @param string
* @return array
*/
public function fetchAll($sql, ...$params)
public function fetchAll(string $sql, ...$params): array
{
return $this->connection->query($sql, ...$params)->fetchAll();
}


/**
* @return SqlLiteral
*/
public static function literal($value, ...$params)
public static function literal($value, ...$params): SqlLiteral
{
return new SqlLiteral($value, $params);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Conventions/DiscoveredConventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function __construct(IStructure $structure)
}


public function getPrimary($table)
public function getPrimary(string $table)
{
return $this->structure->getPrimaryKey($table);
}


public function getHasManyReference($nsTable, $key)
public function getHasManyReference(string $nsTable, string $key): ?array
{
$candidates = $columnCandidates = [];
$targets = $this->structure->getHasManyReference($nsTable);
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getHasManyReference($nsTable, $key)
}


public function getBelongsToReference($table, $key)
public function getBelongsToReference(string $table, string $key): ?array
{
$tableColumns = $this->structure->getBelongsToReference($table);

Expand Down
10 changes: 5 additions & 5 deletions src/Database/Conventions/StaticConventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ class StaticConventions implements IConventions
* @param string %1$s stands for key used after ->, %2$s for table name
* @param string %1$s stands for key used after ->, %2$s for table name
*/
public function __construct($primary = 'id', $foreign = '%s_id', $table = '%s')
public function __construct(string $primary = 'id', string $foreign = '%s_id', string $table = '%s')
{
$this->primary = $primary;
$this->foreign = $foreign;
$this->table = $table;
}


public function getPrimary($table)
public function getPrimary(string $table)
{
return sprintf($this->primary, $this->getColumnFromTable($table));
}


public function getHasManyReference($table, $key)
public function getHasManyReference(string $table, string $key): ?array
{
$table = $this->getColumnFromTable($table);
return [
Expand All @@ -60,7 +60,7 @@ public function getHasManyReference($table, $key)
}


public function getBelongsToReference($table, $key)
public function getBelongsToReference(string $table, string $key): ?array
{
$table = $this->getColumnFromTable($table);
return [
Expand All @@ -70,7 +70,7 @@ public function getBelongsToReference($table, $key)
}


protected function getColumnFromTable($name)
protected function getColumnFromTable(string $name)
{
if ($this->table !== '%s' && preg_match('(^' . str_replace('%s', '(.*)', preg_quote($this->table)) . '\z)', $name, $match)) {
return $match[1];
Expand Down
4 changes: 2 additions & 2 deletions src/Database/DriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getDriverCode()
/**
* @return string|NULL SQLSTATE error code
*/
public function getSqlState()
public function getSqlState(): ?string
{
return $this->errorInfo[0] ?? NULL;
}
Expand All @@ -58,7 +58,7 @@ public function getSqlState()
/**
* @return string|NULL SQL command
*/
public function getQueryString()
public function getQueryString(): ?string
{
return $this->queryString;
}
Expand Down
Loading

0 comments on commit 13bc0e9

Please sign in to comment.