diff --git a/phpstan.neon b/phpstan.neon index 856463e7d..3e00ca15b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,31 +7,7 @@ parameters: level: 5 paths: - - src/mantle/application - - src/mantle/assets - - src/mantle/auth - - src/mantle/cache - - src/mantle/config - - src/mantle/console - - src/mantle/container - - src/mantle/contracts - - src/mantle/database - - src/mantle/events - - src/mantle/facade - - src/mantle/faker - - src/mantle/filesystem - - src/mantle/framework - # - src/mantle/http - - src/mantle/http-client - - src/mantle/log - - src/mantle/new-relic - # - src/mantle/queue - - src/mantle/rest-api - # - src/mantle/scheduling - - src/mantle/support - - src/mantle/testing - - src/mantle/testkit - - src/mantle/view + - src/mantle - mantle.php ignoreErrors: diff --git a/src/mantle/contracts/http/routing/interface-response-factory.php b/src/mantle/contracts/http/routing/interface-response-factory.php index 073c08355..c246c1fb3 100644 --- a/src/mantle/contracts/http/routing/interface-response-factory.php +++ b/src/mantle/contracts/http/routing/interface-response-factory.php @@ -53,10 +53,9 @@ public function view( string $slug, $name = null, $data = [], $status = 200, arr * @param mixed $data * @param int $status * @param array $headers - * @param int $options * @return JsonResponse */ - public function json( $data = [], $status = 200, array $headers = [], $options = 0 ); + public function json( $data = [], $status = 200, array $headers = [] ); /** * Create a new JSONP response instance. @@ -65,10 +64,9 @@ public function json( $data = [], $status = 200, array $headers = [], $options = * @param mixed $data * @param int $status * @param array $headers - * @param int $options * @return JsonResponse */ - public function jsonp( $callback, $data = [], $status = 200, array $headers = [], $options = 0 ); + public function jsonp( $callback, $data = [], $status = 200, array $headers = [] ); /** * Create a new streamed response instance. diff --git a/src/mantle/contracts/http/view/interface-factory.php b/src/mantle/contracts/http/view/interface-factory.php index a25e2fd93..89b7b97b3 100644 --- a/src/mantle/contracts/http/view/interface-factory.php +++ b/src/mantle/contracts/http/view/interface-factory.php @@ -86,6 +86,14 @@ public function make( string $slug, $name = null, array $variables = [] ): View; */ public function get_var( string $key, $default = null ); + /** + * Push a view onto the stack and set it as the current view. + * + * @param View $view View being loaded. + * @return static + */ + public function push( View $view ); + /** * Pop a partial off the top of the stack and set the current partial to the * next one down. diff --git a/src/mantle/http/autoload.php b/src/mantle/http/autoload.php index 6eef4ad1f..8ce37e419 100644 --- a/src/mantle/http/autoload.php +++ b/src/mantle/http/autoload.php @@ -14,6 +14,7 @@ use Mantle\Contracts\Http\Routing\Response_Factory; use Mantle\Contracts\Http\View\Factory as View_Factory; +use Mantle\Http\View\View; use Symfony\Component\Routing\Generator\UrlGenerator; if ( ! function_exists( 'response' ) ) { @@ -103,7 +104,7 @@ function view( ...$args ) { * @param string $slug View slug. * @param array|string $name View name, optional. Supports passing variables in if * $variables is not used. - * @return View|View_Factory + * @return void */ function render_view( ...$args ) { echo view( ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped @@ -134,8 +135,7 @@ function render_main_template() { function loop( ...$args ) { return view() ->loop( ...$args ) - ->map - ->render() + ->map( fn ( View $item ) => $item->render() ) ->implode( '' ); } } @@ -148,9 +148,9 @@ function loop( ...$args ) { * @param string $slug View slug. * @param array|string $name View name, optional. Supports passing variables in if * $variables is not used. - * @return string + * @return void */ - function render_loop( ...$args ) { + function render_loop( ...$args ): void { echo loop( ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } @@ -167,9 +167,9 @@ function render_loop( ...$args ) { * @return string */ function iterate( ...$args ) { - return view()->iterate( ...$args ) - ->map - ->render() + return view() + ->iterate( ...$args ) + ->map( fn ( View $item ) => $item->render() ) ->implode( '' ); } } @@ -182,9 +182,9 @@ function iterate( ...$args ) { * @param string $slug View slug. * @param array|string $name View name, optional. Supports passing variables in if * $variables is not used. - * @return string + * @return void */ - function render_iterate( ...$args ) { + function render_iterate( ...$args ): void { echo iterate( ...$args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } } diff --git a/src/mantle/http/class-request.php b/src/mantle/http/class-request.php index 575eb0a01..5e27bc167 100644 --- a/src/mantle/http/class-request.php +++ b/src/mantle/http/class-request.php @@ -28,21 +28,28 @@ class Request extends SymfonyRequest implements ArrayAccess, Arrayable { /** * Route parameters. * - * @var ParameterBag + * @var ParameterBag|null */ - protected $route_parameters = []; + protected ?ParameterBag $route_parameters = null; + + /** + * The decoded JSON content for the request. + * + * @var ParameterBag|null + */ + protected ?ParameterBag $json = null; /** * Route matched. * - * @var Route + * @var Route|null */ protected $route; /** * All of the converted files for the request. * - * @var array + * @var array|null */ protected $converted_files; @@ -382,7 +389,7 @@ public function to_array() { * Set route parameters. * * @param ParameterBag|array $parameters Route parameters to set. - * @return array + * @return static */ public function set_route_parameters( $parameters ) { if ( ! ( $parameters instanceof ParameterBag ) ) { @@ -390,24 +397,23 @@ public function set_route_parameters( $parameters ) { $parameters = new ParameterBag( array_filter( $parameters, - function ( $parameter ) { - return 0 !== strpos( $parameter, '_' ); - }, + fn ( $parameter ) => 0 !== strpos( $parameter, '_' ), ARRAY_FILTER_USE_KEY ) ); } $this->route_parameters = $parameters; + return $this; } /** * Get route parameters. * - * @return ParameterBag + * @return ParameterBag|null */ - public function get_route_parameters(): ParameterBag { + public function get_route_parameters(): ?ParameterBag { return $this->route_parameters; } @@ -429,7 +435,7 @@ public function set_route_parameter( string $key, $value ) { * @return Route */ public function get_route(): ?Route { - return $this->route ?? null; + return isset( $this->route ) ? $this->route : null; } /** @@ -440,6 +446,7 @@ public function get_route(): ?Route { */ public function set_route( Route $route ) { $this->route = $route; + return $this; } diff --git a/src/mantle/http/class-response.php b/src/mantle/http/class-response.php index c5dc54642..50fa9d932 100644 --- a/src/mantle/http/class-response.php +++ b/src/mantle/http/class-response.php @@ -95,7 +95,7 @@ protected function should_be_json( $content ): bool { * Morph the given content into JSON. * * @param Arrayable|Jsonable|ArrayObject|JsonSerializable|array|mixed $content - * @return string + * @return string|false */ protected function morph_to_json( $content ) { if ( $content instanceof Jsonable ) { diff --git a/src/mantle/http/concerns/trait-interacts-with-content-types.php b/src/mantle/http/concerns/trait-interacts-with-content-types.php index c17759a56..cf7feda66 100644 --- a/src/mantle/http/concerns/trait-interacts-with-content-types.php +++ b/src/mantle/http/concerns/trait-interacts-with-content-types.php @@ -121,6 +121,8 @@ public function prefers( $content_types ) { } } } + + return null; } /** diff --git a/src/mantle/http/routing/class-entity-router.php b/src/mantle/http/routing/class-entity-router.php index 8957dee0a..a62b5572d 100644 --- a/src/mantle/http/routing/class-entity-router.php +++ b/src/mantle/http/routing/class-entity-router.php @@ -178,12 +178,12 @@ protected function handle_bindings_substituted( Bindings_Substituted $event ): v /** * Resolve the endpoints to add for an entity. * - * @param Router $router Router instance. - * @param string $entity Entity class name. - * @param string $controller Controller class name. + * @param Router_Contract $router Router instance. + * @param string $entity Entity class name. + * @param string $controller Controller class name. * @return void */ - protected static function resolve_entity_endpoints( Router $router, string $entity, string $controller ): void { + protected static function resolve_entity_endpoints( Router_Contract $router, string $entity, string $controller ): void { // Singular endpoint. $single_route = $entity::get_route(); diff --git a/src/mantle/http/routing/class-implicit-route-binding.php b/src/mantle/http/routing/class-implicit-route-binding.php index 4c4190e59..72ca76359 100644 --- a/src/mantle/http/routing/class-implicit-route-binding.php +++ b/src/mantle/http/routing/class-implicit-route-binding.php @@ -69,8 +69,11 @@ protected static function get_parameter_name( $name, array $parameters ) { } $snaked_name = Str::snake( $name ); + if ( array_key_exists( $snaked_name, $parameters ) ) { return $snaked_name; } + + return null; } } diff --git a/src/mantle/http/routing/class-response-factory.php b/src/mantle/http/routing/class-response-factory.php index 4b678c4be..1db938cdd 100644 --- a/src/mantle/http/routing/class-response-factory.php +++ b/src/mantle/http/routing/class-response-factory.php @@ -92,11 +92,10 @@ public function view( string $slug, $name = null, $data = [], $status = 200, arr * @param mixed $data * @param int $status * @param array $headers - * @param int $options * @return JsonResponse */ - public function json( $data = [], $status = 200, array $headers = [], $options = 0 ) { - return new JsonResponse( $data, $status, $headers, $options ); + public function json( $data = [], $status = 200, array $headers = [] ) { + return new JsonResponse( $data, $status, $headers, is_string( $data ) ); } /** @@ -106,11 +105,10 @@ public function json( $data = [], $status = 200, array $headers = [], $options = * @param mixed $data * @param int $status * @param array $headers - * @param int $options * @return JsonResponse */ - public function jsonp( $callback, $data = [], $status = 200, array $headers = [], $options = 0 ) { - return $this->json( $data, $status, $headers, $options )->setCallback( $callback ); + public function jsonp( $callback, $data = [], $status = 200, array $headers = [] ) { + return $this->json( $data, $status, $headers )->setCallback( $callback ); } /** diff --git a/src/mantle/http/routing/class-rest-route-registrar.php b/src/mantle/http/routing/class-rest-route-registrar.php index 520181660..43f624a99 100644 --- a/src/mantle/http/routing/class-rest-route-registrar.php +++ b/src/mantle/http/routing/class-rest-route-registrar.php @@ -118,7 +118,7 @@ protected function wrap_callback( callable $callback, string $route ): callable 'namespace' => $this->namespace, 'route' => $route, ], - $request + $request, ) ); @@ -127,9 +127,7 @@ protected function wrap_callback( callable $callback, string $route ): callable ->send( $request ) ->through( $this->gather_route_middleware( $middleware ) ) ->then( - function ( WP_REST_Request $request ) use ( $callback ) { - return $callback( $request ); - } + fn ( WP_REST_Request $request ) => $callback( $request ), ) ); }; diff --git a/src/mantle/http/routing/class-route-registrar.php b/src/mantle/http/routing/class-route-registrar.php index b23d46212..eee21e7aa 100644 --- a/src/mantle/http/routing/class-route-registrar.php +++ b/src/mantle/http/routing/class-route-registrar.php @@ -19,9 +19,9 @@ class Route_Registrar { /** * Router instance. * - * @var Router + * @var Router|null */ - protected $router; + protected ?Router $router; /** * The attributes to pass on to the router. @@ -107,25 +107,13 @@ public function group( $callback ) { $this->router->group( $this->attributes, $callback ); } - /** - * Register a new route with the given verbs. - * - * @param array|string $methods - * @param string $uri - * @param \Closure|array|string|null $action - * @return \Illuminate\Routing\Route - */ - public function match( $methods, $uri, $action = null ) { - return $this->router->match( $methods, $uri, $this->compile_action( $action ) ); - } - /** * Register a new route with the router. * * @param string $method * @param string $uri * @param \Closure|array|string|null $action - * @return \Illuminate\Routing\Route + * @return \Mantle\Http\Routing\Route */ protected function register_route( $method, $uri, $action = null ) { if ( ! is_array( $action ) ) { @@ -156,9 +144,9 @@ protected function compile_action( $action ) { /** * Pass the REST API method back to the REST API registrar. * - * @param string $namespace Route namespace. - * @param \Closure|string $route Route name or callback to register more routes. - * @param array $args Route arguments. + * @param string $namespace Route namespace. + * @param Closure|string $route Route name or callback to register more routes. + * @param array|Closure $args Route arguments. * @return Rest_Route_Registrar */ public function rest_api( string $namespace, $route, $args = [] ): Rest_Route_Registrar { @@ -180,7 +168,7 @@ public function rest_api( string $namespace, $route, $args = [] ): Rest_Route_Re * * @param string $method * @param array $parameters - * @return \Illuminate\Routing\Route|$this + * @return \Mantle\Http\Routing\Route|$this * * @throws BadMethodCallException Thrown on missing method. */ diff --git a/src/mantle/http/routing/class-route-signature-parameters.php b/src/mantle/http/routing/class-route-signature-parameters.php index 5919689cf..e07db670d 100644 --- a/src/mantle/http/routing/class-route-signature-parameters.php +++ b/src/mantle/http/routing/class-route-signature-parameters.php @@ -46,9 +46,7 @@ public static function from_action( array $action, $sub_class = null ) { return is_null( $sub_class ) ? $parameters : array_filter( $parameters, - function ( $p ) use ( $sub_class ) { - return Reflector::is_parameter_subclass_of( $p, $sub_class ); - } + fn ( $p ) => Reflector::is_parameter_subclass_of( $p, $sub_class ), ); } @@ -66,7 +64,7 @@ protected static function from_class_method_string( $uses ): array { $method = '__invoke'; } - if ( ! method_exists( $class, $method ) && is_callable( $class, $method ) ) { + if ( ! method_exists( $class, $method ) && is_callable( [ $class, $method ] ) ) { return []; } diff --git a/src/mantle/http/routing/class-route.php b/src/mantle/http/routing/class-route.php index ceb1ba906..f0e6981aa 100644 --- a/src/mantle/http/routing/class-route.php +++ b/src/mantle/http/routing/class-route.php @@ -10,7 +10,7 @@ use ArrayAccess; use ArrayObject; use JsonSerializable; -use Mantle\Container\Container; +use Mantle\Contracts\Container; use Mantle\Contracts\Http\Routing\Router; use Mantle\Contracts\Support\Arrayable; use Mantle\Database\Model\Model; @@ -48,14 +48,14 @@ class Route extends Symfony_Route { /** * Container instance. * - * @var \Mantle\Container\Container + * @var Container */ protected $container; /** * Router instance. * - * @var Router + * @var Router|null */ protected $router; @@ -83,9 +83,9 @@ public static function get_route_from_match( array $match ): ?Route { /** * Constructor. * - * @param array $methods HTTP methods the route responds to. - * @param string $path The path the route responds to. - * @param \Closure|array $action The route callback or array of actions. + * @param array $methods HTTP methods the route responds to. + * @param string $path The path the route responds to. + * @param \Closure|array|string $action The route callback or array of actions. */ public function __construct( array $methods, string $path, $action ) { parent::__construct( $path ); @@ -147,7 +147,7 @@ public function set_router( Router $router ) { * @return string */ public function get_name(): string { - if ( isset( $this->name ) ) { + if ( ! empty( $this->name ) ) { return (string) $this->name; } @@ -238,7 +238,7 @@ public function callback( $callback ) { * @todo Add route parameters from the request (pass :slug down to the route). * * @param Container $container Service Container. - * @return Response|null + * @return Symfony_Response|null */ public function run( Container $container ): ?Symfony_Response { $this->container = $container; @@ -382,14 +382,14 @@ protected function run_controller_callback() { $controller = $this->get_controller_name(); $method = $this->get_controller_method(); + $controller = $this->container->make( $controller ); + $parameters = $this->resolve_class_method_dependencies( $this->get_request_parameters(), $controller, $method ); - $controller = $this->container->make( $controller ); - if ( method_exists( $controller, 'call_action' ) ) { return $controller->call_action( $method, $parameters ); } diff --git a/src/mantle/http/routing/class-router.php b/src/mantle/http/routing/class-router.php index 5062b4e05..109b0cf1a 100644 --- a/src/mantle/http/routing/class-router.php +++ b/src/mantle/http/routing/class-router.php @@ -76,7 +76,7 @@ class Router implements Router_Contract { /** * REST Route Registrar * - * @var Rest_Route_Registrar + * @var Rest_Route_Registrar|null */ protected $rest_registrar; @@ -190,7 +190,7 @@ protected function load_routes( $routes ) { */ public function add_route( array $methods, string $uri, $action ) { // Send the route to the REST Registrar if set. - if ( $this->rest_registrar ) { + if ( isset( $this->rest_registrar ) ) { $args = [ 'callback' => $action, 'methods' => $methods, @@ -301,7 +301,7 @@ protected function execute_route_match( $match, Request $request ): ?Symfony_Res $route = Route::get_route_from_match( $match ); if ( ! $route ) { - throw new HttpException( 'Unknown route method: ' . \wp_json_encode( $match ) ); + throw new HttpException( 500, 'Unknown route method: ' . \wp_json_encode( $match ) ); } // Store the route match in the request object. @@ -492,11 +492,11 @@ public function substitute_implicit_bindings( Request $request ) { /** * Register a REST API route * - * @param string $namespace Namespace for the REST API route. - * @param \Closure|string $route Route to register or a callback function that - * will register child REST API routes. - * @param array $args Arguments for the route or callback for the route. - * Not used if $route is a callback. + * @param string $namespace Namespace for the REST API route. + * @param Closure|string $route Route to register or a callback function that + * will register child REST API routes. + * @param array|Closure $args Arguments for the route or callback for the route. + * Not used if $route is a callback. * @return Rest_Route_Registrar */ public function rest_api( string $namespace, $route, $args = [] ) { diff --git a/src/mantle/http/routing/events/class-route-matched.php b/src/mantle/http/routing/events/class-route-matched.php index bd070cfa7..7c5d085e2 100644 --- a/src/mantle/http/routing/events/class-route-matched.php +++ b/src/mantle/http/routing/events/class-route-matched.php @@ -9,34 +9,19 @@ use Mantle\Http\Request; use Mantle\Http\Routing\Route; +use WP_REST_Request; /** * Event for route matched event. */ class Route_Matched { - /** - * Route matched. - * - * @var Route|array - */ - public $route; - - /** - * Current request. - * - * @var \Mantle\Http\Request - */ - public $request; - /** * Constructor. * - * @param Route|array $route Route matched, Mantle route or an array of route - * information for REST API routes. - * @param Request $request Current request. + * @param Route|array $route Route matched, Mantle route or an array of route + * information for REST API routes. + * @param Request|WP_REST_Request $request Current request. */ - public function __construct( Route|array $route, $request ) { - $this->route = $route; - $this->request = $request; + public function __construct( public Route|array $route, public Request|WP_REST_Request $request ) { } } diff --git a/src/mantle/http/routing/middleware/class-setup-wordpress.php b/src/mantle/http/routing/middleware/class-setup-wordpress.php index d194278ec..3efb7f4a5 100644 --- a/src/mantle/http/routing/middleware/class-setup-wordpress.php +++ b/src/mantle/http/routing/middleware/class-setup-wordpress.php @@ -70,7 +70,7 @@ public function handle( Request $request, Closure $next ) { $provider = $this->app->get_provider( Query_Monitor_Service_Provider::class ); - if ( $provider ) { + if ( $provider instanceof Query_Monitor_Service_Provider ) { $qm_output = $provider->fire_query_monitor_dispatches(); if ( ! empty( $qm_output ) ) { diff --git a/src/mantle/http/routing/middleware/class-wrap-template.php b/src/mantle/http/routing/middleware/class-wrap-template.php index 7539d3368..a7f87047c 100644 --- a/src/mantle/http/routing/middleware/class-wrap-template.php +++ b/src/mantle/http/routing/middleware/class-wrap-template.php @@ -53,7 +53,7 @@ public function handle( Request $request, Closure $next ) { /** * Filter the template for wrapping the content. * - * @param string $template Template to use. + * @param string|null $template Template to use. */ $template = \apply_filters( 'mantle_wrap_template', null ); diff --git a/src/mantle/http/routing/trait-route-dependency-resolver.php b/src/mantle/http/routing/trait-route-dependency-resolver.php index b38a11f32..bade9341e 100644 --- a/src/mantle/http/routing/trait-route-dependency-resolver.php +++ b/src/mantle/http/routing/trait-route-dependency-resolver.php @@ -111,17 +111,15 @@ protected function transform_dependency( ReflectionParameter $parameter, $parame /** * Determine if an object of the given class is in a list of parameters. * - * @param string $class - * @param array $parameters + * @param class-string $class + * @param array $parameters * @return bool */ - protected function already_in_parameters( $class, array $parameters ) { + protected function already_in_parameters( string $class, array $parameters ) { return ! is_null( Arr::first( $parameters, - function ( $value ) use ( $class ) { - return $value instanceof $class; - } + fn ( $value ) => $value instanceof $class, ) ); } @@ -130,11 +128,11 @@ function ( $value ) use ( $class ) { * Splice the given value into the parameter list. * * @param array $parameters - * @param string $offset + * @param int $offset * @param mixed $value * @return void */ - protected function splice_into_parameters( array &$parameters, $offset, $value ) { + protected function splice_into_parameters( array &$parameters, int $offset, mixed $value ) { array_splice( $parameters, $offset, diff --git a/src/mantle/http/trait-interacts-with-input.php b/src/mantle/http/trait-interacts-with-input.php index ac75a509b..4cf898afd 100644 --- a/src/mantle/http/trait-interacts-with-input.php +++ b/src/mantle/http/trait-interacts-with-input.php @@ -49,7 +49,7 @@ public function has_header( $key ) { * @param string|array|null $default * @return string|array|null */ - public function header( $key = null, $default = null ) { + public function header( $key = null, $default = null ): string|array|null { return $this->retrieve_item( 'headers', $key, $default ); } @@ -58,12 +58,18 @@ public function header( $key = null, $default = null ) { * * @return string|null */ - public function bearer_token() { + public function bearer_token(): ?string { $header = $this->header( 'Authorization', '' ); + if ( is_array( $header ) ) { + $header = Arr::first( $header ); + } + if ( Str::starts_with( $header, 'Bearer ' ) ) { return Str::substr( $header, 7 ); } + + return $header; } /** @@ -72,7 +78,7 @@ public function bearer_token() { * @param string|array $key * @return bool */ - public function exists( $key ) { + public function exists( $key ): bool { return $this->has( $key ); } @@ -321,7 +327,7 @@ public function cookie( $key = null, $default = null ) { * @return string|array|null */ protected function retrieve_item( $source, $key, $default ) { - if ( is_null( $key ) ) { + if ( empty( $key ) ) { return $this->$source->all(); } @@ -336,7 +342,10 @@ protected function retrieve_item( $source, $key, $default ) { public function all_files() { $files = $this->files->all(); - $this->converted_files = $this->converted_files ?? $this->convert_uploaded_files( $files ); + if ( ! isset( $this->converted_files ) ) { + $this->converted_files = $this->convert_uploaded_files( $files ); + } + return $this->converted_files; } @@ -397,7 +406,7 @@ protected function is_valid_file( $file ) { * * @param string|null $key * @param mixed $default - * @return \Illuminate\Http\Uploaded_File|\Illuminate\Http\Uploaded_File[]|array|null + * @return \Mantle\Http\Uploaded_File|\Mantle\Http\Uploaded_File[]|null */ public function file( $key = null, $default = null ) { return data_get( $this->all_files(), $key, $default ); diff --git a/src/mantle/http/view/class-factory.php b/src/mantle/http/view/class-factory.php index 2b20dc65b..7bbc2dd56 100644 --- a/src/mantle/http/view/class-factory.php +++ b/src/mantle/http/view/class-factory.php @@ -66,9 +66,9 @@ class Factory implements ViewFactory { /** * Current view being rendered. * - * @var View + * @var View|null */ - protected $current; + protected $current = null; /** * The extension to engine bindings. @@ -171,6 +171,7 @@ public function get_shared(): array { public function push( View $view ) { $this->stack[] = $view; $this->current = $view; + return $this; } @@ -182,6 +183,7 @@ public function push( View $view ) { */ public function pop() { array_pop( $this->stack ); + $this->current = end( $this->stack ); if ( ! $this->current ) { @@ -252,7 +254,7 @@ protected function resolve_view_path( string $slug, string $name = null ): ?stri * @throws InvalidArgumentException Thrown if child view not found. */ protected function resolve_child_view_path_from_parent( string $slug ) { - $path = Str::before( $this->current->get_path(), '.' ) . '-' . Str::substr( $slug, '1' ); + $path = Str::before( $this->current->get_path(), '.' ) . '-' . Str::substr( $slug, 1 ); foreach ( $this->finder->get_possible_view_files( $path ) as $file ) { if ( file_exists( $file ) ) { diff --git a/src/mantle/http/view/class-view-finder.php b/src/mantle/http/view/class-view-finder.php index 892b31777..44cedf346 100644 --- a/src/mantle/http/view/class-view-finder.php +++ b/src/mantle/http/view/class-view-finder.php @@ -77,7 +77,7 @@ public function add_extension( $extension ) { /** * Get registered extensions. * - * @return string + * @return string[] */ public function get_extensions(): array { return $this->extensions; diff --git a/src/mantle/http/view/class-view.php b/src/mantle/http/view/class-view.php index d2cffef1f..6a016bd2f 100644 --- a/src/mantle/http/view/class-view.php +++ b/src/mantle/http/view/class-view.php @@ -8,6 +8,7 @@ namespace Mantle\Http\View; +use Illuminate\View\Engines\CompilerEngine; use Mantle\Contracts\Http\View\Factory as Factory_Contract; use Mantle\Contracts\View\Engine; use Mantle\Database\Model\Post; @@ -17,38 +18,10 @@ * View Class */ class View { - /** - * View Factory - * - * @var Factory - */ - protected $factory; - - /** - * View Engine - * - * @var Engine - */ - protected $engine; - - /** - * View path. - * - * @var string - */ - protected $path; - - /** - * Array of view data. - * - * @var array - */ - protected $data; - /** * Post object to set for the post. * - * @var Post|\WP_Post|int + * @var Post|\WP_Post|int|null */ protected $post; @@ -69,7 +42,7 @@ class View { /** * Cache TTL for the view. * - * @var int + * @var int|null */ protected $cache_ttl; @@ -79,13 +52,14 @@ class View { * @param Factory_Contract $factory View Factory. * @param Engine|\Illuminate\View\Engines\CompilerEngine $engine View Engine. * @param string $path View path. - * @param array $variables Variables for the view, optional. + * @param array $data Variables for the view, optional. */ - public function __construct( Factory_Contract $factory, $engine, string $path, array $variables = [] ) { - $this->factory = $factory; - $this->engine = $engine; - $this->path = $path; - $this->data = $variables; + public function __construct( + protected Factory_Contract $factory, + protected Engine|CompilerEngine $engine, + protected string $path, + protected array $data = [], + ) { } /** diff --git a/src/mantle/queue/class-pending-closure-dispatch.php b/src/mantle/queue/class-pending-closure-dispatch.php index f145d5571..a9fba25ac 100644 --- a/src/mantle/queue/class-pending-closure-dispatch.php +++ b/src/mantle/queue/class-pending-closure-dispatch.php @@ -15,13 +15,6 @@ * Used to wrap a pending Closure job that will be dispatched to the queue. */ class Pending_Closure_Dispatch extends Pending_Dispatch { - /** - * Job instance. - * - * @var Closure_Job - */ - protected $job; - /** * Add a callback to be executed on failure. * diff --git a/src/mantle/queue/class-pending-dispatch.php b/src/mantle/queue/class-pending-dispatch.php index b29bb28fb..ea015541a 100644 --- a/src/mantle/queue/class-pending-dispatch.php +++ b/src/mantle/queue/class-pending-dispatch.php @@ -10,6 +10,7 @@ use Mantle\Container\Container; use Mantle\Contracts\Queue\Dispatcher; use Mantle\Contracts\Queue\Job; +use RuntimeException; /** * Allow jobs to be added to the queue with ease. @@ -18,9 +19,9 @@ class Pending_Dispatch { /** * Job instance. * - * @var Job + * @var Closure_Job|Job */ - protected $job; + protected Closure_Job|Job $job; // phpcs:ignore Squiz.Commenting.VariableComment.Missing /** * Constructor. @@ -34,10 +35,16 @@ public function __construct( $job ) { /** * Add a dispatch to a specific queue. * + * @throws RuntimeException If the job does not support queueing. + * * @param string $queue Queue to add to. * @return static */ public function on_queue( string $queue ): Pending_Dispatch { + if ( ! method_exists( $this->job, 'on_queue' ) ) { + throw new RuntimeException( 'Job does not support queueing.' ); + } + $this->job->on_queue( $queue ); return $this; @@ -46,10 +53,16 @@ public function on_queue( string $queue ): Pending_Dispatch { /** * Set the delay before the job will be run. * + * @throws RuntimeException If the job does not support queueing. + * * @param int $delay Delay in seconds. * @return static */ public function delay( int $delay ): Pending_Dispatch { + if ( ! method_exists( $this->job, 'delay' ) ) { + throw new RuntimeException( 'Job does not support queueing.' ); + } + $this->job->delay( $delay ); return $this; @@ -59,13 +72,13 @@ public function delay( int $delay ): Pending_Dispatch { * Handle the job and send it to the queue. */ public function __destruct() { - if ( ! $this->job ) { + if ( ! isset( $this->job ) ) { return; } // Allow the queue package to be run independent of the application. if ( ! class_exists( \Mantle\Application\Application::class ) ) { - Container::getInstance()->make( Dispatcher::class )->dispatch( $this->job ); + Container::get_instance()->make( Dispatcher::class )->dispatch( $this->job ); } else { app( Dispatcher::class )->dispatch( $this->job ); } diff --git a/src/mantle/queue/class-queue-fake.php b/src/mantle/queue/class-queue-fake.php index a2ef37f56..8ef63b1af 100644 --- a/src/mantle/queue/class-queue-fake.php +++ b/src/mantle/queue/class-queue-fake.php @@ -10,12 +10,21 @@ use Mantle\Queue\Queue_Manager; use PHPUnit\Framework\Assert as PHPUnit; +use function Mantle\Support\Helpers\collect; + /** * Queue Fake * * Used as a fake provider to perform assertions against. */ class Queue_Fake extends Queue_Manager { + /** + * Pushed jobs. + * + * @var array + */ + protected $jobs = []; + /** * Assert if a job was pushed based on a truth-test callback. * @@ -81,21 +90,17 @@ public function assertNothingPushed() { * * @param string $job * @param callable|null $callback - * @return \Illuminate\Support\Collection + * @return \Mantle\Support\Collection */ public function pushed( $job, $callback = null ) { if ( ! $this->hasPushed( $job ) ) { return collect(); } - $callback = $callback ?: function () { - return true; - }; + $callback = $callback ?: fn () => true; return collect( $this->jobs[ $job ] )->filter( - function ( $data ) use ( $callback ) { - return $callback( $data['job'], $data['queue'] ); - } + fn ( $data ) => $callback( $data['job'], $data['queue'] ), )->pluck( 'job' ); } @@ -108,4 +113,20 @@ function ( $data ) use ( $callback ) { public function hasPushed( $job ) { return isset( $this->jobs[ $job ] ) && ! empty( $this->jobs[ $job ] ); } + + /** + * Push a new job onto the queue. + * + * @param string|object $job + * @param mixed $data + * @param string $queue + * @return void + */ + public function push( $job, $data = '', $queue = null ) { + $this->jobs[ is_object( $job ) ? get_class( $job ) : $job ][] = [ + 'data' => $data, + 'job' => $job, + 'queue' => $queue, + ]; + } } diff --git a/src/mantle/queue/class-queue-manager.php b/src/mantle/queue/class-queue-manager.php index 0b0e94eba..d974f323e 100644 --- a/src/mantle/queue/class-queue-manager.php +++ b/src/mantle/queue/class-queue-manager.php @@ -26,9 +26,9 @@ class Queue_Manager implements Queue_Manager_Contract { /** * Provider class map. * - * @var string[] + * @var class-string[]|Provider[] */ - protected $providers = []; + protected array $providers = []; /** * Provider connections. @@ -65,20 +65,21 @@ public function get_provider( string $name = null ): Provider { /** * Add a provider for the queue manager. * - * @param string $name Provider name. - * @param string $provider Provider class name/instance. + * @param string $name Provider name. + * @param Provider|class-string $provider Provider class name/instance. * @return static * * @throws InvalidArgumentException Thrown invalid provider. */ public function add_provider( string $name, $provider ) { - $provider_class = is_object( $provider ) ? get_class( $provider ) : $provider; - - if ( ! class_implements( $provider_class, Provider::class ) ) { - throw new InvalidArgumentException( "Provider does not implement Provider contract: [$provider_class]" ); + if ( is_string( $provider ) && ( ! class_exists( $provider ) || ! in_array( Provider::class, class_implements( $provider ), true ) ) ) { + throw new InvalidArgumentException( "Provider does not implement Provider contract: [$provider]" ); + } elseif ( is_object( $provider ) && ! ( $provider instanceof Provider ) ) { // @phpstan-ignore-line is always false + throw new InvalidArgumentException( "Provider does not implement Provider contract: [$provider::class]" ); } $this->providers[ $name ] = $provider; + return $this; } diff --git a/src/mantle/queue/class-worker.php b/src/mantle/queue/class-worker.php index ddca1e537..29a327ab6 100644 --- a/src/mantle/queue/class-worker.php +++ b/src/mantle/queue/class-worker.php @@ -57,6 +57,7 @@ public function __construct( Queue_Manager $manager, Dispatcher $events ) { * @param string $queue Queue name. */ public function run( int $size, string $queue = null ) { + $queue ??= 'default'; $provider = $this->manager->get_provider(); $jobs = $provider->pop( $queue, $size ); diff --git a/src/mantle/queue/console/class-run-command.php b/src/mantle/queue/console/class-run-command.php index f31034949..5a506d300 100644 --- a/src/mantle/queue/console/class-run-command.php +++ b/src/mantle/queue/console/class-run-command.php @@ -74,7 +74,7 @@ function( Run_Complete $event ) { ); $this->container['queue.worker']->run( - (int) $this->option( 'count', (int) $this->container['config']['queue.batch_size'] ?? 1 ), + (int) $this->option( 'count', (int) ( $this->container['config']['queue.batch_size'] ?? 1 ) ), $queue ); } diff --git a/src/mantle/queue/events/class-run-complete.php b/src/mantle/queue/events/class-run-complete.php index 4761eda30..bceecd311 100644 --- a/src/mantle/queue/events/class-run-complete.php +++ b/src/mantle/queue/events/class-run-complete.php @@ -7,34 +7,8 @@ namespace Mantle\Queue\Events; -use Mantle\Contracts\Queue\Provider; - /** * Run Complete Event */ -class Run_Complete { - /** - * Queue provider. - * - * @var mixed - */ - public $provider; - - /** - * Queue Name - * - * @var string - */ - public $queue; - - /** - * Constructor. - * - * @param Provider $provider Queue provider. - * @param string $queue Queue name. - */ - public function __construct( Provider $provider, $queue ) { - $this->provider = $provider; - $this->queue = $queue; - } +class Run_Complete extends Run_Start { } diff --git a/src/mantle/queue/events/class-run-start.php b/src/mantle/queue/events/class-run-start.php index ef42cb167..ec6ff4fb6 100644 --- a/src/mantle/queue/events/class-run-start.php +++ b/src/mantle/queue/events/class-run-start.php @@ -8,33 +8,19 @@ namespace Mantle\Queue\Events; use Mantle\Contracts\Queue\Provider; +use Mantle\Support\Collection; /** * Run Start Event */ class Run_Start { - /** - * Queue provider. - * - * @var mixed - */ - public $provider; - - /** - * Queue Name - * - * @var string - */ - public $queue; - /** * Constructor. * - * @param Provider $provider Queue provider. - * @param string $queue Queue name. + * @param Provider $provider Queue provider. + * @param string $queue Queue name. + * @param Collection $jobs Jobs to run. */ - public function __construct( Provider $provider, $queue ) { - $this->provider = $provider; - $this->queue = $queue; + public function __construct( public Provider $provider, public string $queue, public Collection $jobs ) { } } diff --git a/src/mantle/queue/providers/wordpress/class-queue-worker-job.php b/src/mantle/queue/providers/wordpress/class-queue-worker-job.php index 5cacba097..bb41daca9 100644 --- a/src/mantle/queue/providers/wordpress/class-queue-worker-job.php +++ b/src/mantle/queue/providers/wordpress/class-queue-worker-job.php @@ -26,7 +26,7 @@ class Queue_Worker_Job extends \Mantle\Queue\Queue_Worker_Job { * * @var int */ - protected $queue_post_id; + protected ?int $queue_post_id = null; /** * Flag if the job failed. @@ -66,7 +66,7 @@ public function fire() { * @return int|null */ public function get_post_id(): ?int { - return $this->queue_post_id ?? null; + return $this->queue_post_id; } /** diff --git a/src/mantle/scheduling/class-command-event.php b/src/mantle/scheduling/class-command-event.php index 027bf9ad2..d1bc3a6ed 100644 --- a/src/mantle/scheduling/class-command-event.php +++ b/src/mantle/scheduling/class-command-event.php @@ -7,8 +7,9 @@ namespace Mantle\Scheduling; -use Mantle\Contracts\Container; -use Mantle\Framework\Exceptions\Handler; +use DateTimeZone; +use Mantle\Contracts\Application; +use Mantle\Contracts\Exceptions\Handler; use Throwable; /** @@ -17,20 +18,6 @@ * Allow a command to be run on a specific schedule. */ class Command_Event extends Event { - /** - * The command string. - * - * @var string - */ - public $command; - - /** - * The command arguments. - * - * @var array - */ - public $arguments; - /** * The associated command arguments (flags). * @@ -41,35 +28,34 @@ class Command_Event extends Event { /** * Constructor. * - * @param string $command Command class to run. - * @param array $arguments Arguments for the command. - * @param array $assoc_args Associated arguments for the command. - * @param string $timezone Timezone for the event. + * @param string $command Command class to run. + * @param array $parameters Arguments for the command. + * @param array $assoc_args Associated arguments for the command. + * @param DateTimeZone|null $timezone Timezone for the event. */ - public function __construct( string $command, array $arguments = [], array $assoc_args = [], $timezone = null ) { - parent::__construct( null, [], $timezone ); + public function __construct( string $command, array $parameters = [], array $assoc_args = [], ?DateTimeZone $timezone = null ) { + parent::__construct( $command, $parameters, $timezone ); - $this->command = $command; - $this->arguments = $arguments; $this->assoc_args = $assoc_args; } /** * Run the event. * - * @param Container $container Container instance. + * @param Application $container Container instance. */ - public function run( Container $container ) { + public function run( Application $container ) { if ( ! $this->filters_pass( $container ) ) { return; } $this->call_before_callbacks( $container ); - $instance = $container->make( $this->command ); + $instance = $container->make( $this->callback ); try { - $instance->callback( $this->arguments, $this->assoc_args ); + // todo: revisit this and test the command to run. + $instance->callback( $this->parameters, $this->assoc_args ); $this->exit_code = 0; } catch ( Throwable $e ) { diff --git a/src/mantle/scheduling/class-event.php b/src/mantle/scheduling/class-event.php index 43b5d9e28..cdda9a442 100644 --- a/src/mantle/scheduling/class-event.php +++ b/src/mantle/scheduling/class-event.php @@ -11,6 +11,7 @@ use Carbon\Carbon; use Closure; use Cron\CronExpression; +use DateTimeZone; use GuzzleHttp\Client as HttpClient; use GuzzleHttp\Exception\TransferException; use Mantle\Contracts\Application; @@ -26,20 +27,6 @@ class Event { use Macroable, Manages_Frequencies; - /** - * The event callback. - * - * @var \Closure|string - */ - public $callback; - - /** - * The event callback parameters. - * - * @var array - */ - public $parameters; - /** * The cron expression representing the event's frequency. * @@ -47,13 +34,6 @@ class Event { */ public $expression = '* * * * *'; - /** - * The timezone the date should be evaluated on. - * - * @var \DateTimeZone|string - */ - public $timezone; - /** * The list of environments the command should run under. * @@ -121,22 +101,23 @@ class Event { /** * Create a new event instance. * - * @param \Closure|string $callback Event callback. - * @param array $parameters Event parameters.. - * @param \DateTimeZone|null $timezone Event timezone. + * @param \Closure|string $callback Event callback or class name. + * @param array $parameters Event parameters.. + * @param DateTimeZone|null $timezone Event timezone. */ - public function __construct( $callback, array $parameters = [], $timezone = null ) { - $this->callback = $callback; - $this->parameters = $parameters; - $this->timezone = $timezone; + public function __construct( + protected $callback, + protected array $parameters = [], + protected ?DateTimeZone $timezone = null, + ) { } /** * Run the given event, assumed to be a closure or callable callback. * - * @param Container $container + * @param Application $container */ - public function run( Container $container ) { + public function run( Application $container ) { if ( ! $this->filters_pass( $container ) ) { return; } diff --git a/src/mantle/scheduling/class-job-event.php b/src/mantle/scheduling/class-job-event.php index 3b8a7fc89..dec9b31fd 100644 --- a/src/mantle/scheduling/class-job-event.php +++ b/src/mantle/scheduling/class-job-event.php @@ -7,7 +7,8 @@ namespace Mantle\Scheduling; -use Mantle\Contracts\Container; +use DateTimeZone; +use Mantle\Contracts\Application; use Mantle\Framework\Exceptions\Handler; use Throwable; @@ -17,20 +18,6 @@ * Allow a job to be run on a specific schedule. */ class Job_Event extends Event { - /** - * The job class. - * - * @var string - */ - public $job; - - /** - * The command arguments. - * - * @var array - */ - public $arguments; - /** * The associated command arguments (flags). * @@ -38,36 +25,22 @@ class Job_Event extends Event { */ public $assoc_args; - /** - * Constructor. - * - * @param string $job Job to run. - * @param array $arguments Arguments for the command. - * @param string $timezone Timezone for the event. - */ - public function __construct( string $job, array $arguments = [], $timezone = null ) { - parent::__construct( null, [], $timezone ); - - $this->job = $job; - $this->arguments = $arguments; - } - /** * Run the event. * - * @param Container $container Container instance. + * @param Application $container Container instance. */ - public function run( Container $container ) { + public function run( Application $container ) { if ( ! $this->filters_pass( $container ) ) { return; } $this->call_before_callbacks( $container ); - $instance = $container->make( $this->job ); + $instance = $container->make( $this->callback ); try { - $instance->handle( $this->arguments ); + $instance->handle( $this->parameters ); $this->exit_code = 0; } catch ( Throwable $e ) { diff --git a/src/mantle/scheduling/class-schedule.php b/src/mantle/scheduling/class-schedule.php index 495bac7f1..847e6e31e 100644 --- a/src/mantle/scheduling/class-schedule.php +++ b/src/mantle/scheduling/class-schedule.php @@ -32,16 +32,16 @@ class Schedule { /** * Container instance. * - * @var Container + * @var Application */ protected $container; /** * Timezone for scheduling. * - * @var DateTimeZone + * @var DateTimeZone|null */ - protected $timezone; + protected ?DateTimeZone $timezone = null; /** * All of the events on the schedule. @@ -53,10 +53,10 @@ class Schedule { /** * Constructor. * - * @param Container $container Container instance. + * @param Application $container Application container instance. * @param DateTimeZone $timezone Timezone instance, optional. */ - public function __construct( Container $container, DateTimeZone $timezone = null ) { + public function __construct( Application $container, DateTimeZone $timezone = null ) { $this->container = $container; if ( $timezone ) { @@ -87,9 +87,7 @@ public static function schedule_cron_event() { \add_action( static::CRON_HOOK, - function() { - app( static::class )->run_due_events(); - } + fn () => app( static::class )->run_due_events(), ); } @@ -99,11 +97,7 @@ function() { public function run_due_events() { $this ->due_events( $this->container ) - ->each( - function ( Event $event ) { - $event->run( $this->container ); - } - ); + ->each( fn ( Event $event ) => $event->run( $this->container ) ); } /** @@ -178,10 +172,12 @@ public function call( $callback, array $arguments = [] ): Event { * Get all of the events on the schedule that are due. * * @param Application $app Application instance. - * @return Collection + * @return Collection */ public function due_events( Application $app ): Collection { - return collect( $this->events )->filter->is_due( $app ); + return collect( $this->events() )->filter( + fn ( Event $event ) => $event->is_due( $app ), + ); } /** diff --git a/src/mantle/scheduling/trait-manages-frequencies.php b/src/mantle/scheduling/trait-manages-frequencies.php index 995f8f309..747f670b0 100644 --- a/src/mantle/scheduling/trait-manages-frequencies.php +++ b/src/mantle/scheduling/trait-manages-frequencies.php @@ -21,7 +21,7 @@ trait Manages_Frequencies { * The Cron expression representing the event's frequency. * * @param string $expression - * @return $this + * @return static */ public function cron( $expression ) { $this->expression = $expression; @@ -34,7 +34,7 @@ public function cron( $expression ) { * * @param string $start_time * @param string $end_time - * @return $this + * @return static */ public function between( $start_time, $end_time ) { return $this->when( $this->inTimeInterval( $start_time, $end_time ) ); @@ -45,7 +45,7 @@ public function between( $start_time, $end_time ) { * * @param string $start_time * @param string $end_time - * @return $this + * @return static */ public function unlessBetween( $start_time, $end_time ) { return $this->skip( $this->inTimeInterval( $start_time, $end_time ) ); @@ -67,21 +67,19 @@ private function inTimeInterval( $start_time, $end_time ) { if ( $end_time->lessThan( $start_time ) ) { if ( $start_time->greaterThan( $now ) ) { - $start_time->subDay( 1 ); + $start_time->subDay(); } else { - $end_time->addDay( 1 ); + $end_time->addDay(); } } - return function () use ( $now, $start_time, $end_time ) { - return $now->between( $start_time, $end_time ); - }; + return fn () => $now->between( $start_time, $end_time ); } /** * Schedule the event to run every minute. * - * @return $this + * @return static */ public function everyMinute() { return $this->spliceIntoPosition( 1, '*' ); @@ -90,7 +88,7 @@ public function everyMinute() { /** * Schedule the event to run every two minutes. * - * @return $this + * @return static */ public function everyTwoMinutes() { return $this->spliceIntoPosition( 1, '*/2' ); @@ -99,7 +97,7 @@ public function everyTwoMinutes() { /** * Schedule the event to run every three minutes. * - * @return $this + * @return static */ public function everyThreeMinutes() { return $this->spliceIntoPosition( 1, '*/3' ); @@ -108,7 +106,7 @@ public function everyThreeMinutes() { /** * Schedule the event to run every four minutes. * - * @return $this + * @return static */ public function everyFourMinutes() { return $this->spliceIntoPosition( 1, '*/4' ); @@ -117,7 +115,7 @@ public function everyFourMinutes() { /** * Schedule the event to run every five minutes. * - * @return $this + * @return static */ public function everyFiveMinutes() { return $this->spliceIntoPosition( 1, '*/5' ); @@ -126,7 +124,7 @@ public function everyFiveMinutes() { /** * Schedule the event to run every ten minutes. * - * @return $this + * @return static */ public function everyTenMinutes() { return $this->spliceIntoPosition( 1, '*/10' ); @@ -135,7 +133,7 @@ public function everyTenMinutes() { /** * Schedule the event to run every fifteen minutes. * - * @return $this + * @return static */ public function everyFifteenMinutes() { return $this->spliceIntoPosition( 1, '*/15' ); @@ -144,7 +142,7 @@ public function everyFifteenMinutes() { /** * Schedule the event to run every thirty minutes. * - * @return $this + * @return static */ public function everyThirtyMinutes() { return $this->spliceIntoPosition( 1, '0,30' ); @@ -153,7 +151,7 @@ public function everyThirtyMinutes() { /** * Schedule the event to run hourly. * - * @return $this + * @return static */ public function hourly() { return $this->spliceIntoPosition( 1, 0 ); @@ -163,7 +161,7 @@ public function hourly() { * Schedule the event to run hourly at a given offset in the hour. * * @param array|int $offset - * @return $this + * @return static */ public function hourlyAt( $offset ) { $offset = is_array( $offset ) ? implode( ',', $offset ) : $offset; @@ -174,7 +172,7 @@ public function hourlyAt( $offset ) { /** * Schedule the event to run every two hours. * - * @return $this + * @return static */ public function everyTwoHours() { return $this->spliceIntoPosition( 1, 0 ) @@ -184,7 +182,7 @@ public function everyTwoHours() { /** * Schedule the event to run every three hours. * - * @return $this + * @return static */ public function everyThreeHours() { return $this->spliceIntoPosition( 1, 0 ) @@ -194,7 +192,7 @@ public function everyThreeHours() { /** * Schedule the event to run every four hours. * - * @return $this + * @return static */ public function everyFourHours() { return $this->spliceIntoPosition( 1, 0 ) @@ -204,7 +202,7 @@ public function everyFourHours() { /** * Schedule the event to run every six hours. * - * @return $this + * @return static */ public function everySixHours() { return $this->spliceIntoPosition( 1, 0 ) @@ -214,7 +212,7 @@ public function everySixHours() { /** * Schedule the event to run daily. * - * @return $this + * @return static */ public function daily() { return $this->spliceIntoPosition( 1, 0 ) @@ -225,7 +223,7 @@ public function daily() { * Schedule the command at a given time. * * @param string $time - * @return $this + * @return static */ public function at( $time ) { return $this->dailyAt( $time ); @@ -235,7 +233,7 @@ public function at( $time ) { * Schedule the event to run daily at a given time (10:00, 19:30, etc). * * @param string $time - * @return $this + * @return static */ public function dailyAt( $time ) { $segments = explode( ':', $time ); @@ -249,7 +247,7 @@ public function dailyAt( $time ) { * * @param int $first * @param int $second - * @return $this + * @return static */ public function twiceDaily( $first = 1, $second = 13 ) { $hours = $first . ',' . $second; @@ -261,7 +259,7 @@ public function twiceDaily( $first = 1, $second = 13 ) { /** * Schedule the event to run only on weekdays. * - * @return $this + * @return static */ public function weekdays() { return $this->spliceIntoPosition( 5, '1-5' ); @@ -270,7 +268,7 @@ public function weekdays() { /** * Schedule the event to run only on weekends. * - * @return $this + * @return static */ public function weekends() { return $this->spliceIntoPosition( 5, '0,6' ); @@ -279,7 +277,7 @@ public function weekends() { /** * Schedule the event to run only on Mondays. * - * @return $this + * @return static */ public function mondays() { return $this->days( 1 ); @@ -288,7 +286,7 @@ public function mondays() { /** * Schedule the event to run only on Tuesdays. * - * @return $this + * @return static */ public function tuesdays() { return $this->days( 2 ); @@ -297,7 +295,7 @@ public function tuesdays() { /** * Schedule the event to run only on Wednesdays. * - * @return $this + * @return static */ public function wednesdays() { return $this->days( 3 ); @@ -306,7 +304,7 @@ public function wednesdays() { /** * Schedule the event to run only on Thursdays. * - * @return $this + * @return static */ public function thursdays() { return $this->days( 4 ); @@ -315,7 +313,7 @@ public function thursdays() { /** * Schedule the event to run only on Fridays. * - * @return $this + * @return static */ public function fridays() { return $this->days( 5 ); @@ -324,7 +322,7 @@ public function fridays() { /** * Schedule the event to run only on Saturdays. * - * @return $this + * @return static */ public function saturdays() { return $this->days( 6 ); @@ -333,7 +331,7 @@ public function saturdays() { /** * Schedule the event to run only on Sundays. * - * @return $this + * @return static */ public function sundays() { return $this->days( 0 ); @@ -342,7 +340,7 @@ public function sundays() { /** * Schedule the event to run weekly. * - * @return $this + * @return static */ public function weekly() { return $this->spliceIntoPosition( 1, 0 ) @@ -355,7 +353,7 @@ public function weekly() { * * @param int $day * @param string $time - * @return $this + * @return static */ public function weeklyOn( $day, $time = '0:0' ) { $this->dailyAt( $time ); @@ -366,7 +364,7 @@ public function weeklyOn( $day, $time = '0:0' ) { /** * Schedule the event to run monthly. * - * @return $this + * @return static */ public function monthly() { return $this->spliceIntoPosition( 1, 0 ) @@ -379,7 +377,7 @@ public function monthly() { * * @param int $day * @param string $time - * @return $this + * @return static */ public function monthlyOn( $day = 1, $time = '0:0' ) { $this->dailyAt( $time ); @@ -393,7 +391,7 @@ public function monthlyOn( $day = 1, $time = '0:0' ) { * @param int $first * @param int $second * @param string $time - * @return $this + * @return static */ public function twiceMonthly( $first = 1, $second = 16, $time = '0:0' ) { $days = $first . ',' . $second; @@ -409,7 +407,7 @@ public function twiceMonthly( $first = 1, $second = 16, $time = '0:0' ) { * Schedule the event to run on the last day of the month. * * @param string $time - * @return $this + * @return static */ public function lastDayOfMonth( $time = '0:0' ) { $this->dailyAt( $time ); @@ -420,7 +418,7 @@ public function lastDayOfMonth( $time = '0:0' ) { /** * Schedule the event to run quarterly. * - * @return $this + * @return static */ public function quarterly() { return $this->spliceIntoPosition( 1, 0 ) @@ -432,7 +430,7 @@ public function quarterly() { /** * Schedule the event to run yearly. * - * @return $this + * @return static */ public function yearly() { return $this->spliceIntoPosition( 1, 0 ) @@ -445,7 +443,7 @@ public function yearly() { * Set the days of the week the command should run on. * * @param array|mixed $days - * @return $this + * @return static */ public function days( $days ) { $days = is_array( $days ) ? $days : func_get_args(); @@ -457,7 +455,7 @@ public function days( $days ) { * Set the timezone the date should be evaluated on. * * @param \DateTimeZone|string $timezone - * @return $this + * @return static */ public function timezone( $timezone ) { $this->timezone = $timezone; @@ -468,14 +466,14 @@ public function timezone( $timezone ) { /** * Splice the given value into the given position of the expression. * - * @param int $position - * @param string $value - * @return $this + * @param int $position + * @param int|string $value + * @return static */ - protected function spliceIntoPosition( $position, $value ) { + protected function spliceIntoPosition( int $position, int|string $value ) { $segments = explode( ' ', $this->expression ); - $segments[ $position - 1 ] = $value; + $segments[ $position - 1 ] = (string) $value; return $this->cron( implode( ' ', $segments ) ); }