Skip to content

Commit

Permalink
added type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 14, 2023
1 parent 790b1e2 commit 39abfac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/Database/Table/ActiveRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function offsetUnset($column): void
}


public function __set($column, $value)
public function __set(string $column, $value): void
{
throw new Nette\DeprecatedException('ActiveRow is read-only; use update() method instead.');
}
Expand Down Expand Up @@ -288,7 +288,7 @@ public function &__get(string $key)
}


public function __isset($key)
public function __isset(string $key): bool
{
if ($this->accessColumn($key)) {
return isset($this->data[$key]);
Expand All @@ -305,7 +305,7 @@ public function __isset($key)
}


public function __unset($key)
public function __unset(string $key): void
{
throw new Nette\DeprecatedException('ActiveRow is read-only.');
}
Expand All @@ -314,7 +314,7 @@ public function __unset($key)
/**
* @internal
*/
public function accessColumn($key, bool $selectColumn = true): bool
public function accessColumn(?string $key, bool $selectColumn = true): bool
{
if ($this->table->accessColumn($key, $selectColumn) && !$this->dataRefreshed) {
if (!isset($this->table[$this->getSignature()])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Table/GroupedSelection.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function setActive($active)
/**
* @return static
*/
public function select($columns, ...$params)
public function select(string $columns, ...$params)
{
if (!$this->sqlBuilder->getSelect()) {
$this->sqlBuilder->addSelect("$this->name.$this->column");
Expand Down
7 changes: 2 additions & 5 deletions src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function fetchAssoc(string $path): array
* @param string $columns for example "column, MD5(column) AS column_md5"
* @return static
*/
public function select($columns, ...$params)
public function select(string $columns, ...$params)
{
$this->emptyResultSet();
$this->sqlBuilder->addSelect($columns, ...$params);
Expand Down Expand Up @@ -337,7 +337,7 @@ public function joinWhere(string $tableChain, string $condition, ...$params)
* Adds condition, more calls appends with AND.
* @param string|string[] $condition possibly containing ?
*/
protected function condition($condition, array $params, $tableChain = null): void
protected function condition($condition, array $params, ?string $tableChain = null): void
{
$this->emptyResultSet();
if (is_array($condition) && $params === []) { // where(['column1' => 1, 'column2 > ?' => 2])
Expand Down Expand Up @@ -894,9 +894,6 @@ public function update(iterable $data): int
{
if ($data instanceof \Traversable) {
$data = iterator_to_array($data);

} elseif (!is_array($data)) {
throw new Nette\InvalidArgumentException;
}

if (!$data) {
Expand Down
29 changes: 15 additions & 14 deletions src/Database/Table/SqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,8 @@ public function importGroupConditions(self $builder): bool
/********************* SQL selectors ****************d*g**/


public function addSelect($columns, ...$params): void
public function addSelect(string $columns, ...$params): void
{
if (is_array($columns)) {
throw new Nette\InvalidArgumentException('Select column must be a string.');
}

$this->select[] = $columns;
$this->parameters['select'] = array_merge($this->parameters['select'], $params);
}
Expand Down Expand Up @@ -323,7 +319,7 @@ protected function addCondition($condition, array $params, array &$conditions, a
return $this->addConditionComposition($condition, $params[0], $conditions, $conditionsParameters);
}

$hash = $this->getConditionHash($condition, $params);
$hash = $this->getConditionHash(is_string($condition) ? $condition : '', $params);
if (isset($this->conditions[$hash])) {
return false;
}
Expand Down Expand Up @@ -373,7 +369,7 @@ protected function addCondition($condition, array $params, array &$conditions, a
if (!$clone->getSqlBuilder()->select) {
try {
$clone->select($clone->getPrimary());
} catch (\LogicException $e) {
} catch (\LogicException | \TypeError $e) {
throw new Nette\InvalidArgumentException('Selection argument must have defined a select column.', 0, $e);
}
}
Expand Down Expand Up @@ -527,7 +523,7 @@ public function getGroup(): string
}


public function setHaving($having, ...$params): void
public function setHaving(string $having, ...$params): void
{
$this->having = $having;
$this->parameters['having'] = $params;
Expand All @@ -549,7 +545,7 @@ protected function buildSelect(array $columns): string
}


protected function parseJoinConditions(&$joins, $joinConditions): array
protected function parseJoinConditions(array &$joins, array $joinConditions): array
{
$tableJoins = $leftJoinDependency = $finalJoinConditions = [];
foreach ($joinConditions as $tableChain => $joinCondition) {
Expand Down Expand Up @@ -585,7 +581,12 @@ protected function parseJoinConditions(&$joins, $joinConditions): array
}


protected function getSortedJoins(string $table, &$leftJoinDependency, &$tableJoins, &$finalJoins): void
protected function getSortedJoins(
string $table,
array &$leftJoinDependency,
array &$tableJoins,
array &$finalJoins
): void
{
if (isset($this->expandingJoins[$table])) {
$path = implode("' => '", array_map(function (string $value): string { return $this->reservedTableNames[$value]; }, array_merge(array_keys($this->expandingJoins), [$table])));
Expand Down Expand Up @@ -630,7 +631,7 @@ protected function getSortedJoins(string $table, &$leftJoinDependency, &$tableJo
}


protected function parseJoins(&$joins, &$query): void
protected function parseJoins(array &$joins, string &$query): void
{
$query = preg_replace_callback($this->getColumnChainsRegxp(), function (array $match) use (&$joins): string {
return $this->parseJoinsCb($joins, $match);
Expand All @@ -646,7 +647,7 @@ private function getColumnChainsRegxp(): string
}


public function parseJoinsCb(&$joins, $match): string
public function parseJoinsCb(array &$joins, array $match): string
{
$chain = $match['chain'];
if (!empty($chain[0]) && ($chain[0] !== '.' && $chain[0] !== ':')) {
Expand Down Expand Up @@ -843,7 +844,7 @@ protected function addConditionComposition(
}


private function getConditionHash($condition, array $parameters): string
private function getConditionHash(string $condition, array $parameters): string
{
foreach ($parameters as $key => &$parameter) {
if ($parameter instanceof Selection) {
Expand All @@ -853,7 +854,7 @@ private function getConditionHash($condition, array $parameters): string
} elseif (is_object($parameter) && method_exists($parameter, '__toString')) {
$parameter = $parameter->__toString();
} elseif (is_array($parameter) || $parameter instanceof \ArrayAccess) {
$parameter = $this->getConditionHash($key, $parameter);
$parameter = $this->getConditionHash((string) $key, $parameter);
}
}

Expand Down

0 comments on commit 39abfac

Please sign in to comment.