diff --git a/.harness.json b/.harness.json index 60eb3f3..9fb6e75 100644 --- a/.harness.json +++ b/.harness.json @@ -5,10 +5,10 @@ "framework" ], "ratchet": { - "type_coverage": 99.6, - "phpstan_level": 5, + "phpstan_level": 6, "phpstan_baseline": 0, "coverage": 93.05, + "type_coverage": 99.66, "msi": 94 } } diff --git a/composer.json b/composer.json index 796a672..c99318a 100644 --- a/composer.json +++ b/composer.json @@ -51,8 +51,8 @@ "test:unit": "pest --testsuite=Unit", "test:integration": "@php -d auto_prepend_file=tests/WordPress/load.php vendor/bin/pest -c phpunit.wordpress.xml", "test:wordpress": "@test:integration", - "phpstan": "phpstan analyse src tests --level=5", - "phpstan:ci": "phpstan analyse src tests --level=5 --no-progress", + "phpstan": "phpstan analyse src tests", + "phpstan:ci": "phpstan analyse src tests --no-progress", "lint": "pint", "lint:check": "pint --test", "qa": "@php vendor/bin/pollora-qa check", diff --git a/phpstan.neon b/phpstan.neon index f1be827..d50092c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,16 +3,17 @@ includes: - vendor/pollora/harness/presets/phpstan/base.neon parameters: - level: 5 + level: 6 paths: - src - tests + universalObjectCratesClasses: + # Pest tests keep their fixtures in dynamic properties of $this (beforeEach): mixed, not an error type that expect() cannot resolve + - PHPUnit\Framework\TestCase excludePaths: - vendor/* ignoreErrors: # Action Scheduler is a plugin: its functions are not part of the WordPress stubs - '#Function as_[a-z_]+ not found#' - # Pest.php dynamic properties - - '#Access to an undefined property PHPUnit\\Framework\\TestCase::\$[a-zA-Z0-9_]+.#' # Only patterns still needed are listed: report one that no longer matches, so it is removed reportUnmatchedIgnoredErrors: true diff --git a/src/Action.php b/src/Action.php index 5ca17c0..6112251 100644 --- a/src/Action.php +++ b/src/Action.php @@ -20,6 +20,9 @@ * Action::remove('init', $callback); * * @see WordPressAction The underlying WordPress adapter. + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface + * @phpstan-import-type HookRegistration from \Pollora\Hook\Domain\Contract\HookInterface */ class Action { @@ -28,8 +31,8 @@ class Action /** * Add an action hook. * - * @param string|array $hooks Hook name(s). - * @param callable|string|array $callback Callback. + * @param string|list $hooks Hook name(s). + * @param HookCallback $callback Callback. * @param int $priority Priority (default 10). * @param int|null $acceptedArgs Argument count (auto-detected if null). * @return WordPressAction The adapter, to chain calls such as async() @@ -57,7 +60,7 @@ public static function do(string $hook, mixed ...$args): void * Remove an action hook. * * @param string $hook Hook name. - * @param callable|string|array|null $callback Callback to remove. + * @param HookCallback|null $callback Callback to remove. * @param int $priority Priority (default 10). */ public static function remove(string $hook, callable|string|array|null $callback = null, int $priority = 10): bool @@ -79,7 +82,7 @@ public static function exists(string $hook): bool * Get registered callbacks for an action hook. * * @param string $hook Hook name. - * @return array|null Callbacks or null. + * @return list|null Callbacks or null. */ public static function callbacks(string $hook): ?array { diff --git a/src/Adapter/Out/WordPress/Action.php b/src/Adapter/Out/WordPress/Action.php index 8f724b8..b8214aa 100644 --- a/src/Adapter/Out/WordPress/Action.php +++ b/src/Adapter/Out/WordPress/Action.php @@ -19,6 +19,9 @@ * * Implements the Action contract by delegating to WordPress * `add_action()`, `remove_action()`, and `do_action()` functions. + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface + * @phpstan-import-type HookRegistration from \Pollora\Hook\Domain\Contract\HookInterface */ class Action extends AbstractHook implements ActionContract { @@ -121,6 +124,7 @@ protected function removeHookEvent(string $hook, callable|string|array $callback /** * The public capture() method of a handler class, used when capture() is not called. * + * @param HookCallback $callback The registered callback * @return array{0: object, 1: 'capture'}|null */ private function defaultCapture(callable|string|array $callback): ?array @@ -146,6 +150,10 @@ private function makeSynchronous(QueuedHandler $queued): void /** * Replace a callback, in WordPress and in the tracked registrations. + * + * @param HookCallback $current The callback registered today + * @param HookCallback $replacement The callback to register instead + * @param HookCallback|null $handler The handler the replacement stands for, null for none */ private function swapCallback(string $hook, int $priority, callable|string|array $current, callable|string|array $replacement, int $acceptedArgs, callable|string|array|null $handler): void { diff --git a/src/Adapter/Out/WordPress/Filter.php b/src/Adapter/Out/WordPress/Filter.php index df68751..a7cea21 100644 --- a/src/Adapter/Out/WordPress/Filter.php +++ b/src/Adapter/Out/WordPress/Filter.php @@ -12,6 +12,8 @@ * * Implements the Filter contract by delegating to WordPress * `add_filter()`, `remove_filter()`, and `apply_filters()` functions. + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface */ class Filter extends AbstractHook implements FilterContract { diff --git a/src/Async/AsyncRunner.php b/src/Async/AsyncRunner.php index a09ab0e..d59ce72 100644 --- a/src/Async/AsyncRunner.php +++ b/src/Async/AsyncRunner.php @@ -16,6 +16,8 @@ * handler does not stop the others a WP-Cron run holds. * * @internal + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface */ final class AsyncRunner { @@ -69,6 +71,7 @@ public function run(AsyncPayload $payload, bool $throwOnFinalFailure = false): v /** * Call a handler with the hook arguments and, when it asks for it, the context. * + * @param HookCallback $callback * @param array $arguments */ public function call(callable|string|array $callback, array $arguments, AsyncContext $context): void diff --git a/src/Async/CallableDescriptor.php b/src/Async/CallableDescriptor.php index a048fb1..97a1fb9 100644 --- a/src/Async/CallableDescriptor.php +++ b/src/Async/CallableDescriptor.php @@ -23,6 +23,8 @@ * HMAC (Async::closureKey()); the signature is checked before anything is * unserialized. A closure made from a named function or method (strlen(...), * $object->method(...)) is described by that name instead. + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface */ final readonly class CallableDescriptor { @@ -37,7 +39,7 @@ public function __construct( /** * Describe a handler as a string. * - * @param callable|string|array $callback A function name, a [class or object, method] pair, a 'Class::method' string or an invokable object + * @param HookCallback $callback A function name, a [class or object, method] pair, a 'Class::method' string or an invokable object * * @throws UnresolvableHandler When the handler has no name to be resolved by later */ @@ -71,6 +73,7 @@ public function describe(callable|string|array $callback): string * is pluggable. Its serialization is checked at once; it is signed when its * hook first fires. * + * @param HookCallback $callback The registered callback * @return string|null The descriptor, or null for a closure to describe later * * @throws UnresolvableHandler When the handler can never be described diff --git a/src/Async/HandlerArguments.php b/src/Async/HandlerArguments.php index e145386..27eefad 100644 --- a/src/Async/HandlerArguments.php +++ b/src/Async/HandlerArguments.php @@ -12,13 +12,15 @@ * they sit; the other parameters receive the hook arguments, in order. * * @internal + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface */ final class HandlerArguments { /** * How many hook arguments a handler takes, its AsyncContext and injected parameters left out. * - * @param callable|string|array $callback The registered callback + * @param HookCallback $callback The registered callback * @param int $registeredArgs The argument count it was registered with */ public static function hookArgumentCount(callable|string|array $callback, int $registeredArgs): int @@ -95,6 +97,9 @@ private static function isContextParameter(\ReflectionParameter $parameter): boo return $type instanceof \ReflectionNamedType && $type->getName() === AsyncContext::class; } + /** + * @param HookCallback $callback + */ private static function reflect(callable|string|array $callback): ?\ReflectionFunctionAbstract { try { diff --git a/src/Async/QueuedHandler.php b/src/Async/QueuedHandler.php index fb143fa..96aba71 100644 --- a/src/Async/QueuedHandler.php +++ b/src/Async/QueuedHandler.php @@ -8,6 +8,8 @@ * The callback WordPress calls in place of an asynchronous handler: it queues the handler. * * @internal + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface */ final class QueuedHandler { @@ -15,7 +17,7 @@ final class QueuedHandler * @param string $hook Hook the handler is registered on * @param int $priority Priority of the registration * @param string|null $descriptor Handler descriptor, see CallableDescriptor; null for a closure, signed when its hook first fires - * @param callable|string|array $handler The handler as registered, run in place when queuing fails + * @param HookCallback $handler The handler as registered, run in place when queuing fails * @param int $registeredArgs Argument count of the original registration * @param int $acceptedArgs Hook arguments the handler takes, AsyncContext left out * @param PendingAsync $options Options, read when the hook fires diff --git a/src/Domain/Contract/HookInterface.php b/src/Domain/Contract/HookInterface.php index b540701..6d30972 100644 --- a/src/Domain/Contract/HookInterface.php +++ b/src/Domain/Contract/HookInterface.php @@ -9,14 +9,17 @@ * * Defines the common contract for both action and filter hooks, * including registration, removal, existence check, and callback listing. + * + * @phpstan-type HookCallback callable|string|array + * @phpstan-type HookRegistration array{hook: string, callback: HookCallback, priority: int, args: int, handler?: HookCallback} */ interface HookInterface { /** * Add a hook callback. * - * @param string|array $hooks Hook name or array of hook names. - * @param callable|string|array $callback The callback function, class name, or [class, method] pair. + * @param string|list $hooks Hook name or array of hook names. + * @param HookCallback $callback The callback function, class name, or [class, method] pair. * @param int $priority Optional priority (default 10). * @param int|null $acceptedArgs Optional argument count (auto-detected if null). */ @@ -26,7 +29,7 @@ public function add(string|array $hooks, callable|string|array $callback, int $p * Remove a hook callback. * * @param string $hook Hook name. - * @param callable|string|array|null $callback Optional callback to remove (removes first if null). + * @param HookCallback|null $callback Optional callback to remove (removes first if null). * @param int $priority Optional priority (default 10). */ public function remove(string $hook, callable|string|array|null $callback = null, int $priority = 10): self|false; @@ -42,7 +45,7 @@ public function exists(string $hook): bool; * Get the registered callbacks for a hook. * * @param string $hook Hook name. - * @return array|null Callbacks array or null if hook doesn't exist. + * @return list|null Callbacks array or null if hook doesn't exist. */ public function callbacks(string $hook): ?array; } diff --git a/src/Domain/Service/AbstractHook.php b/src/Domain/Service/AbstractHook.php index 521cf2c..bca3be9 100644 --- a/src/Domain/Service/AbstractHook.php +++ b/src/Domain/Service/AbstractHook.php @@ -17,6 +17,9 @@ * with dependency resolution and argument detection, but does NOT depend on Laravel or WordPress. * * This class is a POPO and does not depend on any framework collections or helpers. + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface + * @phpstan-import-type HookRegistration from \Pollora\Hook\Domain\Contract\HookInterface */ abstract class AbstractHook implements HookInterface { @@ -31,6 +34,8 @@ abstract class AbstractHook implements HookInterface * ], * ... * ] + * + * @var array> */ protected array $hooks = []; @@ -41,6 +46,8 @@ abstract class AbstractHook implements HookInterface * [ * 'cacheKey' => numberOfParameters * ] + * + * @var array */ protected static array $reflectionCache = []; @@ -55,7 +62,7 @@ abstract class AbstractHook implements HookInterface /** * Registrations made by the last add() call, one per hook. * - * @var list + * @var list */ protected array $lastRegistrations = []; @@ -85,8 +92,8 @@ public function setCallbackResolver(CallbackResolverInterface $resolver): void /** * Add one or multiple hooks with a callback. * - * @param string|array $hooks Hook name or array of hook names - * @param callable|string|array $callback Function, closure, class name, or class@method + * @param string|list $hooks Hook name or array of hook names + * @param HookCallback $callback Function, closure, class name, or class@method * @param int $priority Optional. Priority of the hook (default: 10) * @param int|null $acceptedArgs Optional. Number of arguments the callback accepts (default: auto-detected) * @return static The same instance, to chain calls such as async() @@ -115,7 +122,7 @@ public function add(string|array $hooks, callable|string|array $callback, int $p * Remove a registered hook. * * @param string $hook The hook name to remove - * @param callable|string|array|null $callback Optional. Specific callback to remove + * @param HookCallback|null $callback Optional. Specific callback to remove * @param int $priority Optional. Priority of the hook to remove. Default is 10. * @return self|false The Hook instance or false if the hook doesn't exist */ @@ -169,7 +176,7 @@ public function remove(string $hook, callable|string|array|null $callback = null * Check if a hook exists. * * @param string $hook The hook name to check - * @param callable|string|array|null $callback Optional. Specific callback to check + * @param HookCallback|null $callback Optional. Specific callback to check * @param int|null $priority Optional. Specific priority to check * @return bool True if the hook exists, false otherwise */ @@ -217,7 +224,7 @@ public static function clearReflectionCache(): void * Get the registered callbacks for a hook. * * @param string $hook The hook name. - * @return array|null Returns an array of callbacks or null if the hook doesn't exist + * @return list|null Returns an array of callbacks or null if the hook doesn't exist */ public function callbacks(string $hook): ?array { @@ -235,7 +242,7 @@ public function callbacks(string $hook): ?array * who registered them: a profiler reads this to mark the ones Pollora put * there, and the class and method they resolved to. * - * @return array> + * @return array> */ public function all(): array { @@ -246,9 +253,9 @@ public function all(): array * Resolve the callback for the hook. * * @param string $hook The hook name - * @param callable|string|array $callback Function, closure, class name, or class@method + * @param HookCallback $callback Function, closure, class name, or class@method * @param int|null $acceptedArgs Optional. Number of arguments (if null, it will be auto-detected) - * @return array Resolved callback and argument count + * @return array{callable: HookCallback, args: int} Resolved callback and argument count * * @throws \InvalidArgumentException|\RuntimeException */ @@ -289,7 +296,7 @@ protected function resolveCallback(string $hook, callable|string|array $callback * @param string $hook The hook name * @param string $className The class name * @param int|null $acceptedArgs Optional. Number of arguments (auto-detected if null) - * @return array Resolved class method callback and argument count + * @return array{callable: array{object, string}, args: int} Resolved class method callback and argument count * * @throws CallbackResolutionFailedException When the class cannot be built or has no method named after the hook */ @@ -322,7 +329,7 @@ protected function resolveClassMethodCallback(string $hook, string $className, ? * @param class-string $className The class name * @param string $method The instance method to call * @param int|null $acceptedArgs Optional. Number of arguments (auto-detected if null) - * @return array Resolved class method callback and argument count + * @return array{callable: array{object, string}, args: int} Resolved class method callback and argument count * * @throws CallbackResolutionFailedException When the class cannot be built */ @@ -356,7 +363,7 @@ protected function resolveInstance(string $className): object * Detect the number of arguments a callable accepts using reflection. * Results are cached for improved performance on repeated calls. * - * @param callable|array $callback The callable function/method + * @param callable|array $callback The callable function/method * @return int Number of accepted arguments * * @throws CallbackAnalysisFailedException When reflection cannot read the callable @@ -385,7 +392,7 @@ protected function detectArguments(callable|array $callback): int * Add a single hook event. * * @param string $hook The hook name - * @param callable|string|array $callback The callback (may not be callable yet for deferred resolution) + * @param HookCallback $callback The callback (may not be callable yet for deferred resolution) * @param int $priority The priority of the hook * @param int $acceptedArgs The number of arguments accepted by the callback */ @@ -411,11 +418,11 @@ protected function addHookEvent(string $hook, callable|string|array $callback, i * * @param string $hook The hook name * @param int $priority The priority of the registration - * @param callable|string|array $current The callback tracked today - * @param callable|string|array $replacement The callback to track instead + * @param HookCallback $current The callback tracked today + * @param HookCallback $replacement The callback to track instead * @param int $acceptedArgs The argument count of the replacement - * @param callable|string|array|null $handler The handler the replacement stands for, so remove() - * and exists() still find it by its own callback; null for none + * @param HookCallback|null $handler The handler the replacement stands for, so remove() + * and exists() still find it by its own callback; null for none */ protected function replaceTrackedCallback(string $hook, int $priority, callable|string|array $current, callable|string|array $replacement, int $acceptedArgs, callable|string|array|null $handler): void { @@ -443,7 +450,7 @@ protected function replaceTrackedCallback(string $hook, int $priority, callable| * (e.g., WordPress remove_action/remove_filter). * * @param string $hook The hook name - * @param callable|string|array $callback The callback to remove + * @param HookCallback $callback The callback to remove * @param int $priority The priority of the hook */ protected function removeHookEvent(string $hook, callable|string|array $callback, int $priority): void @@ -454,7 +461,7 @@ protected function removeHookEvent(string $hook, callable|string|array $callback /** * Read the number of parameters of a callable with reflection. * - * @param callable|array $callback The callable function/method + * @param callable|array $callback The callable function/method * * @throws CallbackAnalysisFailedException When reflection cannot read the callable */ @@ -478,8 +485,8 @@ private function countParameters(callable|array $callback): int * Improved callback comparison for the remove method * that properly handles class-based callbacks. * - * @param callable|string|array $registeredCallback The registered callback. - * @param callable|string|array $requestedCallback The callback requested for removal. + * @param HookCallback $registeredCallback The registered callback. + * @param HookCallback $requestedCallback The callback requested for removal. * @return bool True if callbacks match, false otherwise. */ private function compareCallbacks(callable|string|array $registeredCallback, callable|string|array $requestedCallback): bool @@ -536,6 +543,8 @@ private function compareCallbacks(callable|string|array $registeredCallback, cal * * Static methods and classes not loaded yet are left out: the former are * callable as they are, the latter keep their deferred resolution. + * + * @param HookCallback $callback The callback to test */ private function isInstanceMethodReference(callable|string|array $callback): bool { @@ -561,8 +570,9 @@ private function isInstanceMethodReference(callable|string|array $callback): boo * is returned as is. * * @param string $hook The hook name - * @param callable|string|array $callback The requested callback + * @param HookCallback $callback The requested callback * @param int|null $priority Optional. Priority the registered callback must have + * @return HookCallback */ private function registeredCallbackFor(string $hook, callable|string|array $callback, ?int $priority = null): callable|string|array { @@ -600,7 +610,7 @@ private function studly(string $value): string /** * Generate a unique cache key for a callback. * - * @param callable|array $callback The callback to generate a key for + * @param callable|array $callback The callback to generate a key for * @return string A unique identifier for the callback */ private function getCacheKeyForCallback(callable|array $callback): string diff --git a/src/Filter.php b/src/Filter.php index 12d3e14..2b3110f 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -19,6 +19,9 @@ * Filter::remove('the_content', $callback); * * @see WordPressFilter The underlying WordPress adapter. + * + * @phpstan-import-type HookCallback from \Pollora\Hook\Domain\Contract\HookInterface + * @phpstan-import-type HookRegistration from \Pollora\Hook\Domain\Contract\HookInterface */ class Filter { @@ -27,8 +30,8 @@ class Filter /** * Add a filter hook. * - * @param string|array $hooks Hook name(s). - * @param callable|string|array $callback Callback. + * @param string|list $hooks Hook name(s). + * @param HookCallback $callback Callback. * @param int $priority Priority (default 10). * @param int|null $acceptedArgs Argument count (auto-detected if null). * @return WordPressFilter The adapter, to chain calls @@ -58,7 +61,7 @@ public static function apply(string $hook, mixed $value, mixed ...$args): mixed * Remove a filter hook. * * @param string $hook Hook name. - * @param callable|string|array|null $callback Callback to remove. + * @param HookCallback|null $callback Callback to remove. * @param int $priority Priority (default 10). */ public static function remove(string $hook, callable|string|array|null $callback = null, int $priority = 10): bool @@ -80,7 +83,7 @@ public static function exists(string $hook): bool * Get registered callbacks for a filter hook. * * @param string $hook Hook name. - * @return array|null Callbacks or null. + * @return list|null Callbacks or null. */ public static function callbacks(string $hook): ?array { diff --git a/tests/Stubs/wordpress.php b/tests/Stubs/wordpress.php index f1bd207..d8e462e 100644 --- a/tests/Stubs/wordpress.php +++ b/tests/Stubs/wordpress.php @@ -274,6 +274,9 @@ function delete_option(string $option): bool } if (! function_exists('wp_schedule_single_event')) { + /** + * @param array $args + */ function wp_schedule_single_event(int $timestamp, string $hook, array $args = [], bool $wpError = false): bool|object { if (in_array('wp_schedule_single_event', $GLOBALS['wp_fail'] ?? [], true)) { @@ -298,6 +301,9 @@ public function get_error_message(): string } if (! function_exists('wp_schedule_event')) { + /** + * @param array $args + */ function wp_schedule_event(int $timestamp, string $recurrence, string $hook, array $args = []): bool { $GLOBALS['wp_cron_events'][] = ['timestamp' => $timestamp, 'hook' => $hook, 'args' => $args, 'recurrence' => $recurrence]; @@ -307,6 +313,9 @@ function wp_schedule_event(int $timestamp, string $recurrence, string $hook, arr } if (! function_exists('wp_next_scheduled')) { + /** + * @param array $args + */ function wp_next_scheduled(string $hook, array $args = []): int|false { foreach ($GLOBALS['wp_cron_events'] ?? [] as $event) { @@ -339,6 +348,9 @@ public function esc_like(string $text): string return addcslashes($text, '_%\\'); } + /** + * @return array{query: string, args: list} + */ public function prepare(string $query, mixed ...$args): array { $this->queries[] = ['query' => $query, 'args' => $args]; @@ -346,6 +358,10 @@ public function prepare(string $query, mixed ...$args): array return ['query' => $query, 'args' => $args]; } + /** + * @param array{query: string, args: list} $prepared + * @return list + */ public function get_results(array $prepared, string $output): array { $this->outputs[] = $output; @@ -363,6 +379,9 @@ public function get_results(array $prepared, string $output): array /** * Answers the expired-lock query: prefix, time, limit. + * + * @param array{query: string, args: list} $prepared + * @return list */ public function get_col(array $prepared): array { @@ -406,6 +425,9 @@ public static function is_initialized(?string $functionName = null): bool } if (! function_exists('as_enqueue_async_action')) { + /** + * @param array $args + */ function as_enqueue_async_action(string $hook, array $args = [], string $group = '', bool $unique = false, int $priority = 10): int { return as_schedule_single_action(time(), $hook, $args, $group); @@ -413,6 +435,9 @@ function as_enqueue_async_action(string $hook, array $args = [], string $group = } if (! function_exists('as_schedule_single_action')) { + /** + * @param array $args + */ function as_schedule_single_action(int $timestamp, string $hook, array $args = [], string $group = '', bool $unique = false, int $priority = 10): int { if ($GLOBALS['as_fail'] ?? false) { @@ -426,6 +451,9 @@ function as_schedule_single_action(int $timestamp, string $hook, array $args = [ } if (! function_exists('as_schedule_recurring_action')) { + /** + * @param array $args + */ function as_schedule_recurring_action(int $timestamp, int $interval, string $hook, array $args = [], string $group = '', bool $unique = false, int $priority = 10): int { return as_schedule_single_action($timestamp, $hook, $args, $group); @@ -433,6 +461,9 @@ function as_schedule_recurring_action(int $timestamp, int $interval, string $hoo } if (! function_exists('as_has_scheduled_action')) { + /** + * @param array|null $args + */ function as_has_scheduled_action(string $hook, ?array $args = null, string $group = ''): bool { foreach ($GLOBALS['as_actions'] ?? [] as $action) { diff --git a/tests/Unit/Async/ArgumentNormalizerEdgeTest.php b/tests/Unit/Async/ArgumentNormalizerEdgeTest.php index 57dd7b0..39448c9 100644 --- a/tests/Unit/Async/ArgumentNormalizerEdgeTest.php +++ b/tests/Unit/Async/ArgumentNormalizerEdgeTest.php @@ -119,8 +119,10 @@ public function resolve(array $reference): ?object it('rebuilds an immutable date for a class that is not a date', function (string $class): void { $encoded = [['@type' => 'date', 'class' => $class, 'value' => '2026-10-07T14:30:15.000000+00:00', 'timezone' => 'UTC']]; - expect($this->normalizer->denormalize($encoded)[0])->toBeInstanceOf(DateTimeImmutable::class) - ->and($this->normalizer->denormalize($encoded)[0]::class)->toBe(DateTimeImmutable::class); + $denormalized = $this->normalizer->denormalize($encoded)[0]; + + expect($denormalized)->toBeInstanceOf(DateTimeImmutable::class) + ->and(get_debug_type($denormalized))->toBe(DateTimeImmutable::class); })->with(['stdClass', 'Missing\\DateClass', 'ArrayObject']); it('reads a class that is not a string as no date class', function (): void { diff --git a/tests/Unit/Async/ArgumentNormalizerTest.php b/tests/Unit/Async/ArgumentNormalizerTest.php index 9fe5824..9563884 100644 --- a/tests/Unit/Async/ArgumentNormalizerTest.php +++ b/tests/Unit/Async/ArgumentNormalizerTest.php @@ -79,6 +79,9 @@ it('carries a JsonSerializable object as its array', function (): void { $money = new class implements JsonSerializable { + /** + * @return array{amount: int, currency: string} + */ public function jsonSerialize(): array { return ['amount' => 1999, 'currency' => 'EUR']; diff --git a/tests/Unit/Async/AsyncComponentsEdgeTest.php b/tests/Unit/Async/AsyncComponentsEdgeTest.php index 308e86f..ac9857d 100644 --- a/tests/Unit/Async/AsyncComponentsEdgeTest.php +++ b/tests/Unit/Async/AsyncComponentsEdgeTest.php @@ -44,6 +44,9 @@ function edgeQueued(PendingAsync $pending, string $hook, string $descriptor = 'p return $queued; } +/** + * @param list $arguments + */ function edgeAsyncPayload(string $handler, array $arguments = [1]): AsyncPayload { return new AsyncPayload( @@ -285,6 +288,9 @@ function edgeAsyncPayload(string $handler, array $arguments = [1]): AsyncPayload it('hands the hook arguments of a callable it cannot reflect as a list', function (): void { $callable = new class { + /** + * @param list $arguments + */ public function __call(string $name, array $arguments): void {} }; diff --git a/tests/Unit/Async/AsyncPayloadEdgeTest.php b/tests/Unit/Async/AsyncPayloadEdgeTest.php index 0a63f20..0d209d3 100644 --- a/tests/Unit/Async/AsyncPayloadEdgeTest.php +++ b/tests/Unit/Async/AsyncPayloadEdgeTest.php @@ -5,6 +5,9 @@ use Pollora\Hook\Async\AsyncPayload; use Pollora\Hook\Async\Exceptions\InvalidPayload; +/** + * @param array $overrides + */ function edgePayload(array $overrides = []): AsyncPayload { return new AsyncPayload(...array_merge([ diff --git a/tests/Unit/Async/AsyncPayloadTest.php b/tests/Unit/Async/AsyncPayloadTest.php index 9d3e350..3db08c7 100644 --- a/tests/Unit/Async/AsyncPayloadTest.php +++ b/tests/Unit/Async/AsyncPayloadTest.php @@ -5,6 +5,9 @@ use Pollora\Hook\Async\AsyncPayload; use Pollora\Hook\Async\Exceptions\InvalidPayload; +/** + * @param array $overrides + */ function makeAsyncPayload(array $overrides = []): AsyncPayload { return new AsyncPayload(...array_merge([ diff --git a/tests/Unit/Async/RetryAndUserTest.php b/tests/Unit/Async/RetryAndUserTest.php index ab04948..f52b02f 100644 --- a/tests/Unit/Async/RetryAndUserTest.php +++ b/tests/Unit/Async/RetryAndUserTest.php @@ -35,6 +35,9 @@ Async::flush(); }); +/** + * @return list> + */ function failedAnnouncements(): array { return array_values(array_filter($GLOBALS['wp_actions_done'], fn (array $call): bool => $call['hook'] === 'pollora/async/failed')); diff --git a/tests/Unit/Async/RunnerAndDispatcherEdgeTest.php b/tests/Unit/Async/RunnerAndDispatcherEdgeTest.php index e08e96b..824c3c9 100644 --- a/tests/Unit/Async/RunnerAndDispatcherEdgeTest.php +++ b/tests/Unit/Async/RunnerAndDispatcherEdgeTest.php @@ -67,6 +67,10 @@ function pollora_edge_runner_nested(int $depth): void Async::flush(); }); +/** + * @param list $arguments + * @param array $overrides + */ function edgeRunnerPayload(string $handler, array $arguments = [1], array $overrides = []): AsyncPayload { return new AsyncPayload(...array_merge([ diff --git a/tests/Unit/Async/StorageAndRecoveryTest.php b/tests/Unit/Async/StorageAndRecoveryTest.php index 9ee979b..8e3ffb7 100644 --- a/tests/Unit/Async/StorageAndRecoveryTest.php +++ b/tests/Unit/Async/StorageAndRecoveryTest.php @@ -35,6 +35,9 @@ unset($GLOBALS['wpdb']); }); +/** + * @param array $overrides + */ function storagePayload(array $overrides = []): AsyncPayload { return new AsyncPayload(...array_merge([ @@ -71,16 +74,25 @@ public function esc_like(string $text): string return $text; } + /** + * @return array{query: string, args: list} + */ public function prepare(string $query, mixed ...$args): array { return ['query' => $query, 'args' => $args]; } + /** + * @param array{query: string, args: list} $prepared + */ public function get_results(array $prepared, string $output): mixed { return $this->rows; } + /** + * @param array{query: string, args: list} $prepared + */ public function get_col(array $prepared): mixed { return $this->names; @@ -88,6 +100,9 @@ public function get_col(array $prepared): mixed }; } +/** + * @return list> + */ function executionEventsOf(): array { return array_values(array_filter($GLOBALS['wp_cron_events'], fn (array $event): bool => $event['hook'] === Async::HOOK)); @@ -391,11 +406,13 @@ function executionEventsOf(): array } catch (AsyncException $caught) { } + $previous = $caught?->getPrevious(); + expect($caught)->toBeInstanceOf(AsyncException::class) - ->and($caught->getMessage())->toBe("WP-Cron: the payload of 'save_post' could not be stored.") - ->and($caught->getCode())->toBe(0) - ->and($caught->getPrevious())->toBeInstanceOf(AsyncException::class) - ->and($caught->getPrevious()->getMessage())->toBe("The payload of 'save_post' could not be stored."); + ->and($caught?->getMessage())->toBe("WP-Cron: the payload of 'save_post' could not be stored.") + ->and($caught?->getCode())->toBe(0) + ->and($previous)->toBeInstanceOf(AsyncException::class) + ->and($previous?->getMessage())->toBe("The payload of 'save_post' could not be stored."); }); it('never schedules an execution in the past', function (): void { diff --git a/tests/Unit/Async/UniqueTest.php b/tests/Unit/Async/UniqueTest.php index 2498d2d..885fa57 100644 --- a/tests/Unit/Async/UniqueTest.php +++ b/tests/Unit/Async/UniqueTest.php @@ -40,6 +40,9 @@ Async::flush(); }); +/** + * @return array + */ function uniqueLocks(): array { return array_filter($GLOBALS['wp_options'], fn (string $name): bool => str_starts_with($name, UniqueLock::OPTION_PREFIX), ARRAY_FILTER_USE_KEY); diff --git a/tests/Unit/Domain/Exceptions/HookExceptionsTest.php b/tests/Unit/Domain/Exceptions/HookExceptionsTest.php index 35c586a..58c6a71 100644 --- a/tests/Unit/Domain/Exceptions/HookExceptionsTest.php +++ b/tests/Unit/Domain/Exceptions/HookExceptionsTest.php @@ -22,6 +22,9 @@ public function apply(string $hook, mixed $value, mixed ...$args): mixed return $value; } + /** + * @param callable|array $callback + */ public function probeArguments(callable|array $callback): int { return $this->detectArguments($callback); diff --git a/tests/Unit/Domain/Service/AbstractHookTest.php b/tests/Unit/Domain/Service/AbstractHookTest.php index 00fc70e..14ace3b 100644 --- a/tests/Unit/Domain/Service/AbstractHookTest.php +++ b/tests/Unit/Domain/Service/AbstractHookTest.php @@ -19,6 +19,10 @@ public function apply(string $hook, mixed $value, mixed ...$args): mixed } // Expose protected methods for testing + /** + * @param callable|string|array $callback + * @return array{callable: callable|string|array, args: int} + */ public function testResolveCallback(string $hook, callable|string|array $callback, ?int $acceptedArgs = null): array { return $this->resolveCallback($hook, $callback, $acceptedArgs); @@ -138,6 +142,9 @@ public function myHook(): void {} // Use a testable subclass that tracks removeHookEvent calls $hook = new class extends AbstractHook { + /** + * @var list, priority: int}> + */ public array $removedEvents = []; public function do(string $hook, mixed ...$args): self @@ -150,6 +157,9 @@ public function apply(string $hook, mixed $value, mixed ...$args): mixed return $value; } + /** + * @param callable|string|array $callback + */ protected function removeHookEvent(string $hook, callable|string|array $callback, int $priority): void { $this->removedEvents[] = ['hook' => $hook, 'callback' => $callback, 'priority' => $priority]; @@ -168,6 +178,9 @@ protected function removeHookEvent(string $hook, callable|string|array $callback it('also cleans internal tracking when hook was registered through Pollora', function (): void { $hook = new class extends AbstractHook { + /** + * @var list, priority: int}> + */ public array $removedEvents = []; public function do(string $hook, mixed ...$args): self @@ -180,6 +193,9 @@ public function apply(string $hook, mixed $value, mixed ...$args): mixed return $value; } + /** + * @param callable|string|array $callback + */ protected function removeHookEvent(string $hook, callable|string|array $callback, int $priority): void { $this->removedEvents[] = ['hook' => $hook, 'callback' => $callback, 'priority' => $priority]; diff --git a/tests/Unit/Domain/Service/AbstractHookTrackingTest.php b/tests/Unit/Domain/Service/AbstractHookTrackingTest.php index 7333e90..4e5a3cd 100644 --- a/tests/Unit/Domain/Service/AbstractHookTrackingTest.php +++ b/tests/Unit/Domain/Service/AbstractHookTrackingTest.php @@ -22,16 +22,27 @@ public function apply(string $hook, mixed $value, mixed ...$args): mixed return $value; } + /** + * @param callable|array $callback + */ public function detect(callable|array $callback): int { return $this->detectArguments($callback); } + /** + * @param callable|string|array $current + * @param callable|string|array $replacement + * @param callable|string|array|null $handler + */ public function replace(string $hook, int $priority, callable|string|array $current, callable|string|array $replacement, int $args, callable|string|array|null $handler): void { $this->replaceTrackedCallback($hook, $priority, $current, $replacement, $args, $handler); } + /** + * @param callable|string|array $callback + */ protected function removeHookEvent(string $hook, callable|string|array $callback, int $priority): void { $this->removed[] = ['hook' => $hook, 'callback' => $callback, 'priority' => $priority]; @@ -67,7 +78,7 @@ function declareHookClass(string $body, string $extends = ''): string $hook->remove('init', 'first_fn'); - $callbacks = $hook->callbacks('init'); + $callbacks = $hook->callbacks('init') ?? []; expect(array_keys($callbacks))->toBe([0, 1]) ->and($callbacks[0]['callback'])->toBe('second_fn') ->and($callbacks[1]['callback'])->toBe('third_fn'); diff --git a/tests/WordPress/ActionSchedulerTest.php b/tests/WordPress/ActionSchedulerTest.php index a4ce9e3..f49fde3 100644 --- a/tests/WordPress/ActionSchedulerTest.php +++ b/tests/WordPress/ActionSchedulerTest.php @@ -65,6 +65,9 @@ function pendingActions(): array return $actions; } +/** + * @return list> + */ function fixtureRunsOf(string $key): array { return array_values(array_filter(get_option(PolloraHookFixture::RUNS, []), fn (array $run): bool => $run['key'] === $key));