diff --git a/composer.json b/composer.json index 7d20c922..21b83f90 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "require-dev": { "ext-pdo": "*", "ext-sqlite3": "*", + "jetbrains/phpstorm-attributes": "^1.2", "mockery/mockery": "^1.5.0", "nette/php-generator": "v4.0.5", "nikic/php-parser": "v4.15.2", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 472c0387..6c048cfa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -26,16 +26,6 @@ parameters: path: src/Testing/Actions/ParsePhpDocAction.php # forward compatibility - - - message: "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\Before does not exist\\.$#" - count: 1 - path: src/Testing/Assert/AssertExpectationTestCase.php - - - - message: "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\PostCondition does not exist\\.$#" - count: 1 - path: src/Testing/Assert/AssertExpectationTestCase.php - - message: "#^Method LaraStrict\\\\Testing\\\\Assert\\\\AssertExpectationTestCase\\:\\:beforeStartAssertExpectationManager\\(\\) has no return type specified\\.$#" count: 1 @@ -51,10 +41,3 @@ parameters: message: "#^Method Tests\\\\LaraStrict\\\\Feature\\\\Translations\\\\InvalidServiceProviderTranslations\\:\\:getProviderClass\\(\\) should return class\\-string\\ but returns string\\.$#" count: 1 path: tests/Feature/Translations/InvalidServiceProviderTranslations.php - - # forward compatibility - - - message: "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\BeforeClass does not exist\\.$#" - count: 1 - path: tests/Unit/Database/Services/ChunkWriteServiceTest.php - # forward compatibility diff --git a/phpstan.neon b/phpstan.neon index ab1461dd..62b8bf3e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -32,4 +32,6 @@ parameters: count: 1 path: src/Database/Models/Casts/FloatCast.php reportUnmatched: false + # forward compatibility + - "#^Attribute class PHPUnit\\\\Framework\\\\Attributes\\\\.*?does not exist\\.$#" diff --git a/src/Database/Scopes/AbstractInScope.php b/src/Database/Scopes/AbstractInScope.php index 46e89970..2b1c29d1 100644 --- a/src/Database/Scopes/AbstractInScope.php +++ b/src/Database/Scopes/AbstractInScope.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use JetBrains\PhpStorm\Deprecated; /** * Adds whereIn or where condition for given values. @@ -26,10 +27,11 @@ abstract class AbstractInScope extends AbstractScope * 'or' value */ public function __construct( - private array $values, + private array|int|float|string|bool|null $values, + #[Deprecated(reason: 'Use right parameter instead of this magic.')] string|bool|null $booleanOrTableOrNot = null, string $table = '', - bool $not = false + bool $not = false, ) { if (is_bool($booleanOrTableOrNot)) { $not = $booleanOrTableOrNot; @@ -49,8 +51,12 @@ public function __construct( public function apply(Builder $builder, Model $model): void { $column = $this->table === '' ? $this->getColumn($model) : $this->table . '.' . $this->getColumn($model); - if (count($this->values) === 1) { - $builder->where($column, $this->not ? '<>' : '=', reset($this->values), $this->boolean); + if (is_array($this->values) && count($this->values) === 1) { + $this->values = reset($this->values); + } + + if (is_array($this->values) === false) { + $builder->where($column, $this->not ? '!=' : '=', $this->values, $this->boolean); return; } diff --git a/src/Database/Scopes/WhereIdsScope.php b/src/Database/Scopes/WhereIdsScope.php index 7995e2e4..08afd0a2 100644 --- a/src/Database/Scopes/WhereIdsScope.php +++ b/src/Database/Scopes/WhereIdsScope.php @@ -5,12 +5,14 @@ namespace LaraStrict\Database\Scopes; use Illuminate\Database\Eloquent\Model; +use JetBrains\PhpStorm\Deprecated; class WhereIdsScope extends AbstractInScope { public function __construct( - array $ids, + array|int $ids, private readonly ?string $key = null, + #[Deprecated(reason: 'Use right parameter instead of this magic.')] string|bool|null $booleanOrTableOrNot = null, string $table = '', bool $not = false diff --git a/tests/Unit/Database/Scopes/WhereIdsScopeTest.php b/tests/Unit/Database/Scopes/WhereIdsScopeTest.php new file mode 100644 index 00000000..a0292fc1 --- /dev/null +++ b/tests/Unit/Database/Scopes/WhereIdsScopeTest.php @@ -0,0 +1,91 @@ + + */ + public static function data(): array + { + return [ + [ + static function (self $self) { + $self->assert(new WhereIdsScope(1), 'select * from "test_models" where "id" = ?'); + }, + ], + [ + static function (self $self) { + $self->assert(new WhereIdsScope([1]), 'select * from "test_models" where "id" = ?'); + }, + ], + [ + static function (self $self) { + $self->assert( + new WhereIdsScope([1], not: true), + 'select * from "test_models" where "id" != ?', + ); + }, + ], + [ + static function (self $self) { + $self->assert(new WhereIdsScope([1, 2]), 'select * from "test_models" where "id" in (?, ?)'); + }, + ], + [ + static function (self $self) { + $self->assert( + new WhereIdsScope([1, 2], 'foo'), + 'select * from "test_models" where "foo" in (?, ?)', + ); + }, + ], + [ + static function (self $self) { + $self->assert( + new WhereIdsScope([1, 2], table: 'foo'), + 'select * from "test_models" where "foo"."id" in (?, ?)', + ); + }, + ], + [ + static function (self $self) { + $self->assert( + new WhereIdsScope([1, 2], not: true), + 'select * from "test_models" where "id" not in (?, ?)', + ); + }, + ], + ]; + } + + /** + * @param Closure(static):void $assert + * @dataProvider data + */ + public function test(Closure $assert): void + { + $assert($this); + } + + public function assert(WhereIdsScope $scope, string $expected): void + { + $builder = TestModel::query(); + $scope->apply($builder, new TestModel()); + + $sql = $builder->toSql(); + Assert::assertSame($expected, $sql); + } +}