data_layer()   F
last analyzed

Complexity

Conditions 16
Paths 528

Size

Total Lines 114
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 67
nc 528
nop 0
dl 0
loc 114
rs 2.0555
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Helick\GTM;
4
5
use WP_Taxonomy;
0 ignored issues
show
Bug introduced by
The type WP_Taxonomy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 * Get the container ID.
9
 *
10
 * @return string
11
 */
12
function container_id(): string
13
{
14
    return defined('GTM_CONTAINER_ID') ? GTM_CONTAINER_ID : '';
0 ignored issues
show
Bug introduced by
The constant Helick\GTM\GTM_CONTAINER_ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
}
16
17
/**
18
 * Get the data layer variable.
19
 *
20
 * @return string
21
 */
22
function data_layer_variable(): string
23
{
24
    $dataLayerVar = 'dataLayer';
25
26
    /**
27
     * Control the data layer variable.
28
     *
29
     * @param string $dataLayerVar
30
     */
31
    $dataLayerVar = apply_filters('helick_gtm_data_layer_variable', $dataLayerVar);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
    $dataLayerVar = /** @scrutinizer ignore-call */ apply_filters('helick_gtm_data_layer_variable', $dataLayerVar);
Loading history...
32
33
    $dataLayerVar = preg_replace('/[^a-z0-9_\-]/i', '', (string)$dataLayerVar);
34
35
    return $dataLayerVar;
36
}
37
38
/**
39
 * Get the data layer.
40
 *
41
 * @return array
42
 */
