Skip to content

Commit

Permalink
PHPStan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Dec 30, 2024
1 parent 53b830c commit 64d9741
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
"phpcs": "phpcs .",
"phpunit": "phpunit",
"phpstan": "phpstan --memory-limit=512M",
"lint": [
"@phpcs",
"@phpstan"
],
"lint:fix": [
"@phpcbf"
],
"test": [
"@phpcs",
"@phpstan",
Expand Down
6 changes: 4 additions & 2 deletions src/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ function get_entry_asset_map( string $dir_entry_name ): array {
$asset_file_path = trailingslashit( $base_path ) . 'index.asset.php';

if ( validate_path( $asset_file_path ) ) {
return include $asset_file_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.IncludingFile, WordPressVIPMinimum.Files.IncludingFile.UsingVariable
$asset_map = include $asset_file_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.IncludingFile, WordPressVIPMinimum.Files.IncludingFile.UsingVariable

return is_array( $asset_map ) ? $asset_map : []; // @phpstan-ignore-line returns array
}
}

Expand All @@ -69,7 +71,7 @@ function get_entry_asset_map( string $dir_entry_name ): array {
*
* @param string $dir_entry_name The entry point directory name.
*
* @return array<int, string> The asset's dependency array.
* @return array<string> The asset's dependency array.
*/
function get_asset_dependency_array( string $dir_entry_name ): array {
$asset_arr = get_entry_asset_map( $dir_entry_name );
Expand Down
21 changes: 16 additions & 5 deletions src/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function register_meta_helper(
'all' === $object_slugs
)
) {
return register_meta( $object_type, $meta_key, $args );
return register_meta( $object_type, $meta_key, $args ); // @phpstan-ignore-line array given
}

// Fix potential errors since we're allowing `$object_slugs` to be a string or array.
Expand All @@ -107,14 +107,14 @@ function register_meta_helper(
switch ( $object_type ) {
case 'post':
foreach ( $object_slugs as $object_slug ) {
if ( ! register_post_meta( $object_slug, $meta_key, $args ) ) {
if ( ! register_post_meta( $object_slug, $meta_key, $args ) ) { // @phpstan-ignore-line array given
return false;
}
}
break;
case 'term':
foreach ( $object_slugs as $object_slug ) {
if ( ! register_term_meta( $object_slug, $meta_key, $args ) ) {
if ( ! register_term_meta( $object_slug, $meta_key, $args ) ) { // @phpstan-ignore-line array given
return false;
}
}
Expand Down Expand Up @@ -146,13 +146,24 @@ function register_post_meta_from_defs(): void {

// Loop through definitions and register each.
foreach ( $definitions as $meta_key => $definition ) {
if ( ! is_array( $definition ) ) {
_doing_it_wrong( __FUNCTION__, 'Post meta definition items must be an array.', '1.0.0' );

continue;
}

// Extract post types.
$post_types = $definition['post_types'] ?? [];

// Unset since $definition is passed as register_meta args.
unset( $definition['post_types'] );

// Relocate schema, if specified at the top level.
if ( ! empty( $definition['schema'] ) ) {
if ( ! isset( $definition['show_in_rest'] ) || ! is_array( $definition['show_in_rest'] ) ) {
$definition['show_in_rest'] = [];
}

$definition['show_in_rest']['schema'] = $definition['schema'];
// Unset since $definition is passed as register_meta args.
unset( $definition['schema'] );
Expand All @@ -161,9 +172,9 @@ function register_post_meta_from_defs(): void {
// Register the meta.
register_meta_helper(
'post',
$post_types,
$post_types, // @phpstan-ignore-line array given
$meta_key,
$definition
$definition, // @phpstan-ignore-line array given
);
}
}

0 comments on commit 64d9741

Please sign in to comment.