Skip to content

Commit

Permalink
Merge pull request #3009 from justlevine/chore/strict-phpstan-fixes
Browse files Browse the repository at this point in the history
chore: implement stricter PHPStan config and clean up unnecessary type-guards
  • Loading branch information
jasonbahl authored Jan 23, 2024
2 parents bed443d + 19440ea commit 5db11fb
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 33 deletions.
16 changes: 15 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
parameters:
level: 8
dynamicConstantNames:
- WPGRAPHQL_AUTOLOAD
treatPhpDocTypesAsCertain: false
inferPrivatePropertyTypeFromConstructor: true
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkExplicitMixedMissingReturn: true
checkFunctionNameCase: true
checkInternalClassCaseSensitivity: true
checkMissingIterableValueType: true
treatPhpDocTypesAsCertain: false
checkTooWideReturnTypesInProtectedAndPublicMethods: true
polluteScopeWithAlwaysIterableForeach: false
polluteScopeWithLoopInitialAssignments: false
reportAlwaysTrueInLastCondition: true
reportStaticMethodSignatures: true
reportWrongPhpDocTypeInVarTag: true
stubFiles:
# Simulate added properties
- phpstan/class-wp-post-type.php
Expand Down
3 changes: 0 additions & 3 deletions phpstan/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
*/

define( 'WP_LANG_DIR', true );
define( 'SAVEQUERIES', true );
define( 'WPGRAPHQL_PLUGIN_URL', true );
define( 'WP_CONTENT_DIR', true );
define( 'WP_PLUGIN_DIR', true );
define( 'WPGRAPHQL_AUTOLOAD', false );
define( 'PHPSTAN', true );
2 changes: 1 addition & 1 deletion src/Data/Connection/MenuItemConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function get_query_args() {
}

