Passed
Push — master ( 425aaa...cf215e )
by zyt
02:31
created
src/GoogleFontsOptimizer.php 1 patch
Spacing   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -13,9 +13,9 @@  discard block
 block discarded – undo
13 13
     const FILTER_OB_CLEANER  = 'zwf_gfo_clean_ob';
14 14
     const DEFAULT_OB_CLEANER = false;
15 15
 
16
-    protected $candidates = [];
16
+    protected $candidates = [ ];
17 17
 
18
-    protected $enqueued = [];
18
+    protected $enqueued = [ ];
19 19
 
20 20
     /**
21 21
      * Like the wind.
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
                  * Scan and optimize requests found in the markup (uses more
34 34
                  * memory but works on [almost] any theme)
35 35
                  */
36
-                add_action('template_redirect', [$this, 'startBuffering'], 11);
36
+                add_action('template_redirect', [ $this, 'startBuffering' ], 11);
37 37
                 break;
38 38
 
39 39
             case self::DEFAULT_OPERATION_MODE:
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
                  * Scan only things added via wp_enqueue_style (uses slightly
43 43
                  * less memory usually, but requires a decently coded theme)
44 44
                  */
45
-                add_filter('print_styles_array', [$this, 'processStylesHandles']);
45
+                add_filter('print_styles_array', [ $this, 'processStylesHandles' ]);
46 46
                 break;
47 47
         }
48 48
     }
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
      *
55 55
      * @return bool
56 56
      */
57
-    protected function hasEnoughElements(array $candidates = [])
57
+    protected function hasEnoughElements(array $candidates = [ ])
58 58
     {
59 59
         $enough = true;
60 60
 
@@ -82,25 +82,25 @@  discard block
 block discarded – undo
82 82
         $candidate_handles = $this->findCandidateHandles($handles);
83 83
 
84 84
         // Bail if we don't have anything that makes sense for us to continue
85
-        if (! $this->hasEnoughElements($candidate_handles)) {
85
+        if (!$this->hasEnoughElements($candidate_handles)) {
86 86
             return $handles;
87 87
         }
88 88
 
89 89
         $fonts_array = $this->getFontsArray($this->getCandidates());
90
-        if (isset($fonts_array['complete'])) {
90
+        if (isset($fonts_array[ 'complete' ])) {
91 91
             $combined_font_url = $this->buildGoogleFontsUrlFromFontsArray($fonts_array);
92 92
             $handle_name       = 'zwf-gfo-combined';
93 93
             $this->enqueueStyle($handle_name, $combined_font_url);
94
-            $handles[] = $handle_name;
94
+            $handles[ ] = $handle_name;
95 95
         }
96 96
 
97
-        if (isset($fonts_array['partial'])) {
97
+        if (isset($fonts_array[ 'partial' ])) {
98 98
             $cnt = 0;
99
-            foreach ($fonts_array['partial']['url'] as $url) {
99
+            foreach ($fonts_array[ 'partial' ][ 'url' ] as $url) {
100 100
                 $cnt++;
101 101
                 $handle_name = 'zwf-gfo-combined-txt-' . $cnt;
102 102
                 $this->enqueueStyle($handle_name, $url);
103
-                $handles[] = $handle_name;
103
+                $handles[ ] = $handle_name;
104 104
             }
105 105
         }
106 106
 
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
     public function findCandidateHandles(array $handles)
129 129
     {
130 130
         $handler           = /** @scrutinizer ignore-call */ \wp_styles();
131
-        $candidate_handles = [];
131
+        $candidate_handles = [ ];
132 132
 
133 133
         foreach ($handles as $handle) {
134 134
             // $url = $handler->registered[ $handle ]->src;
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
                 $url = $dep->src;
138 138
                 if ($this->isGoogleWebFontUrl($url)) {
139 139
                     $this->addCandidate($url);
140
-                    $candidate_handles[$handle] = $url;
140
+                    $candidate_handles[ $handle ] = $url;
141 141
                 }
142 142
             }
143 143
         }
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
                 \wp_dequeue_style($handle);
166 166
             }
167 167
             // @codeCoverageIgnoreEnd
168
-            unset($this->enqueued[$handle]);
168
+            unset($this->enqueued[ $handle ]);
169 169
         }
170 170
     }
171 171
 
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
      *
181 181
      * @return void
182 182
      */
183
-    public function enqueueStyle($handle, $url, $deps = [], $version = null)
183
+    public function enqueueStyle($handle, $url, $deps = [ ], $version = null)
184 184
     {
185 185
         // @codeCoverageIgnoreStart
186 186
         // \wp_register_style($handle, $url, $deps, $version);
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
         }
191 191
         // @codeCoverageIgnoreEnd
192 192
 
193
-        $this->enqueued[$handle] = $url;
193
+        $this->enqueued[ $handle ] = $url;
194 194
     }
195 195
 
196 196
     /**
@@ -204,8 +204,8 @@  discard block
 block discarded – undo
204 204
     {
205 205
         $data = $this->enqueued;
206 206
 
207
-        if (null !== $handle && isset($this->enqueued[$handle])) {
208
-            $data = $this->enqueued[$handle];
207
+        if (null !== $handle && isset($this->enqueued[ $handle ])) {
208
+            $data = $this->enqueued[ $handle ];
209 209
         }
210 210
 
211 211
         return $data;
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
             $rel  = null;
234 234
             $type = null;
235 235
             $href = null;
236
-            $find = ['rel', 'type', 'href'];
236
+            $find = [ 'rel', 'type', 'href' ];
237 237
             foreach ($find as $attr) {
238 238
                 if ($link_node->hasAttribute($attr)) {
239 239
                     $$attr = $link_node->getAttribute($attr);
@@ -249,14 +249,14 @@  discard block
 block discarded – undo
249 249
         $candidates = $this->getCandidates();
250 250
 
251 251
         // Bail and return original markup unmodified if we don't have things to do
252
-        if (! $this->hasEnoughElements($candidates)) {
252
+        if (!$this->hasEnoughElements($candidates)) {
253 253
             return $markup;
254 254
         }
255 255
 
256 256
         // Process what we found and modify original markup with our replacement
257 257
         $fonts_array = $this->getFontsArray($candidates);
258 258
         $font_markup = $this->buildFontsMarkup($fonts_array);
259
-        $markup      = $this->modifyMarkup($markup, $font_markup, $fonts_array['links']);
259
+        $markup      = $this->modifyMarkup($markup, $font_markup, $fonts_array[ 'links' ]);
260 260
 
261 261
         return $markup;
262 262
     }
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
             }*/
309 309
 
310 310
             // Start our own buffering
311
-            $started = ob_start([$this, 'endBuffering']);
311
+            $started = ob_start([ $this, 'endBuffering' ]);
312 312
         }
