From b090dcfac16c2159edd620bd392511904d5faa41 Mon Sep 17 00:00:00 2001 From: Pollora maintainer Date: Sat, 21 Nov 2026 16:55:07 +0100 Subject: [PATCH] refactor: make 8 classes final (CONV-08) --- CHANGELOG.md | 1 + src/Application/Services/AssetFile.php | 2 +- .../Services/AssetRegistrationService.php | 2 +- src/Application/Services/MissingBuilds.php | 2 +- src/Domain/Exceptions/AssetException.php | 2 +- src/Domain/Models/ViteManager.php | 2 +- src/Domain/Services/AssetContainerManager.php | 2 +- .../Repositories/InMemoryAssetRepository.php | 2 +- src/Infrastructure/Services/AssetEnqueuer.php | 72 +++++++++---------- tests/Unit/AssetManagerTest.php | 6 +- 10 files changed, 47 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5b7ecf..ab0e4f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- `AssetFile` (application), `AssetRegistrationService`, `MissingBuilds`, `AssetException`, `ViteManager`, `AssetContainerManager`, `InMemoryAssetRepository` and `AssetEnqueuer` are now `final` (CONV-08). This is a restriction of the API for anyone who extended them: a class that is not an extension point can no longer be subclassed; compose them instead - `AssetContainer` moves from `Infrastructure\Repositories` to `Domain\Models` and `AssetFile` from `Infrastructure\Services` to `Application\Services`, so that the domain and application layers no longer depend on the infrastructure layer. Each former class name stays available as a deprecated alias, removed in the next major version - `AssetManager::add()` returns the new `AssetEnqueuerInterface` contract, which the `AssetEnqueuer` implements, instead of the concrete `AssetEnqueuer`. The fluent methods are the same - An unknown asset container, and `useVite()` called before a container is chosen, throw the named `AssetContainerNotFoundException` exception instead of a bare `\RuntimeException`. It extends `\RuntimeException` and the messages are unchanged diff --git a/src/Application/Services/AssetFile.php b/src/Application/Services/AssetFile.php index 6cc2fc3..d943e25 100755 --- a/src/Application/Services/AssetFile.php +++ b/src/Application/Services/AssetFile.php @@ -14,7 +14,7 @@ * This class extends the domain AssetFile and implements the AssetFileInterface contract. * It resolves the public URL of an asset using the configured container and Vite integration. */ -class AssetFile extends DomainAssetFile implements AssetFileInterface +final class AssetFile extends DomainAssetFile implements AssetFileInterface { /** * What turns a file and the name of its container into a URL, set by the environment. diff --git a/src/Application/Services/AssetRegistrationService.php b/src/Application/Services/AssetRegistrationService.php index 71a8d8f..77f24ca 100755 --- a/src/Application/Services/AssetRegistrationService.php +++ b/src/Application/Services/AssetRegistrationService.php @@ -13,7 +13,7 @@ * This service acts as a use case boundary for asset registration, ensuring * that assets are properly instantiated and delegated to the container manager. */ -class AssetRegistrationService +final class AssetRegistrationService { /** * Initializes the registration service with the container manager. diff --git a/src/Application/Services/MissingBuilds.php b/src/Application/Services/MissingBuilds.php index b38ca17..d4dcaec 100644 --- a/src/Application/Services/MissingBuilds.php +++ b/src/Application/Services/MissingBuilds.php @@ -16,7 +16,7 @@ * reachable: each missing build is logged once per request and listed in * wp-admin by `MissingBuildNotice`. */ -class MissingBuilds +final class MissingBuilds { public function __construct(private readonly ?LoggerInterface $logger = null) {} diff --git a/src/Domain/Exceptions/AssetException.php b/src/Domain/Exceptions/AssetException.php index a7574a9..49f604b 100755 --- a/src/Domain/Exceptions/AssetException.php +++ b/src/Domain/Exceptions/AssetException.php @@ -13,4 +13,4 @@ * during asset management, such as invalid configuration, missing files, * or unsupported asset types. */ -class AssetException extends Exception {} +final class AssetException extends Exception {} diff --git a/src/Domain/Models/ViteManager.php b/src/Domain/Models/ViteManager.php index 44ce4fc..080c0f6 100755 --- a/src/Domain/Models/ViteManager.php +++ b/src/Domain/Models/ViteManager.php @@ -11,7 +11,7 @@ * * Use for testing or pure domain logic. */ -class ViteManager implements ViteManagerInterface +final class ViteManager implements ViteManagerInterface { /** * Returns a stub asset container instance. diff --git a/src/Domain/Services/AssetContainerManager.php b/src/Domain/Services/AssetContainerManager.php index 96e6f96..4e53575 100755 --- a/src/Domain/Services/AssetContainerManager.php +++ b/src/Domain/Services/AssetContainerManager.php @@ -13,7 +13,7 @@ * This service provides methods to register assets, retrieve a single asset by name, * and fetch all registered assets via the configured repository implementation. */ -class AssetContainerManager +final class AssetContainerManager { /** * Initializes the container manager with a repository implementation. diff --git a/src/Infrastructure/Repositories/InMemoryAssetRepository.php b/src/Infrastructure/Repositories/InMemoryAssetRepository.php index f29c9dc..3e6b4e6 100755 --- a/src/Infrastructure/Repositories/InMemoryAssetRepository.php +++ b/src/Infrastructure/Repositories/InMemoryAssetRepository.php @@ -14,7 +14,7 @@ * prototyping, or scenarios where persistence is not required. Assets are * stored in a simple associative array keyed by their name/handle. */ -class InMemoryAssetRepository implements AssetRepositoryInterface +final class InMemoryAssetRepository implements AssetRepositoryInterface { /** * Internal array of assets, keyed by asset name/handle. diff --git a/src/Infrastructure/Services/AssetEnqueuer.php b/src/Infrastructure/Services/AssetEnqueuer.php index cce30c3..daf4180 100755 --- a/src/Infrastructure/Services/AssetEnqueuer.php +++ b/src/Infrastructure/Services/AssetEnqueuer.php @@ -24,7 +24,7 @@ * WordPress enqueuing and Vite.js integration. It handles various asset loading scenarios * including dependencies, versioning, and conditional loading. */ -class AssetEnqueuer implements AssetEnqueuerInterface +final class AssetEnqueuer implements AssetEnqueuerInterface { /** * Hooks on whose pages WordPress prints script modules, after its import map. @@ -46,83 +46,83 @@ class AssetEnqueuer implements AssetEnqueuerInterface * * @var string|array */ - protected string|array $path; + private string|array $path; /** * The type of asset ('css' or 'js'). */ - protected string $type; + private string $type; /** * Array of asset handles that this asset depends on. * * @var array */ - protected array $dependencies = []; + private array $dependencies = []; /** * The asset container instance. */ - protected ?AssetContainer $container = null; + private ?AssetContainer $container = null; /** * The Vite manager instance. */ - protected ?ViteManagerInterface $viteManager = null; + private ?ViteManagerInterface $viteManager = null; /** * Whether to use Vite.js for asset handling. */ - protected bool $useVite = false; + private bool $useVite = false; /** * The version string for cache busting. */ - protected ?string $version = null; + private ?string $version = null; /** * The media query for stylesheet (e.g., 'all', 'print', 'screen'). */ - protected string $media = 'all'; + private string $media = 'all'; /** * Whether to load the script in the footer. */ - protected bool $loadInFooter = false; + private bool $loadInFooter = false; /** * The loading strategy for scripts (e.g., 'defer', 'async'). */ - protected ?string $loadStrategy = null; + private ?string $loadStrategy = null; /** * The content to be added inline with the asset. */ - protected ?string $inlineContent = null; + private ?string $inlineContent = null; /** * The position for inline content ('before' or 'after'). */ - protected ?string $inlinePosition = null; + private ?string $inlinePosition = null; /** * Array of WordPress hooks where the asset should be enqueued. * * @var array */ - protected array $hooks = []; + private array $hooks = []; /** * The asset handle (unique identifier). */ - protected string $handle; + private string $handle; /** * Stored localization data to apply after enqueuing. * * @var array */ - protected array $localizationData = []; + private array $localizationData = []; /** * @param AssetManager $assetManager Resolves the containers by name @@ -133,12 +133,12 @@ class AssetEnqueuer implements AssetEnqueuerInterface * @param LoggerInterface|null $logger Where a failure of the deferred enqueue is reported */ public function __construct( - protected AssetManager $assetManager, - protected ?HookAction $action = null, - protected ?HookFilter $filter = null, - protected ?ViteManagerFactory $viteManagers = null, - protected ?RuntimeContext $runtime = null, - protected ?LoggerInterface $logger = null, + private readonly AssetManager $assetManager, + private readonly ?HookAction $action = null, + private readonly ?HookFilter $filter = null, + private readonly ?ViteManagerFactory $viteManagers = null, + private readonly ?RuntimeContext $runtime = null, + private readonly ?LoggerInterface $logger = null, ) {} /** @@ -146,7 +146,7 @@ public function __construct( * * @throws AssetException When the enqueuer was built without one */ - protected function hookAction(): HookAction + private function hookAction(): HookAction { return $this->action ?? throw new AssetException('No hook service is set: the enqueuer was built without an Action.'); } @@ -156,7 +156,7 @@ protected function hookAction(): HookAction * * @throws AssetException When the enqueuer was built without one */ - protected function hookFilter(): HookFilter + private function hookFilter(): HookFilter { return $this->filter ?? throw new AssetException('No filter service is set: the enqueuer was built without a Filter.'); } @@ -418,7 +418,7 @@ public function enqueueStyleOrScript(?string $hook = null): void * Adds a WordPress hook for asset enqueuing. * * @param string $hook WordPress hook name\n */ - protected function addHook(string $hook): self + private function addHook(string $hook): self { $this->hooks[] = $hook; @@ -430,7 +430,7 @@ protected function addHook(string $hook): self * * @param string $hook WordPress hook to attach the client script */ - protected function loadViteClient(string $hook): void + private function loadViteClient(string $hook): void { if ($this->printsScriptModules($hook)) { // The client is a module too: printed ahead of the import map, it would void it. @@ -476,7 +476,7 @@ public function configureViteAssets(): void * * @return bool True if Vite client should be loaded */ - protected function needToLoadViteClient(): bool + private function needToLoadViteClient(): bool { return $this->useVite && $this->viteManager instanceof ViteManagerInterface && $this->viteManager->isRunningHot(); } @@ -486,7 +486,7 @@ protected function needToLoadViteClient(): bool * * @return array Array of asset paths grouped by type */ - protected function getAssetPaths(): array + private function getAssetPaths(): array { if ($this->useVite && ! $this->viteManager->isRunningHot()) { @@ -504,7 +504,7 @@ protected function getAssetPaths(): array * * @throws \InvalidArgumentException When asset type is not supported */ - protected function enqueueAsset(string $type, string $path, ?string $hook = null): void + private function enqueueAsset(string $type, string $path, ?string $hook = null): void { $handle = $this->useVite && ! $this->viteManager->isRunningHot() ? $this->handle.'/'.sanitize_title(basename($path)) : $this->handle; match ($type) { @@ -521,7 +521,7 @@ protected function enqueueAsset(string $type, string $path, ?string $hook = null * * @param string $path Path to the JavaScript file */ - protected function enqueueScript(string $path, string $handle): void + private function enqueueScript(string $path, string $handle): void { wp_enqueue_script($handle, $path, $this->dependencies, $this->version, $this->loadInFooter); @@ -545,7 +545,7 @@ protected function enqueueScript(string $path, string $handle): void /** * Whether a Vite entry enqueued on this hook is printed by WordPress as a script module. */ - protected function printsScriptModules(?string $hook): bool + private function printsScriptModules(?string $hook): bool { return in_array($hook, self::SCRIPT_MODULE_HOOKS, true) && function_exists('wp_enqueue_script_module'); } @@ -558,7 +558,7 @@ protected function printsScriptModules(?string $hook): bool * `{handle}-data`, printed in the head: it runs before the module, which the * browser defers. */ - protected function enqueueScriptModule(string $path, string $handle): void + private function enqueueScriptModule(string $path, string $handle): void { // null, not false: false appends WordPress's version, and a module is identified by its // exact URL — a chunk importing the entry back would load a second copy of it. @@ -594,7 +594,7 @@ protected function enqueueScriptModule(string $path, string $handle): void * @param string $path Path to the CSS file * @param string $handle Unique identifier for the stylesheet */ - protected function enqueueStyle(string $path, string $handle): void + private function enqueueStyle(string $path, string $handle): void { wp_enqueue_style($handle, $path, $this->dependencies, $this->version, $this->media); if (! in_array($this->inlineContent, [null, '', '0'], true)) { @@ -605,7 +605,7 @@ protected function enqueueStyle(string $path, string $handle): void /** * Adds module and crossorigin attributes to Vite script tags. */ - protected function addViteScriptAttributes(string $handle): void + private function addViteScriptAttributes(string $handle): void { $this->hookFilter()->add('script_loader_tag', fn ($tag, $scriptHandle, $src) => $scriptHandle === $handle ? '' @@ -618,7 +618,7 @@ protected function addViteScriptAttributes(string $handle): void * @param string $path Asset path * @return string Full URL to the asset */ - protected function forceFullUrl(string $path): string + private function forceFullUrl(string $path): string { if (str_contains($path, '://')) { return $path; @@ -638,7 +638,7 @@ protected function forceFullUrl(string $path): string * * @throws \InvalidArgumentException When file type is not supported */ - protected function determineFileType(string $path): string + private function determineFileType(string $path): string { $type = pathinfo($path, PATHINFO_EXTENSION); diff --git a/tests/Unit/AssetManagerTest.php b/tests/Unit/AssetManagerTest.php index 7a60182..85d8145 100644 --- a/tests/Unit/AssetManagerTest.php +++ b/tests/Unit/AssetManagerTest.php @@ -4,10 +4,10 @@ use Pollora\Asset\Application\Services\AssetManager; use Pollora\Asset\Application\Services\AssetRetrievalService; +use Pollora\Asset\Domain\Contracts\AssetEnqueuerInterface; use Pollora\Asset\Domain\Contracts\PublicPath; use Pollora\Asset\Domain\Exceptions\AssetException; use Pollora\Asset\Domain\Models\AssetContainer; -use Pollora\Asset\Infrastructure\Services\AssetEnqueuer; /** A public directory at a known place */ function publicPathAt(string $root): PublicPath @@ -72,11 +72,11 @@ public function path(string $to = ''): string describe('AssetManager::add()', function (): void { it('starts the enqueuer the environment builds, with the handle and the file', function (): void { - $enqueuer = Mockery::mock(AssetEnqueuer::class); + $enqueuer = Mockery::mock(AssetEnqueuerInterface::class); $enqueuer->shouldReceive('handle')->once()->with('theme/app')->andReturnSelf(); $enqueuer->shouldReceive('path')->once()->with('app.js')->andReturnSelf(); - $manager = new AssetManager(Mockery::mock(AssetRetrievalService::class), null, fn (): AssetEnqueuer => $enqueuer); + $manager = new AssetManager(Mockery::mock(AssetRetrievalService::class), null, fn (): AssetEnqueuerInterface => $enqueuer); expect($manager->add('theme/app', 'app.js'))->toBe($enqueuer); });