Skip to content

Commit 0d22588

Browse files
authored
Use explicit variadic params instead of func_get_args() in HasAttributes::isDirty/isClean/wasChanged. (#7803)
1 parent d5cf0b4 commit 0d22588

2 files changed

Lines changed: 47 additions & 12 deletions

File tree

‎src/Model/Concerns/HasAttributes.php‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -566,36 +566,41 @@ public function syncChanges(?array $columns = null): static
566566
* Determine if the model or given attribute(s) have been modified.
567567
*
568568
* @param null|array|string $attributes
569+
* @param string ...$extra
569570
*/
570-
public function isDirty($attributes = null): bool
571+
public function isDirty($attributes = null, ...$extra): bool
571572
{
572-
return $this->hasChanges(
573-
$this->getDirty(),
574-
is_array($attributes) ? $attributes : func_get_args()
575-
);
573+
if (! is_array($attributes)) {
574+
$attributes = is_null($attributes) && $extra === [] ? [] : [$attributes, ...$extra];
575+
}
576+
577+
return $this->hasChanges($this->getDirty(), $attributes);
576578
}
577579

578580
/**
579581
* Determine if the model or given attribute(s) have remained the same.
580582
*
581583
* @param null|array|string $attributes
584+
* @param string ...$extra
582585
*/
583-
public function isClean($attributes = null): bool
586+
public function isClean($attributes = null, ...$extra): bool
584587
{
585-
return ! $this->isDirty(...func_get_args());
588+
return ! $this->isDirty($attributes, ...$extra);
586589
}
587590

588591
/**
589592
* Determine if the model or given attribute(s) have been modified.
590593
*
591594
* @param null|array|string $attributes
595+
* @param string ...$extra
592596
*/
593-
public function wasChanged($attributes = null): bool
597+
public function wasChanged($attributes = null, ...$extra): bool
594598
{
595-
return $this->hasChanges(
596-
$this->getChanges(),
597-
is_array($attributes) ? $attributes : func_get_args()
598-
);
599+
if (! is_array($attributes)) {
600+
$attributes = is_null($attributes) && $extra === [] ? [] : [$attributes, ...$extra];
601+
}
602+
603+
return $this->hasChanges($this->getChanges(), $attributes);
599604
}
600605

601606
/**

‎tests/ModelTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,36 @@ public function testCleanAttributes()
178178
$this->assertFalse($model->isClean(['foo', 'bar']));
179179
}
180180

181+
public function testWasChangedAttributes()
182+
{
183+
$model = new ModelStub(['foo' => '1', 'bar' => 2, 'baz' => 3]);
184+
$model->syncOriginal();
185+
$model->foo = 1;
186+
$model->bar = 20;
187+
$model->baz = 30;
188+
189+
// Changes have not been synced yet, so no attribute was changed.
190+
$this->assertFalse($model->wasChanged());
191+
$this->assertFalse($model->wasChanged('bar'));
192+
193+
$model->syncChanges();
194+
195+
$this->assertTrue($model->wasChanged());
196+
$this->assertFalse($model->wasChanged('foo'));
197+
$this->assertTrue($model->wasChanged('bar'));
198+
$this->assertTrue($model->wasChanged('foo', 'bar'));
199+
$this->assertTrue($model->wasChanged(['foo', 'bar']));
200+
201+
// All attributes are equivalent to their original values.
202+
$model->syncOriginal();
203+
$model->syncChanges();
204+
205+
$this->assertFalse($model->wasChanged());
206+
$this->assertFalse($model->wasChanged('bar'));
207+
$this->assertFalse($model->wasChanged('foo', 'bar'));
208+
$this->assertFalse($model->wasChanged(['foo', 'bar']));
209+
}
210+
181211
public function testCalculatedAttributes()
182212
{
183213
$model = new ModelStub();

0 commit comments

Comments
 (0)