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

Don't add too many spaces when replacing clauses #558

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
57 changes: 39 additions & 18 deletions src/Utils/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
use function array_flip;
use function array_keys;
use function count;
use function ctype_space;
use function in_array;
use function is_string;
use function mb_substr;
use function trim;

/**
Expand Down Expand Up @@ -88,7 +90,7 @@

$expressions = $statement->expr;
if (! empty($statement->join)) {
foreach ($statement->join as $join) {

Check warning on line 93 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "Foreach_": --- Original +++ New @@ @@ } $expressions = $statement->expr; if (!empty($statement->join)) { - foreach ($statement->join as $join) { + foreach (array() as $join) { $expressions[] = $join->expr; } }
$expressions[] = $join->expr;
}
}
Expand Down Expand Up @@ -209,9 +211,9 @@
}

if (
($statement instanceof SelectStatement)

Check warning on line 214 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } elseif ($statement instanceof SetStatement) { $flags->queryType = StatementType::Set; } - if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { + if (true || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { if (!empty($statement->limit)) { $flags->limit = true; }

Check warning on line 214 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOrAllSubExprNegation": --- Original +++ New @@ @@ } elseif ($statement instanceof SetStatement) { $flags->queryType = StatementType::Set; } - if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { + if (!$statement instanceof SelectStatement || !$statement instanceof UpdateStatement || !$statement instanceof DeleteStatement) { if (!empty($statement->limit)) { $flags->limit = true; }
|| ($statement instanceof UpdateStatement)

Check warning on line 215 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } elseif ($statement instanceof SetStatement) { $flags->queryType = StatementType::Set; } - if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { + if ($statement instanceof SelectStatement || true || $statement instanceof DeleteStatement) { if (!empty($statement->limit)) { $flags->limit = true; }

Check warning on line 215 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } elseif ($statement instanceof SetStatement) { $flags->queryType = StatementType::Set; } - if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { + if ($statement instanceof SelectStatement || false || $statement instanceof DeleteStatement) { if (!empty($statement->limit)) { $flags->limit = true; }
|| ($statement instanceof DeleteStatement)

Check warning on line 216 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } elseif ($statement instanceof SetStatement) { $flags->queryType = StatementType::Set; } - if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { + if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || true) { if (!empty($statement->limit)) { $flags->limit = true; }

Check warning on line 216 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } elseif ($statement instanceof SetStatement) { $flags->queryType = StatementType::Set; } - if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || $statement instanceof DeleteStatement) { + if ($statement instanceof SelectStatement || $statement instanceof UpdateStatement || false) { if (!empty($statement->limit)) { $flags->limit = true; }
) {
if (! empty($statement->limit)) {
$flags->limit = true;
Expand Down Expand Up @@ -243,11 +245,11 @@
$selectTables = [];
$selectExpressions = [];

if ($statement instanceof SelectStatement) {

Check warning on line 248 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ $flags = static::getFlags($statement); $selectTables = []; $selectExpressions = []; - if ($statement instanceof SelectStatement) { + if (true) { // Finding tables' aliases and their associated real names. $tableAliases = []; foreach ($statement->from as $expr) {
// Finding tables' aliases and their associated real names.
$tableAliases = [];
foreach ($statement->from as $expr) {
if (! isset($expr->table, $expr->alias) || ($expr->table === '') || ($expr->alias === '')) {

Check warning on line 252 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ // Finding tables' aliases and their associated real names. $tableAliases = []; foreach ($statement->from as $expr) { - if (!isset($expr->table, $expr->alias) || $expr->table === '' || $expr->alias === '') { + if (!isset($expr->table, $expr->alias) && $expr->table === '' || $expr->alias === '') { continue; } $tableAliases[$expr->alias] = [$expr->table, $expr->database ?? null];

Check warning on line 252 in src/Utils/Query.php

View workflow job for this annotation

GitHub Actions / Mutation tests with PHP 8.1

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ // Finding tables' aliases and their associated real names. $tableAliases = []; foreach ($statement->from as $expr) { - if (!isset($expr->table, $expr->alias) || $expr->table === '' || $expr->alias === '') { + if ((!isset($expr->table, $expr->alias) || $expr->table === '') && $expr->alias === '') { continue; } $tableAliases[$expr->alias] = [$expr->table, $expr->database ?? null];
continue;
}

Expand Down Expand Up @@ -476,6 +478,29 @@
return trim($ret);
}

/** @param list<string> $parts */
private static function glueQueryPartsWithSpaces(array $parts): string
{
$statement = '';
foreach ($parts as $part) {
if ($part === '') {
continue;
}

if (
$statement !== '' &&
! ctype_space(mb_substr($statement, -1)) &&
! ctype_space(mb_substr($part, 0, 1))
) {
$statement .= ' ';
}

$statement .= $part;
}

return $statement;
}

