Skip to content

Commit 7d290ed

Browse files
committed
refactor: Remove deprecated Entity::setAttributes()
1 parent 332b48c commit 7d290ed

6 files changed

Lines changed: 41 additions & 82 deletions

File tree

system/Entity/Entity.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,6 @@ public function injectRawData(array $data)
519519
return $this;
520520
}
521521

522-
/**
523-
* Set raw data array without any mutations.
524-
*
525-
* @return $this
526-
*
527-
* @deprecated Use injectRawData() instead.
528-
*/
529-
public function setAttributes(array $data)
530-
{
531-
return $this->injectRawData($data);
532-
}
533-
534522
/**
535523
* Checks the datamap to see if this property name is being mapped,
536524
* and returns the DB column name, if any, or the original property name.
@@ -652,7 +640,7 @@ public function __set(string $key, $value = null)
652640
}
653641

654642
// If a "`set` + $key" method exists, it is also a setter.
655-
if (method_exists($this, $method) && $method !== 'setAttributes') {
643+
if (method_exists($this, $method)) {
656644
$this->{$method}($value);
657645

658646
return;

tests/system/Entity/EntityTest.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ public function testSetArrayToPropertyNamedAttributes(): void
7575
$this->assertSame($expected, $entity->toRawArray());
7676
}
7777

78+
public function testSetGetAttributesMethod(): void
79+
{
80+
$entity = new class () extends Entity {
81+
protected $attributes = [
82+
'foo' => null,
83+
'attributes' => null,
84+
];
85+
86+
public function setAttributes(string $value): self
87+
{
88+
$this->attributes['attributes'] = $value;
89+
90+
return $this;
91+
}
92+
93+
public function getAttributes(): string
94+
{
95+
return $this->attributes['attributes'];
96+
}
97+
};
98+
99+
$entity->setAttributes('attributes');
100+
101+
$this->assertSame('attributes', $entity->getAttributes());
102+
}
103+
78104
public function testSimpleSetAndGet(): void
79105
{
80106
$entity = $this->getEntity();
@@ -358,11 +384,11 @@ public function testCastIntBool(): void
358384
];
359385
};
360386

361-
$entity->setAttributes(['active' => '1']);
387+
$entity->injectRawData(['active' => '1']);
362388

363389
$this->assertTrue($entity->active);
364390

365-
$entity->setAttributes(['active' => '0']);
391+
$entity->injectRawData(['active' => '0']);
366392

367393
$this->assertFalse($entity->active);
368394

@@ -1096,19 +1122,19 @@ public function testAsArrayRestoringCastStatus(): void
10961122
// Disabled casting properties, but we will allow casting in the method.
10971123
$entity->cast(false);
10981124
$beforeCast = $this->getPrivateProperty($entity, '_cast');
1099-
$result = $entity->toArray(true, true);
1100-
$afterCast = $this->getPrivateProperty($entity, '_cast');
1125+
$result = $entity->toArray(true, true);
1126+
$afterCast = $this->getPrivateProperty($entity, '_cast');
11011127

1102-
$this->assertSame($result['first'], 2026);
1128+
$this->assertSame(2026, $result['first']);
11031129
$this->assertSame($beforeCast, $afterCast);
11041130

11051131
// Enabled casting properties, but we will disallow casting in the method.
11061132
$entity->cast(true);
11071133
$beforeCast = $this->getPrivateProperty($entity, '_cast');
1108-
$result = $entity->toArray(true, false);
1109-
$afterCast = $this->getPrivateProperty($entity, '_cast');
1134+
$result = $entity->toArray(true, false);
1135+
$afterCast = $this->getPrivateProperty($entity, '_cast');
11101136

1111-
$this->assertSame($result['first'], '2026 Year');
1137+
$this->assertSame('2026 Year', $result['first']);
11121138
$this->assertSame($beforeCast, $afterCast);
11131139
}
11141140

tests/system/Models/ValidationModelRuleGroupTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function testUpdateEntityWithPropertyCleanValidationRulesTrueAndCallingCl
380380

381381
// Simulate to get the entity from the database.
382382
$entity = new SimpleEntity();
383-
$entity->setAttributes([
383+
$entity->injectRawData([
384384
'id' => '1',
385385
'field1' => 'value1',
386386
'field2' => 'value2',
@@ -421,7 +421,7 @@ public function testUpdateEntityWithPropertyCleanValidationRulesFalse(): void
421421

422422
// Simulate to get the entity from the database.
423423
$entity = new SimpleEntity();
424-
$entity->setAttributes([
424+
$entity->injectRawData([
425425
'id' => '1',
426426
'field1' => 'value1',
427427
'field2' => 'value2',
@@ -457,7 +457,7 @@ public function testInsertEntityValidateEntireRules(): void
457457
};
458458

459459
$entity = new SimpleEntity();
460-
$entity->setAttributes([
460+
$entity->injectRawData([
461461
'field1' => 'value1',
462462
// field2 is missing
463463
'field3' => '',

tests/system/Models/ValidationModelTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ public function testUpdateEntityWithPropertyCleanValidationRulesTrueAndCallingCl
393393

394394
// Simulate to get the entity from the database.
395395
$entity = new SimpleEntity();
396-
$entity->setAttributes([
396+
$entity->injectRawData([
397397
'id' => '1',
398398
'field1' => 'value1',
399399
'field2' => 'value2',
@@ -434,7 +434,7 @@ public function testUpdateEntityWithPropertyCleanValidationRulesFalse(): void
434434

435435
// Simulate to get the entity from the database.
436436
$entity = new SimpleEntity();
437-
$entity->setAttributes([
437+
$entity->injectRawData([
438438
'id' => '1',
439439
'field1' => 'value1',
440440
'field2' => 'value2',
@@ -470,7 +470,7 @@ public function testInsertEntityValidateEntireRules(): void
470470
};
471471

472472
$entity = new SimpleEntity();
473-
$entity->setAttributes([
473+
$entity->injectRawData([
474474
'field1' => 'value1',
475475
// field2 is missing
476476
'field3' => '',

utils/phpstan-baseline/empty.notAllowed.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,6 @@ parameters:
197197
count: 2
198198
path: ../../system/Encryption/Handlers/SodiumHandler.php
199199

200-
-
201-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
202-
count: 1
203-
path: ../../system/Entity/Entity.php
204-
205200
-
206201
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
207202
count: 1

utils/phpstan-baseline/missingType.iterableValue.neon

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,56 +2322,6 @@ parameters:
23222322
count: 1
23232323
path: ../../system/Entity/Cast/URICast.php
23242324

2325-
-
2326-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:__construct\(\) has parameter \$data with no value type specified in iterable type array\.$#'
2327-
count: 1
2328-
path: ../../system/Entity/Entity.php
2329-
2330-
-
2331-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:__get\(\) return type has no value type specified in iterable type array\.$#'
2332-
count: 1
2333-
path: ../../system/Entity/Entity.php
2334-
2335-
-
2336-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:__set\(\) has parameter \$value with no value type specified in iterable type array\.$#'
2337-
count: 1
2338-
path: ../../system/Entity/Entity.php
2339-
2340-
-
2341-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:castAs\(\) return type has no value type specified in iterable type array\.$#'
2342-
count: 1
2343-
path: ../../system/Entity/Entity.php
2344-
2345-
-
2346-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:fill\(\) has parameter \$data with no value type specified in iterable type array\.$#'
2347-
count: 1
2348-
path: ../../system/Entity/Entity.php
2349-
2350-
-
2351-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:injectRawData\(\) has parameter \$data with no value type specified in iterable type array\.$#'
2352-
count: 1
2353-
path: ../../system/Entity/Entity.php
2354-
2355-
-
2356-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:jsonSerialize\(\) return type has no value type specified in iterable type array\.$#'
2357-
count: 1
2358-
path: ../../system/Entity/Entity.php
2359-
2360-
-
2361-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:setAttributes\(\) has parameter \$data with no value type specified in iterable type array\.$#'
2362-
count: 1
2363-
path: ../../system/Entity/Entity.php
2364-
2365-
-
2366-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:toArray\(\) return type has no value type specified in iterable type array\.$#'
2367-
count: 1
2368-
path: ../../system/Entity/Entity.php
2369-
2370-
-
2371-
message: '#^Method CodeIgniter\\Entity\\Entity\:\:toRawArray\(\) return type has no value type specified in iterable type array\.$#'
2372-
count: 1
2373-
path: ../../system/Entity/Entity.php
2374-
23752325
-
23762326
message: '#^Method CodeIgniter\\Exceptions\\PageNotFoundException\:\:lang\(\) has parameter \$args with no value type specified in iterable type array\.$#'
23772327
count: 1

0 commit comments

Comments
 (0)