Skip to content

Commit

Permalink
Throw on accessing an undeclared property
Browse files Browse the repository at this point in the history
Summary: Currently, `Phobject` throws when trying to //set// an undeclared property but not when trying to //get// an undeclared property. It seems reasonable to add this functionality.

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D13493
  • Loading branch information
joshuaspence committed Jul 6, 2015
1 parent 112843f commit fffe7e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/__phutil_library_map__.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
'PhagePHPAgent' => 'phage/agent/PhagePHPAgent.php',
'PhagePHPAgentBootloader' => 'phage/bootloader/PhagePHPAgentBootloader.php',
'Phobject' => 'object/Phobject.php',
'PhobjectTestCase' => 'object/__tests__/PhobjectTestCase.php',
'PhutilAPCKeyValueCache' => 'cache/PhutilAPCKeyValueCache.php',
'PhutilAWSEC2Future' => 'future/aws/PhutilAWSEC2Future.php',
'PhutilAWSException' => 'future/aws/PhutilAWSException.php',
Expand Down Expand Up @@ -258,7 +259,6 @@
'PhutilPersonTest' => 'internationalization/__tests__/PhutilPersonTest.php',
'PhutilPersonaAuthAdapter' => 'auth/PhutilPersonaAuthAdapter.php',
'PhutilPhabricatorAuthAdapter' => 'auth/PhutilPhabricatorAuthAdapter.php',
'PhutilPhobjectTestCase' => 'object/__tests__/PhutilPhobjectTestCase.php',
'PhutilPhtTestCase' => 'internationalization/__tests__/PhutilPhtTestCase.php',
'PhutilProcessGroupDaemon' => 'daemon/torture/PhutilProcessGroupDaemon.php',
'PhutilProtocolChannel' => 'channel/PhutilProtocolChannel.php',
Expand Down Expand Up @@ -568,6 +568,7 @@
'PhagePHPAgent' => 'Phobject',
'PhagePHPAgentBootloader' => 'PhageAgentBootloader',
'Phobject' => 'Iterator',
'PhobjectTestCase' => 'PhutilTestCase',
'PhutilAPCKeyValueCache' => 'PhutilKeyValueCache',
'PhutilAWSEC2Future' => 'PhutilAWSFuture',
'PhutilAWSException' => 'Exception',
Expand Down Expand Up @@ -762,7 +763,6 @@
),
'PhutilPersonaAuthAdapter' => 'PhutilAuthAdapter',
'PhutilPhabricatorAuthAdapter' => 'PhutilOAuthAuthAdapter',
'PhutilPhobjectTestCase' => 'PhutilTestCase',
'PhutilPhtTestCase' => 'PhutilTestCase',
'PhutilProcessGroupDaemon' => 'PhutilTortureTestDaemon',
'PhutilProtocolChannel' => 'PhutilChannelChannel',
Expand Down
7 changes: 7 additions & 0 deletions src/object/Phobject.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
abstract class Phobject implements Iterator {

public function __get($name) {
throw new DomainException(
pht(
'Attempt to read from undeclared property %s.',
get_class($this).'::'.$name));
}

public function __set($name, $value) {
throw new DomainException(
pht(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
<?php

final class PhutilPhobjectTestCase extends PhutilTestCase {
final class PhobjectTestCase extends PhutilTestCase {

public function testThrowOnUndeclaredProperty() {
$object = new PhutilTestPhobject();

$caught = null;
try {
$object->duck = 'quack';
$object->duck;
} catch (Exception $ex) {
$caught = $ex;
}
$this->assertTrue($caught instanceof DomainException);

$caught = null;
try {
$object->duck = 'quack';
} catch (Exception $ex) {
$caught = $ex;
}
$this->assertTrue($caught instanceof DomainException);
}

Expand Down

0 comments on commit fffe7e4

Please sign in to comment.