Skip to content
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
6 changes: 0 additions & 6 deletions analyzer-baseline.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2820,12 +2820,6 @@ code = "nullable-return-statement"
message = '''Function `PhpDb\Sql\Join::current` is declared to return `array{'columns': array<array-key, PhpDb\Sql\ExpressionInterface|string>, 'name': PhpDb\Sql\TableIdentifier|non-empty-array<string, PhpDb\Sql\Expression|PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|string>|string, 'on': PhpDb\Sql\Predicate\PredicateInterface|string, 'type': string}` but possibly returns a nullable value (inferred as `array{'columns': array<array-key, PhpDb\Sql\ExpressionInterface|string>, 'name': PhpDb\Sql\TableIdentifier|non-empty-array<string, PhpDb\Sql\Expression|PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|string>|string, 'on': PhpDb\Sql\Predicate\PredicateInterface|string, 'type': string}|null`).'''
count = 1

[[issues]]
file = "src/Sql/Join.php"
code = "possibly-invalid-argument"
message = 'Possible argument type mismatch for argument #1 of `PhpDb\Sql\Exception\InvalidArgumentException::forInvalidJoinName`: expected `string`, but possibly received `PhpDb\Sql\Expression|PhpDb\Sql\Select|PhpDb\Sql\TableIdentifier|string`.'
count = 1

[[issues]]
file = "src/Sql/Join.php"
code = "possibly-undefined-int-array-index"
Expand Down
6 changes: 2 additions & 4 deletions src/Metadata/Source/AbstractSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,8 @@ public function getConstraintKeys(string $constraint, string $table, ?string $sc
foreach ($this->data['constraint_keys'][$schema] ?? [] as $constraintKeyInfo) {
if (
! (

$constraintKeyInfo['table_name'] === $table
&& $constraintKeyInfo['constraint_name'] === $constraint

$constraintKeyInfo['table_name'] === $table
&& $constraintKeyInfo['constraint_name'] === $constraint
)
) {
continue;
Expand Down
7 changes: 6 additions & 1 deletion src/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function array_key_exists;
use function array_shift;
use function count;
use function get_debug_type;
use function is_array;
use function is_string;
use function key;
Expand Down Expand Up @@ -112,7 +113,11 @@ public function join(
string $type = self::JOIN_INNER,
): static {
if (is_array($name) && (! is_string(key($name)) || count($name) !== 1)) {
throw Exception\InvalidArgumentException::forInvalidJoinName(array_shift($name));
$first = array_shift($name);

throw Exception\InvalidArgumentException::forInvalidJoinName(
is_string($first) ? $first : get_debug_type($first),
);
}

if (! is_array($columns)) {
Expand Down
25 changes: 25 additions & 0 deletions test/unit/Sql/JoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpDb\Sql\Select;
use PhpDbTest\DeprecatedAssertionsTrait;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -32,6 +33,16 @@ class JoinTest extends TestCase
{
use DeprecatedAssertionsTrait;

/** @return array<string, array{array<array-key, mixed>, string}> */
public static function invalidJoinNameWithoutStringElementProvider(): array
{
return [
'empty array' => [[], 'null'],
'select without alias' => [[new Select('foo')], Select::class],
'integer element' => [[5], 'int'],
];
}

#[Test]
#[TestDox('unit test: Test count() returns correct count')]
public function countsJoinedTables(): void
Expand Down Expand Up @@ -91,6 +102,20 @@ public function joinFullOuter(): void
static::assertSame($join, $return);
}

/**
* @param array<array-key, mixed> $name
*/
#[Test]
#[DataProvider('invalidJoinNameWithoutStringElementProvider')]
public function joinThrowsInvalidArgumentWhenInvalidNameHasNoLeadingString(array $name, string $described): void
{
$join = new Join();

self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage("expects '{$described}' as a single element associative array");
$join->join($name, 'on');
}

#[Test]
public function joinThrowsOnInvalidMultiElementArray(): void
{
Expand Down
Loading