diff --git a/analyzer-baseline.toml b/analyzer-baseline.toml index 6264b00f..0f0fa4a6 100644 --- a/analyzer-baseline.toml +++ b/analyzer-baseline.toml @@ -2820,12 +2820,6 @@ code = "nullable-return-statement" message = '''Function `PhpDb\Sql\Join::current` is declared to return `array{'columns': array, 'name': PhpDb\Sql\TableIdentifier|non-empty-array|string, 'on': PhpDb\Sql\Predicate\PredicateInterface|string, 'type': string}` but possibly returns a nullable value (inferred as `array{'columns': array, 'name': PhpDb\Sql\TableIdentifier|non-empty-array|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" diff --git a/src/Metadata/Source/AbstractSource.php b/src/Metadata/Source/AbstractSource.php index 3638d561..e91ebded 100644 --- a/src/Metadata/Source/AbstractSource.php +++ b/src/Metadata/Source/AbstractSource.php @@ -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; diff --git a/src/Sql/Join.php b/src/Sql/Join.php index fdee1d37..4dcbb46f 100644 --- a/src/Sql/Join.php +++ b/src/Sql/Join.php @@ -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; @@ -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)) { diff --git a/test/unit/Sql/JoinTest.php b/test/unit/Sql/JoinTest.php index 89e3cfe2..9e004491 100644 --- a/test/unit/Sql/JoinTest.php +++ b/test/unit/Sql/JoinTest.php @@ -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; @@ -32,6 +33,16 @@ class JoinTest extends TestCase { use DeprecatedAssertionsTrait; + /** @return array, 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 @@ -91,6 +102,20 @@ public function joinFullOuter(): void static::assertSame($join, $return); } + /** + * @param array $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 {