Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type hints & support for php 8.1+ #25

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class PDO extends NativePdo
* Logged queries.
* @var array<array>
*/
protected $log = [];
protected array $log = [];

/**
* @inheritDoc
*/
public function __construct($dsn, $username = null, $passwd = null, $options = null)
public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null)
{
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(self::ATTR_STATEMENT_CLASS, [PDOStatement::class, [$this]]);
Expand All @@ -26,7 +26,7 @@ public function __construct($dsn, $username = null, $passwd = null, $options = n
/**
* @inheritDoc
*/
public function exec($statement)
public function exec(string $statement): int|false
{
$start = microtime(true);
$result = parent::exec($statement);
Expand All @@ -38,7 +38,7 @@ public function exec($statement)
/**
* @inheritDoc
*/
public function query($statement, $mode = PDO::FETCH_ASSOC, ...$ctorargs)
public function query(string $statement, ?int $mode = PDO::FETCH_ASSOC, ...$ctorargs): PDOStatement|false
{
$start = microtime(true);
$result = parent::query($statement, $mode, ...$ctorargs);
Expand Down
16 changes: 8 additions & 8 deletions src/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ protected function __construct(PDO $pdo)
* @inheritDoc
*/
public function bindParam(
$param,
&$var,
$type = PDO::PARAM_STR,
$maxLength = null,
$driverOptions = null
) {
int|string $param,
mixed &$var,
int $type = PDO::PARAM_STR,
int $maxLength = 0,
mixed $driverOptions = null
): bool {
$this->bindings[$param] = $var;
return parent::bindParam($param, $var, $type, $maxLength, $driverOptions);
}

/**
* @inheritDoc
*/
public function bindValue($param, $value, $type = PDO::PARAM_STR)
public function bindValue(string|int $param, mixed $value, int $type = PDO::PARAM_STR): bool
{
$this->bindings[$param] = $value;
return parent::bindValue($param, $value, $type);
Expand All @@ -49,7 +49,7 @@ public function bindValue($param, $value, $type = PDO::PARAM_STR)
/**
* @inheritDoc
*/
public function execute($params = null)
public function execute(?array $params = null): bool
{
if (is_array($params)) {
$this->bindings = $params;
Expand Down
25 changes: 8 additions & 17 deletions src/Tracy/BarPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,25 @@ class BarPanel implements IBarPanel

/**
* Title
* @var string
*/
public $title = 'PDO logger';
public string $title = 'PDO logger';

/**
* Title HTML attributes
* @var string
*/
public $title_attributes = 'style="font-size:1.6em"';
public string $title_attributes = 'style="font-size:1.6em"';

/**
* Time table cell HTML attributes
* @var string
*/
public $time_attributes = 'style="font-weight:bold;color:#333;font-family:Courier New;font-size:1.1em"';
public string $time_attributes = 'style="font-weight:bold;color:#333;font-family:Courier New;font-size:1.1em"';

/**
* Query table cell HTML attributes
* @var string
*/
public $query_attributes = '';
public string $query_attributes = '';

/**
* @var PDO
*/
private $pdo;
private PDO $pdo;

public function __construct(PDO $pdo)
{
Expand All @@ -55,18 +48,16 @@ public function __construct(PDO $pdo)

/**
* Get total queries execution time
* @return string
*/
protected function getTotalTime()
protected function getTotalTime(): string
{
return (string) round(array_sum(array_column($this->pdo->getLog(), 'time')), 4);
}

/**
* Renders HTML code for custom tab.
* @return string
*/
public function getTab()
public function getTab(): string
{
$html = '<img src="'.$this->icon.'" alt="PDO queries logger" /> ';
$queries = count($this->pdo->getLog());
Expand All @@ -86,7 +77,7 @@ public function getTab()
* Renders HTML code for custom panel.
* @return string
*/
public function getPanel()
public function getPanel(): string
{
if (class_exists('\SqlFormatter')) {
\SqlFormatter::$pre_attributes = 'style="color: black;"';
Expand Down