Skip to content

Commit

Permalink
Adding PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Feb 28, 2024
1 parent 7d5edee commit 17822a5
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
[*.{php,neon}]
indent_size = 4
indent_style = tab

Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

### Added

* Added PHPStan to the development dependencies.
* `Alley\WP\Alleyvate\Feature` class implementing the `Alley\WP\Types\Feature` interface.

### Changed
Expand All @@ -22,7 +23,7 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C
### Removed

* `site_health`: Removed as a dedicated feature and now implemented directly in the plugin.
* `Alley\WP\Alleyvate\Feature` interface.
* `Alley\WP\Alleyvate\Feature` interface.

## 2.4.0

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"phpstan": "phpstan --memory-limit=512M",
"phpunit": "phpunit",
"test": [
"@phpcs",
"@lint",
"@phpunit"
]
},
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ parameters:
paths:
- src/
- wp-alleyvate.php

ignoreErrors:
- '#WP_REST_Request but does not specify its#'
12 changes: 2 additions & 10 deletions src/alley/wp/alleyvate/class-feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,8 @@ public function filtered_boot(): void {
/**
* Add debug information to the Site Health screen.
*
* @param array{
* wp-alleyvate?: array{
* fields?: array<array{label:string,value:string}>
* }
* } $info Debug information.
* @return array{
* wp-alleyvate?: array{
* fields?: array<array{label:string,value:string}>
* }
* } $info Debug information. Debug information.
* @param array<string, array{label: string, description: string, fields: array<int, mixed>}> $info Debug information.
* @return array<string, array{label: string, description: string, fields: array<int, mixed>}> $info Debug information. Debug information.
*/
public function add_debug_information( $info ): array {
if ( ! \is_array( $info ) ) {
Expand Down
4 changes: 2 additions & 2 deletions src/alley/wp/alleyvate/class-site-health-panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function boot(): void {
/**
* Add debug information for the feature.
*
* @param array $info Debug information.
* @return array
* @param array<string, array{label: string, description: string, fields: array<int, mixed>}> $info Debug information.
* @return array<string, array{label: string, description: string, fields: array<int, mixed>}> Debug information.
*/
public function add_debug_panel( $info ): array {
if ( ! \is_array( $info ) ) {
Expand Down
16 changes: 9 additions & 7 deletions src/alley/wp/alleyvate/features/class-disable-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public static function action__init(): void {
/**
* Short-circuits the comments query to return an empty array or 0 (if count was requested).
*
* @param array|int|null $comment_data Not used.
* @param \WP_Comment_Query $comment_query The comment query object to filter results for.
* @param array<mixed>|int|null $comment_data Not used.
* @param \WP_Comment_Query $comment_query The comment query object to filter results for.
* @return int|array<mixed>
*/
public static function filter__comments_pre_query( $comment_data, \WP_Comment_Query $comment_query ) {
return $comment_query->query_vars['count'] ? 0 : [];
Expand All @@ -106,9 +107,9 @@ public static function filter__comments_pre_query( $comment_data, \WP_Comment_Qu
/**
* Removes REST endpoints related to comments.
*
* @param array $endpoints REST endpoints to be filtered.
* @param array<string> $endpoints REST endpoints to be filtered.
*
* @return array Filtered endpoints.
* @return array<string> Filtered endpoints.
*/
public static function filter__rest_endpoints( array $endpoints ): array {
unset( $endpoints['/wp/v2/comments'] );
Expand All @@ -126,7 +127,8 @@ public static function filter__rest_endpoints( array $endpoints ): array {
*/
public static function filter__rest_prepare( \WP_REST_Response $response ): \WP_REST_Response {
$response->remove_link( 'replies' );
if ( isset( $response->data['comment_status'] ) ) {

if ( is_array( $response->data ) && isset( $response->data['comment_status'] ) ) {
$response->data['comment_status'] = 'closed';
}

Expand All @@ -136,9 +138,9 @@ public static function filter__rest_prepare( \WP_REST_Response $response ): \WP_
/**
* Removes rewrite rules related to comments.
*
* @param array $rules Rewrite rules to be filtered.
* @param array<string> $rules Rewrite rules to be filtered.
*
* @return array Filtered rewrite rules.
* @return array<string> Filtered rewrite rules.
*/
public static function filter__rewrite_rules_array( array $rules ): array {
foreach ( $rules as $regex => $rewrite ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function boot(): void {
* It generates an expensive query and is almost never used in practice.
*/
public static function action__add_meta_boxes(): void {
// @phpstan-ignore-next-line
remove_meta_box( 'postcustom', null, 'normal' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Disable_Dashboard_Widgets implements Feature {
/**
* Array of widgets to be removed.
*
* @var array|\string[][]
* @var array<array{context: string, priority: string, id: string}>
*/
public array $widgets = [
[
Expand Down
6 changes: 3 additions & 3 deletions src/alley/wp/alleyvate/features/class-disable-trackbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function action__init(): void {
* @return \WP_REST_Response Filtered response.
*/
public static function filter__rest_prepare( \WP_REST_Response $response ): \WP_REST_Response {
if ( isset( $response->data['ping_status'] ) ) {
if ( is_array( $response->data ) && isset( $response->data['ping_status'] ) ) {
$response->data['ping_status'] = 'closed';
}

Expand All @@ -59,9 +59,9 @@ public static function filter__rest_prepare( \WP_REST_Response $response ): \WP_
/**
* Removes rewrite rules related to trackbacks.
*
* @param array $rules Rewrite rules to be filtered.
* @param array<string, string> $rules Rewrite rules to be filtered.
*
* @return array Filtered rewrite rules.
* @return array<string, string> Filtered rewrite rules.
*/
public static function filter__rewrite_rules_array( array $rules ): array {
foreach ( $rules as $regex => $rewrite ) {
Expand Down
7 changes: 3 additions & 4 deletions src/alley/wp/alleyvate/features/class-login-nonce.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function boot(): void {
/**
* Adds the `no-store` flag to the `Cache-Control` headers.
*
* @param array $headers The headers array.
* @return array
* @param array<string, string> $headers The headers array.
* @return array<string, string>
*/
public static function add_no_store_to_login( $headers ): array {
if ( ! \is_array( $headers ) ) {
Expand All @@ -78,7 +78,7 @@ public static function add_no_store_to_login( $headers ): array {
* Add a meta refresh to the login page, so it will refresh after the nonce timeout.
*/
public static function action__add_meta_refresh(): void {
printf( '<meta http-equiv="refresh" content="%d">', esc_attr( self::NONCE_TIMEOUT ) );
printf( '<meta http-equiv="refresh" content="%d">', esc_attr( (string) self::NONCE_TIMEOUT ) );
}

/**
Expand Down Expand Up @@ -142,7 +142,6 @@ public static function action__pre_validate_login_nonce(): void {
// This is a login with an invalid nonce. Throw an error.
http_response_code( 403 );
wp_die( 'Login attempt failed. Please try again.', 'Login Error' );
return;
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/alley/wp/alleyvate/features/class-prevent-framing.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function boot(): void {
/**
* Output the X-Frame-Options header to prevent sites from being able to be iframe'd.
*
* @param array $headers The headers to be sent.
* @return array The headers to be sent.
* @param array<string, string> $headers The headers to be sent.
* @return array<string, string> The headers to be sent.
*/
public static function filter__wp_headers( $headers ): array {
if ( ! \is_array( $headers ) ) {
Expand Down

0 comments on commit 17822a5

Please sign in to comment.