diff --git a/src/Container/PackageProxyContainer.php b/src/Container/PackageProxyContainer.php index 49398d0..4ddd049 100644 --- a/src/Container/PackageProxyContainer.php +++ b/src/Container/PackageProxyContainer.php @@ -53,12 +53,9 @@ private function tryContainer(): bool return true; } - /** TODO: We need a better way to deal with status checking besides equality */ if ( - $this->package->statusIs(Package::STATUS_INITIALIZED) - || $this->package->statusIs(Package::STATUS_MODULES_ADDED) - || $this->package->statusIs(Package::STATUS_READY) - || $this->package->statusIs(Package::STATUS_BOOTED) + $this->package->hasContainer() + || $this->package->hasReachedStatus(Package::STATUS_INITIALIZED) ) { $this->container = $this->package->container(); } diff --git a/src/Package.php b/src/Package.php index 91b7654..2c6fbfa 100644 --- a/src/Package.php +++ b/src/Package.php @@ -153,6 +153,14 @@ class Package public const STATUS_BOOTED = 8; public const STATUS_FAILED = -8; + private const SUCCESS_STATUSES = [ + self::STATUS_IDLE => self::STATUS_IDLE, + self::STATUS_INITIALIZED => self::STATUS_INITIALIZED, + self::STATUS_BOOTING => self::STATUS_BOOTING, + self::STATUS_READY => self::STATUS_READY, + self::STATUS_BOOTED => self::STATUS_BOOTED, + ]; + private const OPERATORS = [ '<' => '<', '<=' => '<=', @@ -274,8 +282,8 @@ public function connect(Package $package): bool } // Don't connect, if already booted or boot failed - $failed = $this->statusIs(self::STATUS_FAILED); - if ($failed || $this->checkStatus(self::STATUS_INITIALIZED, '>=')) { + $failed = $this->hasFailed(); + if ($failed || $this->hasReachedStatus(self::STATUS_INITIALIZED)) { $reason = $failed ? 'an errored package' : 'a package with a built container'; $status = $failed ? 'failed' : 'built_container'; $error = "{$errorMessage} to {$reason}."; @@ -606,6 +614,14 @@ public function container(): ContainerInterface return $this->containerConfigurator->createReadOnlyContainer(); } + /** + * @return bool + */ + public function hasContainer(): bool + { + return $this->hasContainer; + } + /** * @return string */ @@ -623,6 +639,27 @@ public function statusIs(int $status): bool return $this->checkStatus($status); } + /** + * @return bool + */ + public function hasFailed(): bool + { + return $this->status === self::STATUS_FAILED; + } + + /** + * @param int $status + * @return bool + */ + public function hasReachedStatus(int $status): bool + { + if ($this->hasFailed()) { + return false; + } + + return isset(self::SUCCESS_STATUSES[$status]) && $this->checkStatus($status, '>='); + } + /** * @param int $status * @param value-of $operator diff --git a/src/Properties/BaseProperties.php b/src/Properties/BaseProperties.php index 9921747..969c1aa 100644 --- a/src/Properties/BaseProperties.php +++ b/src/Properties/BaseProperties.php @@ -10,13 +10,14 @@ class BaseProperties implements Properties protected string $baseName; protected string $basePath; protected ?string $baseUrl; + /** @var array */ protected array $properties; /** * @param string $baseName * @param string $basePath * @param string|null $baseUrl - * @param array $properties + * @param array $properties */ protected function __construct( string $baseName, diff --git a/src/Properties/PluginProperties.php b/src/Properties/PluginProperties.php index 0aacd60..9fa8fd3 100644 --- a/src/Properties/PluginProperties.php +++ b/src/Properties/PluginProperties.php @@ -65,6 +65,7 @@ protected function __construct(string $pluginMainFile) $properties[$key] = $pluginData[$pluginDataKey] ?? ''; unset($pluginData[$pluginDataKey]); } + /** @var array $properties */ $properties = array_merge($properties, $pluginData); $this->pluginMainFile = wp_normalize_path($pluginMainFile); diff --git a/tests/unit/Container/ContainerConfiguratorTest.php b/tests/unit/Container/ContainerConfiguratorTest.php index 68a5122..d583ada 100644 --- a/tests/unit/Container/ContainerConfiguratorTest.php +++ b/tests/unit/Container/ContainerConfiguratorTest.php @@ -526,6 +526,7 @@ public function testCustomContainer(): void $childContainer = new class ($expectedId, $expectedValue) implements ContainerInterface { + /** @var array */ private array $values = []; public function __construct(string $expectedId, object $expectedValue) diff --git a/tests/unit/Container/ReadOnlyContainerTest.php b/tests/unit/Container/ReadOnlyContainerTest.php index f393cc7..fae2594 100644 --- a/tests/unit/Container/ReadOnlyContainerTest.php +++ b/tests/unit/Container/ReadOnlyContainerTest.php @@ -94,6 +94,7 @@ public function testHasGetServiceFromChildContainer(): void $childContainer = new class ($expectedKey, $expectedValue) implements ContainerInterface { + /** @var array */ private array $data = []; public function __construct(string $key, \stdClass $value) diff --git a/tests/unit/PackageTest.php b/tests/unit/PackageTest.php index 75a9811..ebe0035 100644 --- a/tests/unit/PackageTest.php +++ b/tests/unit/PackageTest.php @@ -27,8 +27,28 @@ public function testBasic(): void $package = Package::new($propertiesStub); static::assertTrue($package->statusIs(Package::STATUS_IDLE)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE)); + static::assertFalse($package->hasReachedStatus(Package::STATUS_INITIALIZED)); + static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTING)); + static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTED)); + + $package->build(); + + static::assertFalse($package->statusIs(Package::STATUS_IDLE)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_INITIALIZED)); + static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTING)); + static::assertFalse($package->hasReachedStatus(Package::STATUS_BOOTED)); + static::assertTrue($package->boot()); + static::assertTrue($package->statusIs(Package::STATUS_BOOTED)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_IDLE)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_INITIALIZED)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_BOOTING)); + static::assertTrue($package->hasReachedStatus(Package::STATUS_BOOTED)); + static::assertFalse($package->hasReachedStatus(3)); + static::assertSame($expectedName, $package->name()); static::assertInstanceOf(Properties::class, $package->properties()); static::assertInstanceOf(ContainerInterface::class, $package->container()); @@ -900,6 +920,8 @@ public function testFailureFlowWithFailureOnBootDebugModeOff(): void static::assertFalse($package->boot()); static::assertTrue($package->statusIs(Package::STATUS_FAILED)); + static::assertTrue($package->hasFailed()); + static::assertFalse($package->hasReachedStatus(Package::STATUS_IDLE)); } /** @@ -971,6 +993,7 @@ function (\Throwable $throwable) use ($exception, $package): void { ); static::assertFalse($package->addModule($module1)->addModule($module2)->build()->boot()); + static::assertTrue($package->hasFailed()); static::assertTrue($package->statusIs(Package::STATUS_FAILED)); } @@ -1025,6 +1048,7 @@ function (\Throwable $throwable) use ($exception, $package): void { static::assertFalse($package->connect($connected)); static::assertFalse($package->boot()); static::assertTrue($package->statusIs(Package::STATUS_FAILED)); + static::assertTrue($package->hasFailed()); } /** @@ -1066,6 +1090,7 @@ function (\Throwable $throwable) use ($exception, $package): void { static::assertFalse($package->build()->boot()); static::assertTrue($package->statusIs(Package::STATUS_FAILED)); + static::assertTrue($package->hasFailed()); } /** @@ -1089,6 +1114,7 @@ public function testFailureFlowWithFailureOnBuildDebugModeOn(): void static function (\Throwable $throwable) use ($exception, $package): void { static::assertSame($exception, $throwable); static::assertTrue($package->statusIs(Package::STATUS_FAILED)); + static::assertTrue($package->hasFailed()); } );