From 18d52ea402785a6ca3eaa3f629a5f7bb1b7695f3 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 25 Feb 2019 23:37:14 -0500 Subject: [PATCH] Make exclusion contexts return their parent on change --- lib/Context/Context.php | 6 +++++- lib/Context/ExclusionContext.php | 20 +++++++++++++++++++- tests/cases/Misc/TestContext.php | 2 ++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/Context/Context.php b/lib/Context/Context.php index d9977735..df45dc6d 100644 --- a/lib/Context/Context.php +++ b/lib/Context/Context.php @@ -27,7 +27,7 @@ class Context extends ExclusionContext { public $notMarkedSince; public function __construct() { - $this->not = new ExclusionContext; + $this->not = new ExclusionContext($this); } public function __clone() { @@ -35,6 +35,10 @@ class Context extends ExclusionContext { $this->not = clone $this->not; } + public function __destruct() { + unset($this->not); + } + public function reverse(bool $spec = null) { return $this->act(__FUNCTION__, func_num_args(), $spec); } diff --git a/lib/Context/ExclusionContext.php b/lib/Context/ExclusionContext.php index 5a2a9cf6..6a662f28 100644 --- a/lib/Context/ExclusionContext.php +++ b/lib/Context/ExclusionContext.php @@ -24,6 +24,24 @@ class ExclusionContext { public $authorTerms; protected $props = []; + protected $parent; + + public function __construct(self $c = null) { + $this->parent = $c; + } + + public function __clone() { + if ($this->parent) { + $p = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1]['object'] ?? null; + if ($p instanceof self) { + $this->parent = $p; + } + } + } + + public function __destruct() { + unset($this->parent); + } protected function act(string $prop, int $set, $value) { if ($set) { @@ -34,7 +52,7 @@ class ExclusionContext { $this->props[$prop] = true; $this->$prop = $value; } - return $this; + return $this->parent ?? $this; } else { return isset($this->props[$prop]); } diff --git a/tests/cases/Misc/TestContext.php b/tests/cases/Misc/TestContext.php index db088b4f..d134c0fc 100644 --- a/tests/cases/Misc/TestContext.php +++ b/tests/cases/Misc/TestContext.php @@ -104,5 +104,7 @@ class TestContext extends \JKingWeb\Arsse\Test\AbstractTest { $this->assertEquals($c1->not, $c2->not); $this->assertNotSame($c1, $c2); $this->assertNotSame($c1->not, $c2->not); + $this->assertSame($c1, $c1->not->article(null)); + $this->assertSame($c2, $c2->not->article(null)); } }