Skip to content

Commit 992ffa3

Browse files
authored
Merge pull request #223 from simon-mundy/fix/206-join-name-type
Throw InvalidArgumentException for join names without a leading string
2 parents 0c4ed09 + 2f0c6ed commit 992ffa3

4 files changed

Lines changed: 33 additions & 11 deletions

File tree

‎analyzer-baseline.toml‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,12 +2820,6 @@ code = "nullable-return-statement"
28202820
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`).'''
28212821
count = 1
28222822

2823-
[[issues]]
2824-
file = "src/Sql/Join.php"
2825-
code = "possibly-invalid-argument"
2826-
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`.'
2827-
count = 1
2828-
28292823
[[issues]]
28302824
file = "src/Sql/Join.php"
28312825
code = "possibly-undefined-int-array-index"

‎src/Metadata/Source/AbstractSource.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,8 @@ public function getConstraintKeys(string $constraint, string $table, ?string $sc
300300
foreach ($this->data['constraint_keys'][$schema] ?? [] as $constraintKeyInfo) {
301301
if (
302302
! (
303-
304-
$constraintKeyInfo['table_name'] === $table
305-
&& $constraintKeyInfo['constraint_name'] === $constraint
306-
303+
$constraintKeyInfo['table_name'] === $table
304+
&& $constraintKeyInfo['constraint_name'] === $constraint
307305
)
308306
) {
309307
continue;

‎src/Sql/Join.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function array_key_exists;
1313
use function array_shift;
1414
use function count;
15+
use function get_debug_type;
1516
use function is_array;
1617
use function is_string;
1718
use function key;
@@ -112,7 +113,11 @@ public function join(
112113
string $type = self::JOIN_INNER,
113114
): static {
114115
if (is_array($name) && (! is_string(key($name)) || count($name) !== 1)) {
115-
throw Exception\InvalidArgumentException::forInvalidJoinName(array_shift($name));
116+
$first = array_shift($name);
117+
118+
throw Exception\InvalidArgumentException::forInvalidJoinName(
119+
is_string($first) ? $first : get_debug_type($first),
120+
);
116121
}
117122

118123
if (! is_array($columns)) {

‎test/unit/Sql/JoinTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpDb\Sql\Select;
1010
use PhpDbTest\DeprecatedAssertionsTrait;
1111
use PHPUnit\Framework\Attributes\CoversMethod;
12+
use PHPUnit\Framework\Attributes\DataProvider;
1213
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1314
use PHPUnit\Framework\Attributes\RequiresPhp;
1415
use PHPUnit\Framework\Attributes\Test;
@@ -32,6 +33,16 @@ class JoinTest extends TestCase
3233
{
3334
use DeprecatedAssertionsTrait;
3435

36+
/** @return array<string, array{array<array-key, mixed>, string}> */
37+
public static function invalidJoinNameWithoutStringElementProvider(): array
38+
{
39+
return [
40+
'empty array' => [[], 'null'],
41+
'select without alias' => [[new Select('foo')], Select::class],
42+
'integer element' => [[5], 'int'],
43+
];
44+
}
45+
3546
#[Test]
3647
#[TestDox('unit test: Test count() returns correct count')]
3748
public function countsJoinedTables(): void
@@ -91,6 +102,20 @@ public function joinFullOuter(): void
91102
static::assertSame($join, $return);
92103
}
93104

105+
/**
106+
* @param array<array-key, mixed> $name
107+
*/
108+
#[Test]
109+
#[DataProvider('invalidJoinNameWithoutStringElementProvider')]
110+
public function joinThrowsInvalidArgumentWhenInvalidNameHasNoLeadingString(array $name, string $described): void
111+
{
112+
$join = new Join();
113+
114+
self::expectException(InvalidArgumentException::class);
115+
self::expectExceptionMessage("expects '{$described}' as a single element associative array");
116+
$join->join($name, 'on');
117+
}
118+
94119
#[Test]
95120
public function joinThrowsOnInvalidMultiElementArray(): void
96121
{

0 commit comments

Comments
 (0)