Skip to content

Commit ff61bb0

Browse files
committed
Never set undefined properties from UpdateTrait.
1 parent 5ede44d commit ff61bb0

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/UpdateTidyTrait.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77
namespace karmabunny\kb;
88

99
/**
10-
* This modifies the behaviour of a DataObject/Collection so that only
11-
* properties defined in the class are updated.
10+
* This is functional the same as {@see UpdateTrait} only it uses the
11+
* `getProperties()` helper to determine which fields belong to the class.
1212
*
13-
* The default implementation will create _new_ fields that aren't typed. This
14-
* trait will only update fields that are explicitly defined. Unknown fields
15-
* are silently ignored.
16-
*
17-
* For a more aggressive approach {@see UpdateStrictTrait}.
13+
* To raise errors on unknown fields {@see UpdateStrictTrait}.
1814
*
1915
* @package karmabunny\kb
2016
*/

src/UpdateTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
/**
1010
* This implements basic `update()` behaviour for an object.
1111
*
12+
* Only fields that are defined as properties will be set. Unknown fields
13+
* are silently ignored.
14+
*
15+
* To raise errors on unknown fields {@see UpdateStrictTrait}.
16+
*
1217
* @package karmabunny\kb
1318
*/
1419
trait UpdateTrait
@@ -21,6 +26,7 @@ trait UpdateTrait
2126
public function update($config)
2227
{
2328
foreach ($config as $key => $item) {
29+
if (!property_exists($this, $key)) continue;
2430
$this->$key = $item;
2531
}
2632

0 commit comments

Comments
 (0)