313 313
 
314 314
         return $started;
@@ -317,7 +317,7 @@  discard block
 block discarded – undo
317 317
     public function endBuffering($markup)
318 318
     {
319 319
         // Bail early on things we don't want to parse
320
-        if (! $this->isMarkupDoable($markup)) {
320
+        if (!$this->isMarkupDoable($markup)) {
321 321
             return $markup;
322 322
         }
323 323
 
@@ -402,7 +402,7 @@  discard block
 block discarded – undo
402 402
             $is_amp_markup = $this->isAmpMarkup($content);
403 403
         }
404 404
 
405
-        if ($has_no_html_tag && ! $has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet) {
405
+        if ($has_no_html_tag && !$has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet) {
406 406
             $doable = false;
407 407
         }
408 408
 
@@ -435,7 +435,7 @@  discard block
 block discarded – undo
435 435
     /**
436 436
      * @param array $urls
437 437
      */
438
-    public function setCandidates(array $urls = [])
438
+    public function setCandidates(array $urls = [ ])
439 439
     {
440 440
         $this->candidates = $urls;
441 441
     }
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
      */
446 446
     public function addCandidate($url)
447 447
     {
448
-        $this->candidates[] = $url;
448
+        $this->candidates[ ] = $url;
449 449
     }
450 450
 
451 451
     /**
@@ -487,28 +487,28 @@  discard block
 block discarded – undo
487 487
      *
488 488
      * @return null|string
489 489
      */
490
-    public function buildGoogleFontsUrl(array $fonts, $subsets = [])
490
+    public function buildGoogleFontsUrl(array $fonts, $subsets = [ ])
491 491
     {
492 492
         $base_url  = 'https://fonts.googleapis.com/css';
493
-        $font_args = [];
494
-        $family    = [];
493
+        $font_args = [ ];
494
+        $family    = [ ];
495 495
 
496 496
         foreach ($fonts as $font_name => $font_weight) {
497 497
             if (is_array($font_weight)) {
498 498
                 $font_weight = implode(',', $font_weight);
499 499
             }
500 500
             // Trimming end colon handles edge case of being given an empty $font_weight
501
-            $family[] = trim(trim($font_name) . ':' . trim($font_weight), ':');
501
+            $family[ ] = trim(trim($font_name) . ':' . trim($font_weight), ':');
502 502
         }
503 503
 
504
-        $font_args['family'] = implode('|', $family);
504
+        $font_args[ 'family' ] = implode('|', $family);
505 505
 
506
-        if (! empty($subsets)) {
506
+        if (!empty($subsets)) {
507 507
             if (is_array($subsets)) {
508 508
                 $subsets = array_unique($subsets);
509 509
                 $subsets = implode(',', $subsets);
510 510
             }
511
-            $font_args['subset'] = trim($subsets);
511
+            $font_args[ 'subset' ] = trim($subsets);
512 512
         }
513 513
 
514 514
         $url = $base_url . '?' . http_build_query($font_args);
@@ -524,26 +524,26 @@  discard block
 block discarded – undo
524 524
      *
525 525
      * @return array
526 526
      */
527
-    protected function getFontsArray(array $candidates = [])
527
+    protected function getFontsArray(array $candidates = [ ])
528 528
     {
529
-        $fonts = [];
529
+        $fonts = [ ];
530 530
 
531 531
         foreach ($candidates as $candidate) {
532
-            $fonts['links'][] = $candidate;
532
+            $fonts[ 'links' ][ ] = $candidate;
533 533
 
534
-            $params = [];
534
+            $params = [ ];
535 535
             parse_str(parse_url($candidate, PHP_URL_QUERY), $params);
536 536
 
537
-            if (isset($params['text'])) {
537
+            if (isset($params[ 'text' ])) {
538 538
                 // Fonts with character limitations are segregated into
539 539
                 // under 'partial' (when `text` query param is used)
540
-                $font_family                = explode(':', $params['family']);
541
-                $fonts['partial']['name'][] = $font_family[0];
542
-                $fonts['partial']['url'][]  = $this->httpsify($candidate);
540
+                $font_family                = explode(':', $params[ 'family' ]);
541
+                $fonts[ 'partial' ][ 'name' ][ ] = $font_family[ 0 ];
542
+                $fonts[ 'partial' ][ 'url' ][ ]  = $this->httpsify($candidate);
543 543
             } else {
544 544
                 $fontstrings = $this->buildFontStringsFromQueryParams($params);
545 545
                 foreach ($fontstrings as $font) {
546
-                    $fonts['complete'][] = $font;
546
+                    $fonts[ 'complete' ][ ] = $font;
547 547
                 }
548 548
             }
549 549
         }
@@ -562,29 +562,29 @@  discard block
 block discarded – undo
562 562
      */
563 563
     protected function buildFontStringsFromQueryParams(array $params)
564 564
     {
565
-        $fonts = [];
565
+        $fonts = [ ];
566 566
 
567
-        foreach (explode('|', $params['family']) as $families) {
567
+        foreach (explode('|', $params[ 'family' ]) as $families) {
568 568
             $font_family = explode(':', $families);
569 569
             $subset      = false;
570
-            if (isset($params['subset'])) {
570
+            if (isset($params[ 'subset' ])) {
571 571
                 // Use the found subset parameter
572
-                $subset = $params['subset'];
573
-            } elseif (isset($font_family[2])) {
572
+                $subset = $params[ 'subset' ];
573
+            } elseif (isset($font_family[ 2 ])) {
574 574
                 // Use the subset in the family string
575
-                $subset = $font_family[2];
575
+                $subset = $font_family[ 2 ];
576 576
             }
577 577
 
578 578
             // We need to have a name and a size
579
-            if (strlen($font_family[0]) > 0 && strlen($font_family[1]) > 0) {
579
+            if (strlen($font_family[ 0 ]) > 0 && strlen($font_family[ 1 ]) > 0) {
580 580
                 $parts = [
581
-                    $font_family[0],
582
-                    $font_family[1]
581
+                    $font_family[ 0 ],
582
+                    $font_family[ 1 ]
583 583
                 ];
584 584
                 if ($subset) {
585
-                    $parts[] = $subset;
585
+                    $parts[ ] = $subset;
586 586
                 }
587
-                $fonts[] = implode(':', $parts);
587
+                $fonts[ ] = implode(':', $parts);
588 588
             }
589 589
         }
590 590
 
@@ -618,37 +618,37 @@  discard block
 block discarded – undo
618 618
      */
619 619
     protected function consolidateFontsArray(array $fonts_array)
620 620
     {
621
-        $fonts         = [];
622
-        $subsets       = [];
623
-        $fonts_to_subs = [];
621
+        $fonts         = [ ];
622
+        $subsets       = [ ];
623
+        $fonts_to_subs = [ ];
624 624
 
625
-        foreach ($fonts_array['complete'] as $font_string) {
625
+        foreach ($fonts_array[ 'complete' ] as $font_string) {
626 626
             $parts = explode(':', $font_string);
627
-            $name  = $parts[0];
628
-            $size  = $parts[1];
627
+            $name  = $parts[ 0 ];
628
+            $size  = $parts[ 1 ];
629 629
 
630
-            if (isset($fonts[$name])) {
630
+            if (isset($fonts[ $name ])) {
631 631
                 // If a name already exists, append the new size
632
-                $fonts[$name] .= ',' . $size;
632
+                $fonts[ $name ] .= ',' . $size;
633 633
             } else {
634 634
                 // Create a new key for the name and size
635
-                $fonts[$name] = $size;
635
+                $fonts[ $name ] = $size;
636 636
             }
637 637
 
638 638
             // Check if subset is specified as the third element
639
-            if (isset($parts[2])) {
640
-                $subset = $parts[2];
639
+            if (isset($parts[ 2 ])) {
640
+                $subset = $parts[ 2 ];
641 641
                 // Collect all the subsets defined into a single array
642 642
                 $elements = explode(',', $subset);
643 643
                 foreach ($elements as $sub) {
644
-                    $subsets[] = $sub;
644
+                    $subsets[ ] = $sub;
645 645
                 }
646 646
                 // Keeping a separate map of names => requested subsets for
647 647
                 // webfontloader purposes
648
-                if (isset($fonts_to_subs[$name])) {
649
-                    $fonts_to_subs[$name] .= ',' . $subset;
648
+                if (isset($fonts_to_subs[ $name ])) {
649
+                    $fonts_to_subs[ $name ] .= ',' . $subset;
650 650
                 } else {
651
-                    $fonts_to_subs[$name] = $subset;
651
+                    $fonts_to_subs[ $name ] = $subset;
652 652
                 }
653 653
             }
654 654
         }
@@ -665,7 +665,7 @@  discard block
 block discarded – undo
665 665
         // Sanitize and de-dup $fonts_to_subs mapping
666 666
         $fonts_to_subs = $this->dedupValues($fonts_to_subs, false); // no sort
667 667
 
668
-        return [$fonts, $subsets, $fonts_to_subs];
668
+        return [ $fonts, $subsets, $fonts_to_subs ];
669 669
     }
670 670
 
671 671
     /**
@@ -690,7 +690,7 @@  discard block
 block discarded – undo
690 690
             if (false !== $sort) {
691 691
                 sort($parts, (int) $sort);
692 692
             }
693
-            $data[$key] = $parts;
693
+            $data[ $key ] = $parts;
694 694
         }
695 695
 
696 696
         return $data;
@@ -752,9 +752,9 @@  discard block
 block discarded – undo
752 752
             $href     = $this->encodeUnencodedAmpersands($font_url);
753 753
             $markup   = '<link rel="stylesheet" type="text/css" href="' . $href . '">';
754 754
 
755
-            if (isset($fonts_array['partial'])) {
756
-                if (is_array($fonts_array['partial']['url'])) {
757
-                    foreach ($fonts_array['partial']['url'] as $other) {
755
+            if (isset($fonts_array[ 'partial' ])) {
756
+                if (is_array($fonts_array[ 'partial' ][ 'url' ])) {
757
+                    foreach ($fonts_array[ 'partial' ][ 'url' ] as $other) {
758 758
                         $markup .= '<link rel="stylesheet" type="text/css"';
759 759
                         $markup .= ' href="' . $this->encodeUnencodedAmpersands($other) . '">';
760 760
                     }
@@ -762,24 +762,24 @@  discard block
 block discarded – undo
762 762
             }
763 763
         } else {
764 764
             // Bulding WebFont script loader
765
-            $families_array = [];
765
+            $families_array = [ ];
766 766
 
767 767
             list($fonts, $subsets, $mapping) = $this->consolidateFontsArray($fonts_array);
768 768
             foreach ($fonts as $name => $sizes) {
769 769
                 $family = $name . ':' . implode(',', $sizes);
770
-                if (isset($mapping[$name])) {
771
-                    $family .= ':' . implode(',', $mapping[$name]);
770
+                if (isset($mapping[ $name ])) {
771
+                    $family .= ':' . implode(',', $mapping[ $name ]);
772 772
                 }
773
-                $families_array[] = $family;
773
+                $families_array[ ] = $family;
774 774
             }
775 775
             $families = "'" . implode("', '", $families_array) . "'";
776 776
 
777 777
             // Load 'text' requests with the "custom" module
778 778
             $custom = '';
779
-            if (isset($fonts_array['partial'])) {
779
+            if (isset($fonts_array[ 'partial' ])) {
780 780
                 $custom  = ",\n    custom: {\n";
781
-                $custom .= "        families: [ '" . implode("', '", $fonts_array['partial']['name']) . "' ],\n";
782
-                $custom .= "        urls: [ '" . implode("', '", $fonts_array['partial']['url']) . "' ]\n";
781
+                $custom .= "        families: [ '" . implode("', '", $fonts_array[ 'partial' ][ 'name' ]) . "' ],\n";
782
+                $custom .= "        urls: [ '" . implode("', '", $fonts_array[ 'partial' ][ 'url' ]) . "' ]\n";
783 783
                 $custom .= '    }';
784 784
             }
785 785
 
Please login to merge, or discard this patch.