43
function data_layer(): array
44
{
45
    $dataLayer = [];
46
47
    if (is_multisite()) {
0 ignored issues
show
Bug introduced by
The function is_multisite was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
    if (/** @scrutinizer ignore-call */ is_multisite()) {
Loading history...
48
        $dataLayer['network'] = [
49
            'id'  => get_main_site_id(),
0 ignored issues
show
Bug introduced by
The function get_main_site_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            'id'  => /** @scrutinizer ignore-call */ get_main_site_id(),
Loading history...
50
            'url' => get_site_url(get_main_site_id()),
0 ignored issues
show
Bug introduced by
The function get_site_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            'url' => /** @scrutinizer ignore-call */ get_site_url(get_main_site_id()),
Loading history...
51
        ];
52
    }
53
54
    $dataLayer['site'] = [
55
        'id'  => is_multisite() ? get_current_blog_id() : 0,
0 ignored issues
show
Bug introduced by
The function get_current_blog_id was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        'id'  => is_multisite() ? /** @scrutinizer ignore-call */ get_current_blog_id() : 0,
Loading history...
56
        'url' => home_url(),
0 ignored issues
show
Bug introduced by
The function home_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        'url' => /** @scrutinizer ignore-call */ home_url(),
Loading history...
57
    ];
58
59
    $dataLayer['context'] = [
60
        'is_home'              => is_home(),
0 ignored issues
show
Bug introduced by
The function is_home was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        'is_home'              => /** @scrutinizer ignore-call */ is_home(),
Loading history...
61
        'is_front_page'        => is_front_page(),
0 ignored issues
show
Bug introduced by
The function is_front_page was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        'is_front_page'        => /** @scrutinizer ignore-call */ is_front_page(),
Loading history...
62
        'is_post_type_archive' => is_post_type_archive(),
0 ignored issues
show
Bug introduced by
The function is_post_type_archive was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        'is_post_type_archive' => /** @scrutinizer ignore-call */ is_post_type_archive(),
Loading history...
63
        'is_tax'               => is_tax(),
0 ignored issues
show
Bug introduced by
The function is_tax was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        'is_tax'               => /** @scrutinizer ignore-call */ is_tax(),
Loading history...
64
        'is_author'            => is_author(),
0 ignored issues
show
Bug introduced by
The function is_author was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        'is_author'            => /** @scrutinizer ignore-call */ is_author(),
Loading history...
65
        'is_date'              => is_date(),
0 ignored issues
show
Bug introduced by
The function is_date was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        'is_date'              => /** @scrutinizer ignore-call */ is_date(),
Loading history...
66
        'is_search'            => is_search(),
0 ignored issues
show
Bug introduced by
The function is_search was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        'is_search'            => /** @scrutinizer ignore-call */ is_search(),
Loading history...
67
        'is_singular'          => is_singular(),
0 ignored issues
show
Bug introduced by
The function is_singular was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        'is_singular'          => /** @scrutinizer ignore-call */ is_singular(),
Loading history...
68
        'is_404'               => is_404(),
0 ignored issues
show
Bug introduced by
The function is_404 was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        'is_404'               => /** @scrutinizer ignore-call */ is_404(),
Loading history...
69
    ];
70
71
    if (is_archive()) {
0 ignored issues
show
Bug introduced by
The function is_archive was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
    if (/** @scrutinizer ignore-call */ is_archive()) {
Loading history...
72
        if (is_date()) {
73
            $dataLayer['date'] = get_the_date();
0 ignored issues
show
Bug introduced by
The function get_the_date was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
            $dataLayer['date'] = /** @scrutinizer ignore-call */ get_the_date();
Loading history...
74
        }
75
76
        if (is_search()) {
77
            $dataLayer['search'] = get_search_query();
0 ignored issues
show
Bug introduced by
The function get_search_query was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
            $dataLayer['search'] = /** @scrutinizer ignore-call */ get_search_query();
Loading history...
78
        }
79
80
        if (is_post_type_archive()) {
81
            $dataLayer['post_type'] = get_post_type();
0 ignored issues
show
Bug introduced by
The function get_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
            $dataLayer['post_type'] = /** @scrutinizer ignore-call */ get_post_type();
Loading history...
82
        }
83
84
        if (is_tag() || is_category() || is_tax()) {
0 ignored issues
show
Bug introduced by
The function is_category was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        if (is_tag() || /** @scrutinizer ignore-call */ is_category() || is_tax()) {
Loading history...
Bug introduced by
The function is_tag was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        if (/** @scrutinizer ignore-call */ is_tag() || is_category() || is_tax()) {
Loading history...
85
            $term = get_queried_object();
0 ignored issues
show
Bug introduced by
The function get_queried_object was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            $term = /** @scrutinizer ignore-call */ get_queried_object();
Loading history...
86
87
            $dataLayer[$term->taxonomy] = [
88
                'id'   => $term->term_id,
89
                'slug' => $term->slug,
90
                'name' => $term->name,
91
            ];
92
        }
93
94
        if (is_author()) {
95
            $user = get_queried_object();
96
97
            $dataLayer['author'] = [
98
                'id'   => $user->ID,
99
                'slug' => $user->user_nicename,
100
                'name' => $user->display_name,
101
            ];
102
        }
103
    }
104
105
    if (is_singular()) {
106
        $dataLayer[get_post_type()] = [
107
            'id'           => get_the_ID(),
0 ignored issues
show
Bug introduced by
The function get_the_ID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
            'id'           => /** @scrutinizer ignore-call */ get_the_ID(),
Loading history...
108
            'slug'         => get_post_field('post_name'),
0 ignored issues
show
Bug introduced by
The function get_post_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            'slug'         => /** @scrutinizer ignore-call */ get_post_field('post_name'),
Loading history...
109
            'title'        => get_the_title(),
0 ignored issues
show
Bug introduced by
The function get_the_title was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            'title'        => /** @scrutinizer ignore-call */ get_the_title(),
Loading history...
110
            'template'     => get_page_template_slug(),
0 ignored issues
show
Bug introduced by
The function get_page_template_slug was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            'template'     => /** @scrutinizer ignore-call */ get_page_template_slug(),
Loading history...
111
            'comments'     => (int)get_comments_number(),
0 ignored issues
show
Bug introduced by
The function get_comments_number was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            'comments'     => (int)/** @scrutinizer ignore-call */ get_comments_number(),
Loading history...
112
            'published_at' => get_the_date('c'),
113
            'modified_at'  => get_the_modified_date('c'),
0 ignored issues
show
Bug introduced by
The function get_the_modified_date was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
            'modified_at'  => /** @scrutinizer ignore-call */ get_the_modified_date('c'),
Loading history...
114
        ];
115
116
        $dataLayer[get_post_type()]['author'] = [
117
            'id'   => get_the_author_meta('ID'),
0 ignored issues
show
Bug introduced by
The function get_the_author_meta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
            'id'   => /** @scrutinizer ignore-call */ get_the_author_meta('ID'),
Loading history...
118
            'slug' => get_the_author_meta('user_nicename'),
119
            'name' => get_the_author_meta('display_name'),
120
        ];
121
122
        $taxonomies = get_object_taxonomies(get_post_type(), 'objects');
0 ignored issues
show
Bug introduced by
The function get_object_taxonomies was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        $taxonomies = /** @scrutinizer ignore-call */ get_object_taxonomies(get_post_type(), 'objects');
Loading history...
123
        $taxonomies = array_filter($taxonomies, function (WP_Taxonomy $taxonomy) {
124
            return $taxonomy->public;
125
        });
126
127
        // TODO: ugly :(
128
        foreach ($taxonomies as $taxonomy) {
129
            $terms = get_the_terms(get_the_ID(), $taxonomy->name);
0 ignored issues
show
Bug introduced by
The function get_the_terms was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
            $terms = /** @scrutinizer ignore-call */ get_the_terms(get_the_ID(), $taxonomy->name);
Loading history...
130
            if (!$terms || is_wp_error($terms)) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
            if (!$terms || /** @scrutinizer ignore-call */ is_wp_error($terms)) {
Loading history...
131
                continue;
132
            }
133
134
            $dataLayer[get_post_type()][$taxonomy->name] = array_column($terms, 'slug');
135
        }
136
    }
137
138
    if (is_user_logged_in()) {
0 ignored issues
show
Bug introduced by
The function is_user_logged_in was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
    if (/** @scrutinizer ignore-call */ is_user_logged_in()) {
Loading history...
139
        $user = wp_get_current_user();
0 ignored issues
show
Bug introduced by
The function wp_get_current_user was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
        $user = /** @scrutinizer ignore-call */ wp_get_current_user();
Loading history...
140
141
        $dataLayer['user'] = [
142
            'id'           => $user->ID,
143
            'slug'         => $user->user_nicename,
144
            'name'         => $user->display_name,
145
            'capabilities' => array_keys($user->caps),
146
        ];
147
    }
148
149
    /**
150
     * Control the data layer.
151
     *
152
     * @param array $dataLayer
153
     */
154
    $dataLayer = apply_filters('helick_gtm_data_layer', $dataLayer);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
    $dataLayer = /** @scrutinizer ignore-call */ apply_filters('helick_gtm_data_layer', $dataLayer);
Loading history...
155
156
    return (array)$dataLayer;
157
}
158