Conditions | 44 |
Paths | 3602 |
Total Lines | 180 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
21 | public static function ao_ccss_enqueue( $hash ) { |
||
22 | $self = new self(); |
||
23 | // Get key status. |
||
24 | $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( false ); |
||
25 | |||
26 | // Queue is available to anyone... |
||
27 | $enqueue = true; |
||
28 | |||
29 | // ... which are not the ones below. |
||
30 | if ( is_user_logged_in() || is_feed() || is_404() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $self->ao_ccss_ua() || 'nokey' == $key['status'] || 'invalid' == $key['status'] || false === apply_filters( 'autoptimize_filter_ccss_enqueue_should_enqueue', true ) ) { |
||
31 | $enqueue = false; |
||
32 | autoptimizeCriticalCSSCore::ao_ccss_log( "Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls, to criticalcss.com itself or when a valid API key is not found", 3 ); |
||
33 | } |
||
34 | |||
35 | if ( $enqueue ) { |
||
36 | // Continue if queue is available |
||
37 | // Attach required arrays/ vars. |
||
38 | global $ao_ccss_rules; |
||
39 | global $ao_ccss_queue_raw; |
||
40 | global $ao_ccss_queue; |
||
41 | global $ao_ccss_forcepath; |
||
42 | |||
43 | // Get request path and page type, and initialize the queue update flag. |
||
44 | $req_orig = $_SERVER['REQUEST_URI']; |
||
45 | $req_path = strtok( $req_orig, '?' ); |
||
46 | |||
47 | // Check if we have a lang param. we need to keep as WPML can switch languages based on that |
||
48 | // and that includes RTL -> LTR so diff. structure, so rules would be RTL vs LTR |
||
49 | // but this needs changes in the structur of the rule object so off by default for now |
||
50 | // as now this will simply result in conditional rules being overwritten. |
||
51 | if ( apply_filters( 'autoptimize_filter_ccss_coreenqueue_honor_lang', false ) && strpos( $req_orig, 'lang=' ) !== false ) { |
||
52 | $req_params = strtok( '?' ); |
||
53 | parse_str( $req_params, $req_params_arr ); |
||
54 | if ( array_key_exists( 'lang', $req_params_arr ) && !empty( $req_params_arr['lang'] ) ) { |
||
55 | $req_path .= '?lang=' . $req_params_arr['lang']; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | $req_type = $self->ao_ccss_get_type(); |
||
60 | $job_qualify = false; |
||
61 | $target_rule = false; |
||
62 | $rule_properties = false; |
||
63 | $queue_update = false; |
||
64 | |||
65 | // Match for paths in rules. |
||
66 | foreach ( $ao_ccss_rules['paths'] as $path => $props ) { |
||
67 | |||
68 | // Prepare rule target and log. |
||
69 | $target_rule = 'paths|' . $path; |
||
70 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 ); |
||
71 | |||
72 | // Path match |
||
73 | // -> exact match needed for AUTO rules |
||
74 | // -> partial match OK for MANUAL rules (which have empty hash and a file with CCSS). |
||
75 | if ( $path === $req_path || ( false == $props['hash'] && false != $props['file'] && preg_match( '|' . $path . '|', $req_path ) ) ) { |
||
76 | |||
77 | // There's a path match in the rule, so job QUALIFIES with a path rule match. |
||
78 | $job_qualify = true; |
||
79 | $rule_properties = $props; |
||
80 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 ); |
||
81 | |||
82 | // Stop processing other path rules. |
||
83 | break; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // Match for types in rules if no path rule matches and if we're not enforcing paths. |
||
88 | if ( ! $job_qualify && ( ! $ao_ccss_forcepath || ! in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) || ! apply_filters( 'autoptimize_filter_ccss_coreenqueue_ignorealltypes', false ) ) ) { |
||
89 | foreach ( $ao_ccss_rules['types'] as $type => $props ) { |
||
90 | |||
91 | // Prepare rule target and log. |
||
92 | $target_rule = 'types|' . $type; |
||
93 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying page type <' . $req_type . '> on path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 ); |
||
94 | |||
95 | if ( $req_type == $type ) { |
||
96 | // Type match. |
||
97 | // There's a type match in the rule, so job QUALIFIES with a type rule match. |
||
98 | $job_qualify = true; |
||
99 | $rule_properties = $props; |
||
100 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Page type <' . $req_type . '> on path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 ); |
||
101 | |||
102 | // Stop processing other type rules. |
||
103 | break; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | if ( $job_qualify && ( ( false == $rule_properties['hash'] && false != $rule_properties['file'] ) || strpos( $req_type, 'template_' ) !== false ) ) { |
||
109 | // If job qualifies but rule hash is false and file isn't false (MANUAL rule) or if template, job does not qualify despite what previous evaluations says. |
||
110 | $job_qualify = false; |
||
111 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission DISQUALIFIED by MANUAL rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 ); |
||
112 | } elseif ( ! $job_qualify && empty( $rule_properties ) ) { |
||
113 | // But if job does not qualify and rule properties are set, job qualifies as there is no matching rule for it yet |
||
114 | // Fill-in the new target rule. |
||
115 | $job_qualify = true; |
||
116 | |||
117 | // Should we switch to path-base AUTO-rules? Conditions: |
||
118 | // 1. forcepath option has to be enabled (off by default) |
||
119 | // 2. request type should be (by default, but filterable) one of is_page (removed for now: woo_is_product or woo_is_product_category). |
||
120 | if ( ( $ao_ccss_forcepath && in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) || apply_filters( 'autoptimize_filter_ccss_coreenqueue_ignorealltypes', false ) ) { |
||
121 | if ( '/' !== $req_path ) { |
||
122 | $target_rule = 'paths|' . $req_path; |
||
123 | } else { |
||
124 | // Exception; we don't want a path-based rule for "/" as that messes things up, hard-switch this to a type-based is_front_page rule. |
||
125 | $target_rule = 'types|' . 'is_front_page'; |
||
126 | } |
||
127 | } else { |
||
128 | $target_rule = 'types|' . $req_type; |
||
129 | } |
||
130 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by MISSING rule for page type <' . $req_type . '> on path <' . $req_path . '>, new rule target is <' . $target_rule . '>', 3 ); |
||
131 | } else { |
||
132 | // Or just log a job qualified by a matching rule. |
||
133 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by AUTO rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 ); |
||
134 | } |
||
135 | |||
136 | // Submit job. |
||
137 | if ( $job_qualify ) { |
||
138 | if ( ! array_key_exists( $req_path, $ao_ccss_queue ) ) { |
||
139 | // This is a NEW job |
||
140 | // Merge job into the queue. |
||
141 | $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job( |
||
142 | $req_path, |
||
143 | $target_rule, |
||
144 | $req_type, |
||
145 | $hash, |
||
146 | null, |
||
147 | null, |
||
148 | null, |
||
149 | null, |
||
150 | true |
||
151 | ); |
||
152 | // Set update flag. |
||
153 | $queue_update = true; |
||
154 | } else { |
||
155 | // This is an existing job |
||
156 | // The job is still NEW, most likely this is extra CSS file for the same page that needs a hash. |
||
157 | if ( 'NEW' == $ao_ccss_queue[ $req_path ]['jqstat'] ) { |
||
158 | // Add hash if it's not already in the job. |
||
159 | if ( ! in_array( $hash, $ao_ccss_queue[ $req_path ]['hashes'] ) ) { |
||
160 | // Push new hash to its array and update flag. |
||
161 | $queue_update = array_push( $ao_ccss_queue[ $req_path ]['hashes'], $hash ); |
||
162 | |||
163 | // Log job update. |
||
164 | autoptimizeCriticalCSSCore::ao_ccss_log( 'Hashes UPDATED on local job id <' . $ao_ccss_queue[ $req_path ]['ljid'] . '>, job status NEW, target rule <' . $ao_ccss_queue[ $req_path ]['rtarget'] . '>, hash added: ' . $hash, 3 ); |
||
165 | |||
166 | // Return from here as the hash array is already updated. |
||
167 | return true; |
||
168 | } |
||
169 | } elseif ( 'NEW' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_QUEUED' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_ONGOING' != $ao_ccss_queue[ $req_path ]['jqstat'] ) { |
||
170 | // Allow requeuing jobs that are not NEW, JOB_QUEUED or JOB_ONGOING |
||
171 | // Merge new job keeping some previous job values. |
||
172 | $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job( |
||
173 | $req_path, |
||
174 | $target_rule, |
||
175 | $req_type, |
||
176 | $hash, |
||
177 | $ao_ccss_queue[ $req_path ]['file'], |
||
178 | $ao_ccss_queue[ $req_path ]['jid'], |
||
179 | $ao_ccss_queue[ $req_path ]['jrstat'], |
||
180 | $ao_ccss_queue[ $req_path ]['jvstat'], |
||
181 | false |
||
182 | ); |
||
183 | // Set update flag. |
||
184 | $queue_update = true; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | if ( $queue_update ) { |
||
189 | // Persist the job to the queue and return. |
||
190 | $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
||
191 | update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
||
192 | return true; |
||
193 | } else { |
||
194 | // Or just return false if no job was added. |
||
195 | autoptimizeCriticalCSSCore::ao_ccss_log( 'A job for path <' . $req_path . '> already exist with NEW or PENDING status, skipping job creation', 3 ); |
||
196 | return false; |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
309 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.