Skip to content

Commit

Permalink
Remove unused code in Command class + Cleanup psalm annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Nov 13, 2023
1 parent 8b652d1 commit b5ed5c1
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.1.1 under development

- no changes in this release.
- Enh #302: Remove unused code in `Command` class (@vjik)

## 1.1.0 November 12, 2023

Expand Down
1 change: 0 additions & 1 deletion src/AbstractTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function tokenize(): SqlToken
$token[] = (new SqlToken())->type(SqlToken::TYPE_STATEMENT);

$this->tokenStack->push($token[0]);
/** @psalm-var SqlToken */
$this->currentToken = $this->tokenStack->top();
$length = 0;

Check warning on line 105 in src/AbstractTokenizer.php

View workflow job for this annotation

GitHub Actions / PHP 8-sqlite-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ $token[] = (new SqlToken())->type(SqlToken::TYPE_STATEMENT); $this->tokenStack->push($token[0]); $this->currentToken = $this->tokenStack->top(); - $length = 0; + $length = -1; while (!$this->isEof()) { if ($this->isWhitespace($length) || $this->isComment($length)) { $this->addTokenFromBuffer();

Check warning on line 105 in src/AbstractTokenizer.php

View workflow job for this annotation

GitHub Actions / PHP 8-sqlite-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ $token[] = (new SqlToken())->type(SqlToken::TYPE_STATEMENT); $this->tokenStack->push($token[0]); $this->currentToken = $this->tokenStack->top(); - $length = 0; + $length = -1; while (!$this->isEof()) { if ($this->isWhitespace($length) || $this->isComment($length)) { $this->addTokenFromBuffer();

Check warning on line 105 in src/AbstractTokenizer.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-sqlite-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ $token[] = (new SqlToken())->type(SqlToken::TYPE_STATEMENT); $this->tokenStack->push($token[0]); $this->currentToken = $this->tokenStack->top(); - $length = 0; + $length = -1; while (!$this->isEof()) { if ($this->isWhitespace($length) || $this->isComment($length)) { $this->addTokenFromBuffer();

Expand Down
1 change: 0 additions & 1 deletion src/Builder/JsonExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function __construct(private QueryBuilderInterface $queryBuilder)
*/
public function build(ExpressionInterface $expression, array &$params = []): string
{
/** @psalm-var mixed $value */
$value = $expression->getValue();

if ($value instanceof QueryInterface) {
Expand Down
18 changes: 0 additions & 18 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function insertWithReturningPks(string $table, array $columns): bool|arra
continue;
}

/** @psalm-var mixed */
$result[$name] = $columns[$name] ?? $tableSchema?->getColumn($name)?->getDefaultValue();
}

Expand Down Expand Up @@ -72,7 +71,6 @@ public function execute(): int
{
$sql = $this->getSql();

/** @psalm-var array<string, string> $params */
$params = $this->params;

$statements = $this->splitStatements($sql, $params);
Expand All @@ -83,11 +81,8 @@ public function execute(): int

$result = 0;

/** @psalm-var array<array-key, array<array-key, string|array>> $statements */
foreach ($statements as $statement) {
[$statementSql, $statementParams] = $statement;
$statementSql = is_string($statementSql) ? $statementSql : '';
$statementParams = is_array($statementParams) ? $statementParams : [];
$this->setSql($statementSql)->bindValues($statementParams);
$result = parent::execute();
}
Expand All @@ -111,7 +106,6 @@ protected function queryInternal(int $queryMode): mixed
{
$sql = $this->getSql();

/** @psalm-var array<string, string> $params */
$params = $this->params;

$statements = $this->splitStatements($sql, $params);
Expand All @@ -122,22 +116,14 @@ protected function queryInternal(int $queryMode): mixed

[$lastStatementSql, $lastStatementParams] = array_pop($statements);

/**
* @psalm-var array<array-key, array> $statements
*/
foreach ($statements as $statement) {
/**
* @psalm-var string $statementSql
* @psalm-var array $statementParams
*/
[$statementSql, $statementParams] = $statement;
$this->setSql($statementSql)->bindValues($statementParams);
parent::execute();
}

$this->setSql($lastStatementSql)->bindValues($lastStatementParams);

/** @psalm-var string $result */
$result = parent::queryInternal($queryMode);

$this->setSql($sql)->bindValues($params);
Expand All @@ -155,8 +141,6 @@ protected function queryInternal(int $queryMode): mixed
*
* @return array|bool List of SQL statements or `false` if there's a single statement.
*
* @psalm-param array<string, string> $params
*
* @psalm-return false|list<array{0: string, 1: array}>
*/
private function splitStatements(string $sql, array $params): bool|array
Expand Down Expand Up @@ -186,8 +170,6 @@ private function splitStatements(string $sql, array $params): bool|array

/**
* Returns named bindings used in the specified statement token.
*
* @psalm-param array<string, string> $params
*/
private function extractUsedParams(SqlToken $statement, array $params): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/DMLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function upsert(
bool|array $updateColumns,
array &$params
): string {
/** @psalm-var Constraint[] $constraints */
/** @var Constraint[] $constraints */
$constraints = [];

[$uniqueNames, $insertNames, $updateNames] = $this->prepareUpsertColumns(
Expand Down
2 changes: 0 additions & 2 deletions src/DQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function build(QueryInterface $query, array $params = []): array
$sql = $this->buildOrderByAndLimit($sql, $orderBy, $query->getLimit(), $query->getOffset());

if (!empty($orderBy)) {
/** @psalm-var array<string|ExpressionInterface> $orderBy */
foreach ($orderBy as $expression) {
if ($expression instanceof ExpressionInterface) {
$this->buildExpression($expression, $params);
Expand All @@ -57,7 +56,6 @@ public function build(QueryInterface $query, array $params = []): array
$groupBy = $query->getGroupBy();

if (!empty($groupBy)) {
/** @psalm-var array<string|ExpressionInterface> $groupBy */
foreach ($groupBy as $expression) {
if ($expression instanceof ExpressionInterface) {
$this->buildExpression($expression, $params);
Expand Down
4 changes: 1 addition & 3 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
final class QueryBuilder extends AbstractQueryBuilder
{
/**
* @var array Mapping from abstract column types (keys) to physical column types (values).
*
* @psalm-var string[] $typeMap
* @var string[] Mapping from abstract column types (keys) to physical column types (values).
*/
protected array $typeMap = [
SchemaInterface::TYPE_PK => 'integer PRIMARY KEY AUTOINCREMENT NOT NULL',
Expand Down
23 changes: 11 additions & 12 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@
final class Schema extends AbstractPdoSchema
{
/**
* @var array Mapping from physical column types (keys) to abstract column types (values).
*
* @psalm-var array<array-key, string> $typeMap
* @var string[] Mapping from physical column types (keys) to abstract column types (values).
*/
private array $typeMap = [
'tinyint' => self::TYPE_TINYINT,
Expand Down Expand Up @@ -218,7 +216,6 @@ protected function loadTableForeignKeys(string $tableName): array
$primaryKey = $this->getTablePrimaryKey($table);

if ($primaryKey !== null) {
/** @psalm-var string $primaryKeyColumnName */
foreach ((array) $primaryKey->getColumnNames() as $i => $primaryKeyColumnName) {
$foreignKey[$i]['to'] = $primaryKeyColumnName;
}
Expand Down Expand Up @@ -303,7 +300,6 @@ protected function loadTableChecks(string $tableName): array

$sql = ($sql === false || $sql === null) ? '' : (string) $sql;

/** @psalm-var SqlToken[]|SqlToken[][]|SqlToken[][][] $code */
$code = (new SqlTokenizer($sql))->tokenize();
$pattern = (new SqlTokenizer('any CREATE any TABLE any()'))->tokenize();
$result = [];
Expand Down Expand Up @@ -363,7 +359,6 @@ protected function loadTableDefaultValues(string $tableName): array
*/
protected function findColumns(TableSchemaInterface $table): bool
{
/** @psalm-var ColumnInfo[] $columns */
$columns = $this->getPragmaTableInfo($table->getName());
$jsonColumns = $this->getJsonColumns($table);

Expand Down Expand Up @@ -444,7 +439,6 @@ public function findUniqueIndexes(TableSchemaInterface $table): array

foreach ($indexList as $index) {
$indexName = $index['name'];
/** @psalm-var IndexInfo[] $indexInfo */
$indexInfo = $this->getPragmaIndexInfo($index['name']);

if ($index['unique']) {
Expand Down Expand Up @@ -551,13 +545,16 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
* @throws Throwable
*
* @return array The table columns info.
*
* @psalm-return ColumnInfo[] $tableColumns;
*/
private function loadTableColumnsInfo(string $tableName): array
{
$tableColumns = $this->getPragmaTableInfo($tableName);
/** @psalm-var ColumnInfo[] $tableColumns */
$tableColumns = $this->normalizeRowKeyCase($tableColumns, true);

/** @psalm-var ColumnInfo[] */
return DbArrayHelper::index($tableColumns, 'cid');
}

Expand Down Expand Up @@ -585,7 +582,6 @@ private function loadTableConstraints(string $tableName, string $returnType): Co
];

foreach ($indexes as $index) {
/** @psalm-var IndexInfo[] $columns */
$columns = $this->getPragmaIndexInfo($index['name']);

if ($index['origin'] === 'pk') {
Expand All @@ -611,8 +607,6 @@ private function loadTableConstraints(string $tableName, string $returnType): Co
* Extra check for PK in case of `INTEGER PRIMARY KEY` with ROWID.
*
* @link https://www.sqlite.org/lang_createtable.html#primkeyconst
*
* @psalm-var ColumnInfo[] $tableColumns
*/
$tableColumns = $this->loadTableColumnsInfo($tableName);

Expand Down Expand Up @@ -659,16 +653,18 @@ private function getPragmaForeignKeyList(string $tableName): array
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @psalm-return IndexInfo[]
*/
private function getPragmaIndexInfo(string $name): array
{
$column = $this->db
->createCommand('PRAGMA INDEX_INFO(' . (string) $this->db->getQuoter()->quoteValue($name) . ')')
->queryAll();
/** @psalm-var IndexInfo[] $column */
$column = $this->normalizeRowKeyCase($column, true);
DbArrayHelper::multisort($column, 'seqno');

/** @psalm-var IndexInfo[] $column */
return $column;
}

Expand All @@ -688,9 +684,12 @@ private function getPragmaIndexList(string $tableName): array
* @throws Exception
* @throws InvalidConfigException
* @throws Throwable
*
* @psalm-return ColumnInfo[]
*/
private function getPragmaTableInfo(string $tableName): array
{
/** @psalm-var ColumnInfo[] */
return $this->db->createCommand(
'PRAGMA TABLE_INFO(' . $this->db->getQuoter()->quoteSimpleTableName($tableName) . ')'
)->queryAll();
Expand All @@ -703,7 +702,7 @@ private function getPragmaTableInfo(string $tableName): array
*/
protected function findViewNames(string $schema = ''): array
{
/** @psalm-var string[][] $views */
/** @var string[][] $views */
$views = $this->db->createCommand(
<<<SQL
SELECT name as view FROM sqlite_master WHERE type = 'view' AND name NOT LIKE 'sqlite_%'
Expand Down

0 comments on commit b5ed5c1

Please sign in to comment.