Skip to content

Commit

Permalink
Hardening logic to prevent multiple unwanted initialization of public…
Browse files Browse the repository at this point in the history
… properties
  • Loading branch information
Ocramius committed Nov 28, 2013
1 parent 24be7f7 commit b85dc27
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(
$initialization = $initializationTracker->getName();

$this->setBody(
'if ($this->' . $initialization . ') {' . "\n return;\n}\n\n"
'if ($this->' . $initialization . ' || ! $this->' . $initializer . ') {' . "\n return;\n}\n\n"
. "\$this->" . $initialization . " = true;\n\n"
. "foreach (self::\$" . $publicPropertiesDefaults->getName() . " as \$key => \$default) {\n"
. " \$this->\$key = \$default;\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ public function testWillModifyByRefRetrievedPublicProperties()
$this->assertSame('foo', $proxy->property0);
}

/**
* Verifies that properties' default values are preserved
*/
public function testKeepsInitializerWhenNotOverwitten()
{
$instance = new BaseClass();
Expand All @@ -224,6 +221,30 @@ public function testKeepsInitializerWhenNotOverwitten()
$this->assertSame($initializer, $proxy->getProxyInitializer());
}

/**
* Verifies that public properties are not being initialized multiple times
*/
public function testKeepsInitializedPublicProperties()
{
$instance = new BaseClass();
$proxyName = $this->generateProxy(get_class($instance));
$initializer = function (BaseClass $proxy, $method, $parameters, & $initializer) {
$initializer = null;
$proxy->publicProperty = 'newValue';
};
/* @var $proxy \ProxyManager\Proxy\GhostObjectInterface|BaseClass */
$proxy = new $proxyName($initializer);

$proxy->initializeProxy();
$this->assertSame('newValue', $proxy->publicProperty);

$proxy->publicProperty = 'otherValue';

$proxy->initializeProxy();

$this->assertSame('otherValue', $proxy->publicProperty);
}

/**
* Verifies that properties' default values are preserved
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testBodyStructure()
$callInitializer = new CallInitializer($initializer, $propertiesDefaults, $initializationTracker);

$this->assertStringMatchesFormat(
'%Aif ($this->track) {%areturn;%a}%a'
'%Aif ($this->track || ! $this->init) {%areturn;%a}%a'
. '$this->track = true;%a'
. 'foreach (self::$props as $key => $default) {%a'
. '$this->$key = $default;%a'
Expand Down

0 comments on commit b85dc27

Please sign in to comment.