From 6cd61dab8be9514ff47c9f8f7cccd6e841c31945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 4 Oct 2021 12:27:58 +0200 Subject: [PATCH] Warn if property does not exist and add it to DiContainerTrait (#329) --- src/DiContainerTrait.php | 2 ++ src/Exception.php | 2 ++ src/WarnDynamicPropertyTrait.php | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/WarnDynamicPropertyTrait.php diff --git a/src/DiContainerTrait.php b/src/DiContainerTrait.php index 72c236bf..0b74808a 100644 --- a/src/DiContainerTrait.php +++ b/src/DiContainerTrait.php @@ -32,6 +32,8 @@ */ trait DiContainerTrait { + use WarnDynamicPropertyTrait; + /** * Call from __construct() to initialize the properties allowing * developer to pass Dependency Injector Container. diff --git a/src/Exception.php b/src/Exception.php index 32de802f..e8a4fd1e 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -11,6 +11,8 @@ */ class Exception extends \Exception { + use WarnDynamicPropertyTrait; + /** @var array */ public $params = []; diff --git a/src/WarnDynamicPropertyTrait.php b/src/WarnDynamicPropertyTrait.php new file mode 100644 index 00000000..12effcd6 --- /dev/null +++ b/src/WarnDynamicPropertyTrait.php @@ -0,0 +1,51 @@ +warnPropertyDoesNotExist($name); + + return isset($this->{$name}); + } + + /** + * @return mixed + */ + public function &__get(string $name) + { + $this->warnPropertyDoesNotExist($name); + + return $this->{$name}; + } + + /** + * @param mixed $value + */ + public function __set(string $name, $value): void + { + $this->warnPropertyDoesNotExist($name); + + $this->{$name} = $value; + } + + public function __unset(string $name): void + { + $this->warnPropertyDoesNotExist($name); + + unset($this->{$name}); + } +}