Skip to content

Commit

Permalink
Make the dynamic property writable again (#166)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Towers <[email protected]>
  • Loading branch information
mjauvin and LukeTowers authored Feb 20, 2024
1 parent dce5c40 commit e03b13c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/Extension/ExtendableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ trait ExtendableTrait
*/
protected static $extendableStaticMethods = [];

/**
* @var bool Indicates if dynamic properties can be created.
*/
protected static $extendableGuardProperties = true;

/**
* @var ClassLoader|null Class loader instance.
*/
Expand Down Expand Up @@ -207,11 +202,7 @@ public function addDynamicProperty($dynamicName, $value = null)
if (array_key_exists($dynamicName, $this->getDynamicProperties())) {
return;
}
self::$extendableGuardProperties = false;

array_set($this->extensionData['dynamicProperties'], $dynamicName, $value);

self::$extendableGuardProperties = true;
}

/**
Expand Down Expand Up @@ -412,12 +403,12 @@ public function extendableSet($name, $value)
$parent = $this->extensionGetParentClass();
if ($parent !== false && $this->extensionMethodExists($parent, '__set')) {
$this->extensionCallMethod($parent, '__set', [$name, $value]);
return;
}

/*
* Setting an undefined property
*/
if (!self::$extendableGuardProperties) {
// Don't allow automatic creation of dynamic properties through the setter magic method,
// addDynamicProperty() must be used instead.
if (array_key_exists($name, $this->getDynamicProperties())) {
array_set($this->extensionData['dynamicProperties'], $name, $value);
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Extension/ExtendableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ public function testDynamicPropertyOnClass()
$this->assertTrue($subject->propertyExists('dynamicAttribute'));
}

public function testModifyDynamicPropertyValueOnClass()
{
$subject = $this->mockClassLoader(ExtendableTestExampleExtendableClass::class);
$subject->dynamicAttribute = "Initial Value";
$subject->addDynamicProperty('dynamicAttribute', 'Test');
$this->assertEquals('Test', $subject->dynamicAttribute);
$subject->dynamicAttribute = "New value";
$this->assertEquals('New value', $subject->dynamicAttribute);
}

public function testDynamicallyExtendingClass()
{
$subject = $this->mockClassLoader(ExtendableTestExampleExtendableClass::class);
Expand Down

0 comments on commit e03b13c

Please sign in to comment.