• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 5604143621

pending completion
5604143621

push

github

web-flow
chore: implement WPGraphQL Coding Standards (#769)

* chore: avoid magic constants [PHPCS]

* chore: change ruleset to wp-graphql-cs and lint

* devops: HPOS added back to CI with the release of WC 3.9

---------

Co-authored-by: Geoff Taylor <geoff@axistaylor.com>

562 of 562 new or added lines in 77 files covered. (100.0%)

10309 of 12440 relevant lines covered (82.87%)

53.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.26
/includes/data/connection/class-product-attribute-connection-resolver.php
1
<?php
2
/**
3
 * ConnectionResolver - Product_Attribute_Connection_Resolver
4
 *
5
 * Resolves connections to ProductAttributes
6
 *
7
 * @package WPGraphQL\WooCommerce\Data\Connection
8
 * @since 0.0.1
9
 */
10

11
namespace WPGraphQL\WooCommerce\Data\Connection;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use GraphQLRelay\Relay;
16
use WPGraphQL\AppContext;
17

18
const GLOBAL_ID_DELIMITER = ':';
19

20
/**
21
 * Class Product_Attribute_Connection_Resolver
22
 */
23
class Product_Attribute_Connection_Resolver {
24
        /**
25
         * Builds Product attribute items
26
         *
27
         * @param array                                $attributes  Array of WC_Product_Attributes instances.
28
         * @param \WPGraphQL\WooCommerce\Model\Product $source      Parent product model.
29
         * @param array                                $args        Connection arguments.
30
         * @param \WPGraphQL\AppContext                $context     AppContext object.
31
         * @param \GraphQL\Type\Definition\ResolveInfo $info        ResolveInfo object.
32
         * @param string                               $type     Attribute type.
33
         *
34
         * @throws \GraphQL\Error\UserError  Invalid product attribute enumeration value.
35
         * @return array
36
         */
37
        private function get_items( $attributes, $source, $args, $context, $info, $type = null ) {
38
                $items = [];
1✔
39
                foreach ( $attributes as $attribute_name => $data ) {
1✔
40
                        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
41
                        $data->_relay_id = base64_encode(
1✔
42
                                $attribute_name
1✔
43
                                . GLOBAL_ID_DELIMITER
1✔
44
                                . $source->ID
1✔
45
                                . GLOBAL_ID_DELIMITER
1✔
46
                                . $data->get_name()
1✔
47
                        );
1✔
48
                        $items[]         = $data;
1✔
49
                }
50

51
                $type = ! empty( $args['where']['type'] ) ? $args['where']['type'] : $type;
1✔
52

53
                if ( ! is_null( $type ) ) {
1✔
54
                        switch ( $type ) {
55
                                case 'local':
×
56
                                        $items = array_filter(
×
57
                                                $items,
×
58
                                                static function ( $item ) {
×
59
                                                        return ! $item->is_taxonomy();
×
60
                                                }
×
61
                                        );
×
62
                                        break;
×
63
                                case 'global':
×
64
                                        $items = array_filter(
×
65
                                                $items,
×
66
                                                static function ( $item ) {
×
67
                                                        return $item->is_taxonomy();
×
68
                                                }
×
69
                                        );
×
70
                                        break;
×
71
                                default:
72
                                        throw new UserError( __( 'Invalid product attribute type provided', 'wp-graphql-woocommerce' ) );
×
73
                        }
74
                }//end if
75

76
                return $items;
1✔
77
        }
78

79
        /**
80
         * Creates connection
81
         *
82
         * @param mixed                                $source   Connection source Model instance.
83
         * @param array                                $args     Connection arguments.
84
         * @param \WPGraphQL\AppContext                $context  AppContext object.
85
         * @param \GraphQL\Type\Definition\ResolveInfo $info     ResolveInfo object.
86
         * @param string                               $type     Attribute type.
87
         *
88
         * @return array|null
89
         */
90
        public function resolve( $source, array $args, AppContext $context, ResolveInfo $info, $type = null ) {
91
                $attributes = $this->get_items( $source->attributes, $source, $args, $context, $info, $type );
1✔
92

93
                $connection = Relay::connectionFromArray( $attributes, $args );
1✔
94
                $nodes      = [];
1✔
95
                if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) {
1✔
96
                        foreach ( $connection['edges'] as $edge ) {
1✔
97
                                $nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null;
1✔
98
                        }
99
                }
100
                $connection['nodes'] = ! empty( $nodes ) ? $nodes : null;
1✔
101
                return ! empty( $attributes ) ? $connection : null;
1✔
102
        }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc