diff --git a/src/ProtectedGlobal.php b/src/ProtectedGlobal.php index d2fecf7..c7dfe41 100644 --- a/src/ProtectedGlobal.php +++ b/src/ProtectedGlobal.php @@ -6,7 +6,7 @@ class ProtectedGlobal implements ArrayAccess { const WARNING_MESSAGE = "Global variables are protected - see https://php.gt/globals"; - public $whiteListData = []; + protected $whiteListData; public function __construct(array $whiteListData = []) { $this->whiteListData = $whiteListData; @@ -19,7 +19,7 @@ public function __toString():string { public function __debugInfo():array { return array_merge([ "WARNING" => (string)$this, - ], $this->whiteListData); + ], $this->whiteListData ?? []); } public function offsetExists($offset):bool { @@ -41,6 +41,7 @@ public function offsetGet($offset) { public function offsetSet($offset, $value):void { if(array_key_exists($offset, $this->whiteListData)) { $this->whiteListData[$offset] = $value; + return; } $this->throwException(); @@ -49,6 +50,7 @@ public function offsetSet($offset, $value):void { public function offsetUnset($offset):void { if(array_key_exists($offset, $this->whiteListData)) { unset($this->whiteListData); + return; } $this->throwException(); diff --git a/test/unit/ProtectedGlobalTest.php b/test/unit/ProtectedGlobalTest.php index 9ec95a7..2c490e2 100644 --- a/test/unit/ProtectedGlobalTest.php +++ b/test/unit/ProtectedGlobalTest.php @@ -54,4 +54,23 @@ public function testOffsetSetThrowsException() { self::expectException(ProtectedGlobalException::class); $sut["something"] = "broken"; } + + public function testOffsetSet() { + $whiteList = [ + "name" => "test", + ]; + $sut = new ProtectedGlobal($whiteList); + $sut["name"] = "changed"; + + self::assertEquals("changed", $sut["name"]); + } + + public function testOffsetUnsetThrowsException() { + $whiteList = [ + "name" => "test", + ]; + $sut = new ProtectedGlobal($whiteList); + self::expectException(ProtectedGlobalException::class); + unset($sut["something"]); + } } \ No newline at end of file