Skip to content

Commit

Permalink
PHPStan: Phase 4 finale (#387)
Browse files Browse the repository at this point in the history
* Starting pass at phpstan on database

* Contd work

* Continued work for database

* Starting work on collection

* Finishing collections

* PHPSTAN on interface

* Wrapping up for now

* Wrapping up phpstan for support

* Enable for scheduling

* PHPStan support for queue

* PHPStan on HTTP
  • Loading branch information
srtfisher committed May 17, 2023
1 parent a7cd851 commit d8a9ff8
Show file tree
Hide file tree
Showing 37 changed files with 280 additions and 413 deletions.
26 changes: 1 addition & 25 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/mantle/contracts/http/view/interface-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions src/mantle/http/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -134,8 +135,7 @@ function render_main_template() {
function loop( ...$args ) {
return view()
->loop( ...$args )
->map
->render()
->map( fn ( View $item ) => $item->render() )
->implode( '' );
}
}
Expand All @@ -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
}
}
Expand All @@ -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( '' );
}
}
Expand All @@ -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
}
}
Expand Down
29 changes: 18 additions & 11 deletions src/mantle/http/class-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -382,32 +389,31 @@ 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 ) ) {
// Remove internal route 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;
}

Expand All @@ -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;
}

/**
Expand All @@ -440,6 +446,7 @@ public function get_route(): ?Route {
*/
public function set_route( Route $route ) {
$this->route = $route;

return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mantle/http/class-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public function prefers( $content_types ) {
}
}
}

return null;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/mantle/http/routing/class-entity-router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
3 changes: 3 additions & 0 deletions src/mantle/http/routing/class-implicit-route-binding.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 4 additions & 6 deletions src/mantle/http/routing/class-response-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
}

/**
Expand All @@ -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 );
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/mantle/http/routing/class-rest-route-registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected function wrap_callback( callable $callback, string $route ): callable
'namespace' => $this->namespace,
'route' => $route,
],
$request
$request,
)
);

Expand All @@ -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 ),
)
);
};
Expand Down
26 changes: 7 additions & 19 deletions src/mantle/http/routing/class-route-registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
*/
Expand Down
6 changes: 2 additions & 4 deletions src/mantle/http/routing/class-route-signature-parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
);
}

Expand All @@ -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 [];
}

Expand Down
Loading

0 comments on commit d8a9ff8

Please sign in to comment.