Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .harness.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 4 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions src/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<string> $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()
Expand Down Expand Up @@ -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
Expand All @@ -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<HookRegistration>|null Callbacks or null.
*/
public static function callbacks(string $hook): ?array
{
Expand Down
8 changes: 8 additions & 0 deletions src/Adapter/Out/WordPress/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand All @@ -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
{
Expand Down
2 changes: 2 additions & 0 deletions src/Adapter/Out/WordPress/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
3 changes: 3 additions & 0 deletions src/Async/AsyncRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<int|string, mixed> $arguments
*/
public function call(callable|string|array $callback, array $arguments, AsyncContext $context): void
Expand Down
5 changes: 4 additions & 1 deletion src/Async/CallableDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/Async/HandlerArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/Async/QueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
* 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
{
/**
* @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
Expand Down
11 changes: 7 additions & 4 deletions src/Domain/Contract/HookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, mixed>
* @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<string> $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).
*/
Expand All @@ -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;
Expand All @@ -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<HookRegistration>|null Callbacks array or null if hook doesn't exist.
*/
public function callbacks(string $hook): ?array;
}
Loading
Loading