Skip to content

Commit 2d2f997

Browse files
committed
fix: ensure simple_metric_config::to_form_data actually returns only public properties
1 parent 0ecdf5f commit 2d2f997

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

classes/simple_metric_config.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,20 @@ public static function with_form_data(stdClass $formdata): static {
191191
/**
192192
* Transforms an instance into an associative array of data that can be passed to {@see moodleform::set_data}.
193193
*
194-
* Simply casts the instance as an array, turning every public property into a key-value-pair in that array.
194+
* Turns every **public** property into a key-value-pair in that array.
195195
*
196196
* @return array<string, mixed> Data to set on the config form.
197+
*
198+
* @phpcs:disable Squiz.WhiteSpace.ScopeClosingBrace
197199
*/
198200
#[\Override]
199201
public function to_form_data(): array {
200-
return (array) $this;
202+
// This is a dirty hack to quickly get _only_ the public properties.
203+
$closure = fn (simple_metric_config $config): array => get_object_vars($config);
204+
// Since `get_object_vars` is scope-aware, just calling it directly from an instance of a concrete subclass would also
205+
// return any protected properties set on that instance. We simulate a different scope by temporarily binding a closure to
206+
// an instance of an anonymous class; that closure gets the config instance as an argument and calls `get_object_vars`.
207+
return $closure->call(new class {}, $this);
201208
}
202209

203210
/**

0 commit comments

Comments
 (0)