Skip to content

Commit

Permalink
chore: implement strict PHPStan rules and fix resulting issues (#409)
Browse files Browse the repository at this point in the history
* chore: implement strict PHPStan rules and fix resulting issues

* chore: lint and types

* fix: undefined $form in loadKeys()

* chore: return types
  • Loading branch information
justlevine committed Apr 6, 2024
1 parent 7b23b49 commit a7fed25
Show file tree
Hide file tree
Showing 33 changed files with 167 additions and 109 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Expand Up @@ -3,8 +3,9 @@
## [Unreleased]

- feat: Add plugin dependency header.
- chore: update Composer deps and lint.
- chore: lock WPBrowser to <3.5.0 to prevent conflicts with Codeception.
- chore: Update Composer deps and lint.
- chore: Lock WPBrowser to <3.5.0 to prevent conflicts with Codeception.
- chore: Implement strict PHPStan rules and fix resulting issues.
- ci: Update GitHub Actions to latest versions.
- ci: Test plugin compatibility with WordPress 6.5.0.
- ci: Test plugin compatibility with PHP 8.2.
Expand Down
17 changes: 16 additions & 1 deletion phpstan.neon.dist
@@ -1,9 +1,24 @@
parameters:
level: 8
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkExplicitMixedMissingReturn: true
checkFunctionNameCase: true
checkInternalClassCaseSensitivity: true
checkMissingIterableValueType: false # @todo make true
checkTooWideReturnTypesInProtectedAndPublicMethods: true
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
polluteScopeWithAlwaysIterableForeach: false
polluteScopeWithLoopInitialAssignments: false
reportAlwaysTrueInLastCondition: true
reportStaticMethodSignatures: true
reportWrongPhpDocTypeInVarTag: true
treatPhpDocTypesAsCertain: false
featureToggles:
disableRuntimeReflectionProvider: true
dynamicConstantNames:
- WPGRAPHQL_GF_AUTOLOAD
stubFiles:
# Simulate added properties
- phpstan/class-app-context.stub
Expand Down
1 change: 0 additions & 1 deletion phpstan/constants.php
Expand Up @@ -3,7 +3,6 @@
* Constants defined in this file are to help phpstan analyze code where constants outside the plugin (WordPress core constants, etc) are being used.
*/

define( 'WPGRAPHQL_GF_AUTOLOAD', true );
define( 'CRGEARY_JAMSTACK_DEPLOYMENTS_OPTIONS_KEY', 'wp-jamstack-deployments' );
define( 'WPGRAPHQL_GF_PLUGIN_FILE', 'wp-graphql-gravity-forms.php' );
define( 'WPGRAPHQL_GF_VERSION', '0.12.5' );
6 changes: 5 additions & 1 deletion src/Connection/AbstractConnection.php
Expand Up @@ -31,6 +31,8 @@ public static function register_hooks(): void {

/**
* Gets custom connection configuration arguments, such as the resolver, edgeFields, connectionArgs, etc.
*
* @return array<string,array<string,mixed>>
*/
public static function get_connection_args(): array {
return [];
Expand All @@ -39,7 +41,9 @@ public static function get_connection_args(): array {
/**
* Returns a filtered array of connection args.
*
* @param array $filter_by .
* @param string[] $filter_by .
*
* @return array<string,array<string,mixed>>
*/
public static function get_filtered_connection_args( ?array $filter_by = null ): array {
$connection_args = static::get_connection_args();
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/FormFieldsConnection.php
Expand Up @@ -31,7 +31,7 @@ public static function register(): void {
}

/**
* Gets custom connection configuration arguments, such as the resolver, edgeFields, connectionArgs, etc.
* {@inheritDoc}
*/
public static function get_connection_args(): array {
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Connection/FormFieldsConnectionResolver.php
Expand Up @@ -19,7 +19,7 @@
*/
class FormFieldsConnectionResolver {
/**
* @var array<string, null>
* @var array<string,null>
*/
private const EMPTY_CHOICES = [
'text' => null,
Expand Down
24 changes: 12 additions & 12 deletions src/Data/FieldValueInput/AbstractFieldValueInput.php
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractFieldValueInput {
/**
* The Gravity Forms entry object, if it exists.
*
* @var array|null
* @var array<string|int,mixed>|null
*/
protected ?array $entry;

Expand All @@ -47,7 +47,7 @@ abstract class AbstractFieldValueInput {
/**
* The Gravity Forms form object.
*
* @var array
* @var array<string,mixed>
*/
protected array $form;

Expand Down Expand Up @@ -75,11 +75,11 @@ abstract class AbstractFieldValueInput {
/**
* The class constructor.
*
* @param array $input_args The GraphQL input args for the form field.
* @param array $form The current Gravity Forms form object.
* @param bool $is_draft Whether the mutation is handling a Draft Entry.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array|null $entry The current Gravity Forms entry object.
* @param array $input_args The GraphQL input args for the form field.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param bool $is_draft Whether the mutation is handling a Draft Entry.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<int|string,mixed>|null $entry The current Gravity Forms entry object.
*
* @throws \GraphQL\Error\UserError .
*/
Expand All @@ -93,11 +93,11 @@ public function __construct( array $input_args, array $form, bool $is_draft, ?GF
/**
* Filters the accepted GraphQL input value key for the form field.
*
* @param string $name The GraphQL input value name to use. E.g. `nameValues`.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array $form The current Gravity Forms form object.
* @param array|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
* @param string $name The GraphQL input value name to use. E.g. `nameValues`.
* @param \GF_Field $field The current Gravity Forms field object.
* @param array<string,mixed> $form The current Gravity Forms form object.
* @param array<int|string,mixed>|null $entry The current Gravity Forms entry object. Only available when using update (`gfUpdateEntry`, `gfUpdateDraftEntry`) mutations.
* @param bool $is_draft_mutation Whether the mutation is handling a Draft Entry (`gfUpdateDraftEntry`, or `gfSubmitForm` when `saveAsDraft` is `true`).
*/
$this->field_name = apply_filters(
'graphql_gf_field_value_input_name',
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/AddressValuesInput.php
Expand Up @@ -35,6 +35,8 @@ protected function get_field_name(): string {

/**
* {@inheritDoc}
*
* @return array
*/
protected function prepare_value() {
$value = $this->args;
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/CheckboxValuesInput.php
Expand Up @@ -39,6 +39,8 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return array
*
* @throws \GraphQL\Error\UserError .
*/
protected function prepare_value() {
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/ConsentValueInput.php
Expand Up @@ -37,6 +37,8 @@ protected function get_field_name(): string {

/**
* {@inheritDoc}
*
* @return array
*/
protected function prepare_value() {
$field = $this->field;
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/FileUploadValuesInput.php
Expand Up @@ -32,6 +32,8 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return string|mixed
*
* @throws \GraphQL\Error\UserError
*/
protected function prepare_value() {
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/ImageValuesInput.php
Expand Up @@ -37,6 +37,8 @@ protected function get_field_name(): string {
/**
* {@inheritDoc}
*
* @return array<string,mixed>
*
* @throws \GraphQL\Error\UserError
*/
protected function prepare_value() {
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/NameValuesInput.php
Expand Up @@ -35,6 +35,8 @@ protected function get_field_name(): string {

/**
* {@inheritDoc}
*
* @return array
*/
protected function prepare_value() {
$value = $this->args;
Expand Down
2 changes: 2 additions & 0 deletions src/Data/FieldValueInput/ProductValueInput.php
Expand Up @@ -92,6 +92,8 @@ public function get_args() {

/**
* {@inheritDoc}
*
* @return array<string,mixed>
*/
protected function prepare_value() {
$field = $this->field;
Expand Down
18 changes: 5 additions & 13 deletions src/Data/Loader/DraftEntriesLoader.php
Expand Up @@ -34,19 +34,7 @@ protected function get_model( $entry, $key ): DraftEntry {
}

/**
* Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded
* posts as the values
*
* Note that order of returned values must match exactly the order of keys.
* If some entry is not available for given key - it must include null for the missing key.
*
* For example:
* loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3']
*
* @param array $keys .
*
* @return array|false
* @throws \Exception .
* {@inheritDoc}
*/
public function loadKeys( array $keys ) {
if ( empty( $keys ) ) {
Expand All @@ -55,6 +43,10 @@ public function loadKeys( array $keys ) {

$loaded_entries = [];
foreach ( $keys as $key ) {
if ( empty( $key ) ) {
continue;
}

$loaded_entries[ $key ] = GFFormsModel::get_draft_submission_values( $key ) ?: null;
}

Expand Down
22 changes: 7 additions & 15 deletions src/Data/Loader/EntriesLoader.php
Expand Up @@ -33,19 +33,7 @@ protected function get_model( $entry, $key ): SubmittedEntry {
}

/**
* Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded
* posts as the values
*
* Note that order of returned values must match exactly the order of keys.
* If some entry is not available for given key - it must include null for the missing key.
*
* For example:
* loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3']
*
* @param array $keys .
*
* @return array|false
* @throws \Exception .
* {@inheritDoc}
*/
protected function loadKeys( array $keys ) {
if ( empty( $keys ) ) {
Expand All @@ -55,8 +43,12 @@ protected function loadKeys( array $keys ) {
// Associate the requested keys with their loaded entry.
$loaded_entries = [];
foreach ( $keys as $key ) {
$entry = GFAPI::get_entry( $key );
$loaded_entries[ $key ] = ! is_wp_error( $entry ) ? $entry : null;
if ( empty( $key ) ) {
continue;
}

$entry = GFAPI::get_entry( (int) $key );
$loaded_entries[ $key ] = ! $entry instanceof \WP_Error ? $entry : null;
}

return $loaded_entries;
Expand Down
20 changes: 6 additions & 14 deletions src/Data/Loader/FormsLoader.php
Expand Up @@ -33,19 +33,7 @@ protected function get_model( $entry, $key ): Form {
}

/**
* Given array of keys, loads and returns a map consisting of keys from `keys` array and loaded
* posts as the values
*
* Note that order of returned values must match exactly the order of keys.
* If some entry is not available for given key - it must include null for the missing key.
*
* For example:
* loadKeys(['a', 'b', 'c']) -> ['a' => 'value1, 'b' => null, 'c' => 'value3']
*
* @param array $keys .
*
* @return array|false
* @throws \Exception .
* {@inheritDoc}
*/
protected function loadKeys( array $keys ) {
if ( empty( $keys ) ) {
Expand All @@ -54,7 +42,11 @@ protected function loadKeys( array $keys ) {

$loaded_forms = [];
foreach ( $keys as $key ) {
$form = GFAPI::get_form( $key );
if ( empty( $key ) ) {
continue;
}

$form = GFAPI::get_form( (int) $key );

// Run the form through `gform_pre_render` to support 3rd party plugins like Populate Anything.
if ( ! empty( $form ) ) {
Expand Down
Expand Up @@ -38,7 +38,7 @@ class QuizResults extends AbstractObject implements Field {

// @todo grab search criteria from connection args.
/**
* @var array<string, string>
* @var array<string,string>
*/
private const SEARCH_CRITERIA = [ 'status' => 'active' ];

Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/Enum.php
Expand Up @@ -15,6 +15,8 @@
interface Enum {
/**
* Gets the Enum type values.
*
* @return array<string,array<string,mixed>>
*/
public static function get_values(): array;
}
4 changes: 4 additions & 0 deletions src/Interfaces/Mutation.php
Expand Up @@ -15,13 +15,17 @@ interface Mutation {
/**
* Defines the input field configuration.
*
* @return array<string,array<string,mixed>>
*
* @since 0.4.0
*/
public static function get_input_fields(): array;

/**
* Defines the output field configuration.
*
* @return array<string,array<string,mixed>>
*
* @since 0.4.0
*/
public static function get_output_fields(): array;
Expand Down
2 changes: 2 additions & 0 deletions src/Interfaces/TypeWithConnections.php
Expand Up @@ -14,6 +14,8 @@
interface TypeWithConnections {
/**
* Gets the the connection config for the GraphQL Type.
*
* @return array<string,array<string,mixed>>
*/
public static function get_connections(): array;
}
4 changes: 3 additions & 1 deletion src/Interfaces/TypeWithFields.php
Expand Up @@ -13,7 +13,9 @@
*/
interface TypeWithFields {
/**
* Gets the properties for the type.
* Gets the GraphQL fields for the type.
*
* @return array<string,array<string,mixed>> The GraphQL field configs for the type.
*/
public static function get_fields(): array;
}
2 changes: 2 additions & 0 deletions src/Interfaces/TypeWithInterfaces.php
Expand Up @@ -14,6 +14,8 @@
interface TypeWithInterfaces {
/**
* Gets the the connection config for the GraphQL Type.
*
* @return string[]
*/
public static function get_interfaces(): array;
}
2 changes: 1 addition & 1 deletion src/Mutation/AbstractMutation.php
Expand Up @@ -51,7 +51,7 @@ public static function get_type_config(): array {
* @throws \GraphQL\Error\UserError .
*/
protected static function check_required_inputs( ?array $input ): void {
if ( empty( $input ) || ! is_array( $input ) ) {
if ( empty( $input ) ) {
throw new UserError( esc_html__( 'Mutation not processed. The input data was missing or invalid.', 'wp-graphql-gravity-forms' ) );
}
}
Expand Down

0 comments on commit a7fed25

Please sign in to comment.