From 4682e0c104bd78a26546e48d97a83d58ce980b2a Mon Sep 17 00:00:00 2001 From: vrana Date: Wed, 3 Oct 2012 11:07:56 -0700 Subject: [PATCH] Warn against writing to undeclared properties Summary: I make this error quite often: I forget to declare a property I am writing to or I make a typo in it. PHP implicitly creates a public property which I don't like. I would much rather see a linter warning me against this than this runtime check but writing it is very difficult: - We need to explore all parents of the class we are checking. - It is even possible that children will declare that property but it's OK to treat this as error anyway. - We can extend also builtin or external classes. - It's somewhat doable for `$this` but even more complex for any `$obj` because we don't know the class of it. This should catch significant part of these errors and I'm fine with that. I don't plan escalating to exception because this error is not fatal and should not stop the application from working. Test Plan: Loaded homepage, checked log. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D3601 --- src/aphront/AphrontController.php | 5 +++++ src/infrastructure/storage/lisk/LiskDAO.php | 11 +++++++++++ src/view/AphrontView.php | 7 ++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/aphront/AphrontController.php b/src/aphront/AphrontController.php index 94f0071f18..8fc6a48d1d 100644 --- a/src/aphront/AphrontController.php +++ b/src/aphront/AphrontController.php @@ -61,4 +61,9 @@ abstract class AphrontController { return $this->currentApplication; } + public function __set($name, $value) { + phlog('Wrote to undeclared property '.get_class($this).'::$'.$name.'.'); + $this->$name = $value; + } + } diff --git a/src/infrastructure/storage/lisk/LiskDAO.php b/src/infrastructure/storage/lisk/LiskDAO.php index 80f7ef051f..a1e65df2ac 100644 --- a/src/infrastructure/storage/lisk/LiskDAO.php +++ b/src/infrastructure/storage/lisk/LiskDAO.php @@ -1796,4 +1796,15 @@ abstract class LiskDAO { throw new Exception("Unable to resolve method '{$method}'."); } + + /** + * Warns against writing to undeclared property. + * + * @task util + */ + public function __set($name, $value) { + phlog('Wrote to undeclared property '.get_class($this).'::$'.$name.'.'); + $this->$name = $value; + } + } diff --git a/src/view/AphrontView.php b/src/view/AphrontView.php index 99196f28e1..23807285a8 100644 --- a/src/view/AphrontView.php +++ b/src/view/AphrontView.php @@ -1,7 +1,7 @@ $name = $value; + } + }