// Only query for menu items in assigned locations.
if ( ! empty( $locations ) && is_array( $locations ) ) {
if ( ! empty( $locations ) ) {

// unset the location arg
// we don't need this passed as a taxonomy parameter to wp_query
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/PluginConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function get_query() {
$recently_activated_list = isset( $active_stati['recently_activated'] ) ? get_site_option( 'recently_activated', [] ) : [];

// Loop through the plugins, add additional data, and store them in $plugins_by_status.
foreach ( (array) $all_plugins as $plugin_file => $plugin_data ) {
foreach ( $all_plugins as $plugin_file => $plugin_data ) {
if ( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin_file ) ) {
unset( $all_plugins[ $plugin_file ] );
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Data/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public static function get_allowed_settings_by_group( TypeRegistry $type_registr
/**
* Set the setting groups that are allowed
*/
$allowed_settings_by_group = ! empty( $allowed_settings_by_group ) && is_array( $allowed_settings_by_group ) ? $allowed_settings_by_group : [];
$allowed_settings_by_group = ! empty( $allowed_settings_by_group ) ? $allowed_settings_by_group : [];

/**
* Filter the $allowed_settings_by_group to allow enabling or disabling groups in the GraphQL Schema.
Expand Down Expand Up @@ -513,7 +513,7 @@ public static function get_allowed_settings( TypeRegistry $type_registry ) {
/**
* Verify that we have the allowed settings
*/
$allowed_settings = ! empty( $allowed_settings ) && is_array( $allowed_settings ) ? $allowed_settings : [];
$allowed_settings = ! empty( $allowed_settings ) ? $allowed_settings : [];

/**
* Filter the $allowed_settings to allow some to be enabled or disabled from showing in
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Loader/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function loadKeys( array $keys ) {
$plugins = array_merge( $site_plugins, $mu_plugins, $dropin_plugins );

$loaded = [];
if ( ! empty( $plugins ) && is_array( $plugins ) ) {
if ( ! empty( $plugins ) ) {
foreach ( $keys as $key ) {
if ( isset( $plugins[ $key ] ) ) {
$plugin = $plugins[ $key ];
Expand Down
7 changes: 3 additions & 4 deletions src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,8 @@ protected function wrap_fields() {
}

$clean_array = [];
$self = $this;
foreach ( $this->fields as $key => $data ) {
$clean_array[ $key ] = function () use ( $key, $data, $self ) {
$clean_array[ $key ] = function () use ( $key, $data ) {
if ( is_array( $data ) ) {
$callback = ( ! empty( $data['callback'] ) ) ? $data['callback'] : null;

Expand Down Expand Up @@ -386,9 +385,9 @@ protected function wrap_fields() {
$result = $pre;
} else {
if ( is_callable( $callback ) ) {
$self->setup();
$this->setup();
$field = call_user_func( $callback );
$self->tear_down();
$this->tear_down();
} else {
$field = $callback;
}
Expand Down
12 changes: 5 additions & 7 deletions src/Registry/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,13 +888,11 @@ public function get_types(): array {
*/
public function prepare_fields( array $fields, string $type_name ): array {
$prepared_fields = [];
if ( ! empty( $fields ) && is_array( $fields ) ) {
foreach ( $fields as $field_name => $field_config ) {
if ( is_array( $field_config ) && isset( $field_config['type'] ) ) {
$prepared_field = $this->prepare_field( $field_name, $field_config, $type_name );
if ( ! empty( $prepared_field ) ) {
$prepared_fields[ $this->format_key( $field_name ) ] = $prepared_field;
}
foreach ( $fields as $field_name => $field_config ) {
if ( is_array( $field_config ) && isset( $field_config['type'] ) ) {
$prepared_field = $this->prepare_field( $field_name, $field_config, $type_name );
if ( ! empty( $prepared_field ) ) {
$prepared_fields[ $this->format_key( $field_name ) ] = $prepared_field;
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/Registry/Utils/TermObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ protected static function get_connections( WP_Taxonomy $tax_object ) {
'description' => __( 'The ancestors of the node. Default ordered as lowest (closest to the child) to highest (closest to the root).', 'wp-graphql' ),
'connectionTypeName' => ucfirst( $tax_object->graphql_single_name ) . 'ToAncestors' . ucfirst( $tax_object->graphql_single_name ) . 'Connection',
'resolve' => static function ( Term $term, $args, AppContext $context, $info ) use ( $tax_object ) {
if ( ! $tax_object instanceof WP_Taxonomy ) {
return null;
}

$ancestor_ids = get_ancestors( absint( $term->term_id ), $term->taxonomyName, 'taxonomy' );

if ( empty( $ancestor_ids ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/Server/ValidationRules/QueryDepth.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function getMaxQueryDepth() {
public function setMaxQueryDepth( int $maxQueryDepth ) {
$this->checkIfGreaterOrEqualToZero( 'maxQueryDepth', $maxQueryDepth );

$this->maxQueryDepth = (int) $maxQueryDepth;
$this->maxQueryDepth = $maxQueryDepth;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Type/ObjectType/EnqueuedScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public static function register_type() {
'version' => [
'description' => __( 'The version of the enqueued script', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $script ) {
/** @var \WP_Scripts $wp_scripts */
global $wp_scripts;

return ! empty( $script->ver ) && is_string( $script->ver ) ? (string) $script->ver : $wp_scripts->default_version;
return ! empty( $script->ver ) && is_string( $script->ver ) ? $script->ver : $wp_scripts->default_version;
},
],
],
Expand Down
2 changes: 1 addition & 1 deletion src/Type/WPInterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct( array $config, TypeRegistry $type_registry ) {

$interface_config_fields = $interface_type->getFields();

if ( empty( $interface_config_fields ) || ! is_array( $interface_config_fields ) ) {
if ( empty( $interface_config_fields ) ) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/WPMutationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function is_config_valid( array $config ): bool {
$is_valid = false;
}

return (bool) $is_valid;
return $is_valid;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Type/WPObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function __construct( $config, TypeRegistry $type_registry ) {

$interface_config_fields = $interface_type->getFields();

if ( empty( $interface_config_fields ) || ! is_array( $interface_config_fields ) ) {
if ( empty( $interface_config_fields ) ) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Utils/InstrumentSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static function instrument_resolvers( Type $type, string $type_name ): Ty
* @return mixed[]
*/
protected static function wrap_fields( array $fields, string $type_name ) {
if ( empty( $fields ) || ! is_array( $fields ) ) {
if ( empty( $fields ) ) {
return $fields;
}

Expand Down Expand Up @@ -139,7 +139,7 @@ protected static function wrap_fields( array $fields, string $type_name ) {
* If the current field doesn't have a resolve function, use the defaultFieldResolver,
* otherwise use the $field_resolver
*/
if ( null === $field_resolver || ! is_callable( $field_resolver ) ) {
if ( null === $field_resolver ) {
$result = Executor::defaultFieldResolver( $source, $args, $context, $info );
} else {
$result = $field_resolver( $source, $args, $context, $info );
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/QueryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public static function get_wrapped_field_type( Type $type, FieldDefinition $fiel
}

if ( $type instanceof NonNull || $type instanceof ListOfType ) {
if ( $type instanceof ListOfType && isset( $parent_type->name ) && 'RootQuery' === $parent_type->name ) {
if ( $type instanceof ListOfType && isset( $parent_type->name ) ) {
$is_list_type = true;
}

Expand All @@ -362,7 +362,7 @@ public static function get_wrapped_field_type( Type $type, FieldDefinition $fiel

// Determine if we're dealing with a connection
if ( $type instanceof ObjectType || $type instanceof InterfaceType ) {
$interfaces = method_exists( $type, 'getInterfaces' ) ? $type->getInterfaces() : [];
$interfaces = $type->getInterfaces();
$interface_names = ! empty( $interfaces ) ? array_map(
static function ( InterfaceType $interface_obj ) {
return $interface_obj->name;
Expand Down

0 comments on commit 5db11fb

Please sign in to comment.