/**
* Builds a query by rebuilding the statement from the tokens list supplied
* and replaces a clause.
Expand All @@ -500,18 +525,17 @@
): string {
// TODO: Update the tokens list and the statement.

if ($new === null) {
$new = $old;
}

$parts = [
static::getClause($statement, $list, $old, -1, false),
$new ?? $old,
];
if ($onlyType) {
return static::getClause($statement, $list, $old, -1, false) . ' ' .
$new . ' ' . static::getClause($statement, $list, $old, 0) . ' ' .
static::getClause($statement, $list, $old, 1, false);
$parts[] = static::getClause($statement, $list, $old, 0);
}

return static::getClause($statement, $list, $old, -1, false) . ' ' .
$new . ' ' . static::getClause($statement, $list, $old, 1, false);
$parts[] = static::getClause($statement, $list, $old, 1, false);

return self::glueQueryPartsWithSpaces($parts);
}

/**
Expand All @@ -533,33 +557,30 @@
return '';
}

/**
* Value to be returned.
*/
$ret = '';

// If there is only one clause, `replaceClause()` should be used.
if ($count === 1) {
return static::replaceClause($statement, $list, $ops[0][0], $ops[0][1]);
}

// Adding everything before first replacement.
$ret .= static::getClause($statement, $list, $ops[0][0], -1) . ' ';
$parts = [static::getClause($statement, $list, $ops[0][0], -1)];

// Doing replacements.
foreach ($ops as $i => $clause) {
$ret .= $clause[1] . ' ';
$parts[] = $clause[1];

// Adding everything between this and next replacement.
if ($i + 1 === $count) {
continue;
}

$ret .= static::getClause($statement, $list, $clause[0], $ops[$i + 1][0]) . ' ';
$parts[] = static::getClause($statement, $list, $clause[0], $ops[$i + 1][0]);
}

// Adding everything after the last replacement.
return $ret . static::getClause($statement, $list, $ops[$count - 1][0], 1);
$parts[] = static::getClause($statement, $list, $ops[$count - 1][0], 1);

return self::glueQueryPartsWithSpaces($parts);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Utils/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ public function testReplaceClause(): void
$this->assertEquals(
'select supplier.city, supplier.id from supplier '
. 'union select customer.city, customer.id from customer'
. ' ORDER BY city ',
. ' ORDER BY city',
Query::replaceClause(
$parser->statements[0],
$parser->list,
Expand All @@ -572,7 +572,7 @@ public function testReplaceClauseOnlyKeyword(): void
$parser = new Parser('SELECT *, (SELECT 1) FROM film LIMIT 0, 10');
$this->assertNotNull($parser->list);
$this->assertEquals(
' SELECT SQL_CALC_FOUND_ROWS *, (SELECT 1) FROM film LIMIT 0, 10',
'SELECT SQL_CALC_FOUND_ROWS *, (SELECT 1) FROM film LIMIT 0, 10',
Query::replaceClause(
$parser->statements[0],
$parser->list,
Expand All @@ -588,7 +588,7 @@ public function testReplaceNonExistingPart(): void
$parser = new Parser('ALTER TABLE `sale_mast` OPTIMIZE PARTITION p3');
$this->assertNotNull($parser->list);
$this->assertEquals(
' ALTER TABLE `sale_mast` OPTIMIZE PARTITION p3',
'ALTER TABLE `sale_mast` OPTIMIZE PARTITION p3',
Query::replaceClause(
$parser->statements[0],
$parser->list,
Expand Down Expand Up @@ -629,9 +629,9 @@ public function testReplaceClauses(): void
$this->assertEquals(
'SELECT c.city_id, c.country_id ' .
'INTO OUTFILE "/dev/null" ' .
'FROM city AS c ' .
'FROM city AS c ' .
'ORDER BY city_id ASC ' .
'LIMIT 0, 10 ',
'LIMIT 0, 10',
Query::replaceClauses(
$parser->statements[0],
$parser->list,
Expand Down
Loading