Passed
Push — develop ( 18dfd4...a5b7fe )
by Andrew
05:21 queued 11s
created
src/services/Redirects.php 1 patch
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
                 Craft::t(
129 129
                     'retour',
130 130
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
131
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
131
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
132 132
                 ),
133 133
                 __METHOD__
134 134
             );
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
         $response = Craft::$app->getResponse();
163 163
         if ($redirect !== null) {
164 164
             // Figure out what type of source matching was done
165
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
165
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
166 166
             switch ($redirectSrcMatch) {
167 167
                 case 'pathonly':
168 168
                     $url = $pathOnly;
@@ -174,11 +174,11 @@  discard block
 block discarded – undo
174 174
                     $url = $pathOnly;
175 175
                     break;
176 176
             }
177
-            $dest = $redirect['redirectDestUrl'];
177
+            $dest = $redirect[ 'redirectDestUrl' ];
178 178
             // If this isn't a full URL, make it one based on the appropriate site
179 179
             if (!UrlHelper::isFullUrl($dest)) {
180 180
                 try {
181
-                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect['siteId'] ?? null);
181
+                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect[ 'siteId' ] ?? null);
182 182
                 } catch (\yii\base\Exception $e) {
183 183
                 }
184 184
             }
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
                     $dest .= '?' . $request->getQueryStringWithoutPath();
189 189
                 }
190 190
             }
191
-            $status = $redirect['redirectHttpCode'];
191
+            $status = $redirect[ 'redirectHttpCode' ];
192 192
             Craft::info(
193 193
                 Craft::t(
194 194
                     'retour',
195 195
                     'Redirecting {url} to {dest} with status {status}',
196
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
196
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
197 197
                 ),
198 198
                 __METHOD__
199 199
             );
@@ -273,13 +273,13 @@  discard block
 block discarded – undo
273 273
     public function getRedirectFromCache($url, int $siteId = 0)
274 274
     {
275 275
         $cache = Craft::$app->getCache();
276
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
276
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
277 277
         $redirect = $cache->get($cacheKey);
278 278
         Craft::info(
279 279
             Craft::t(
280 280
                 'retour',
281 281
                 'Cached redirect hit for {url}',
282
-                ['url' => $url]
282
+                [ 'url' => $url ]
283 283
             ),
284 284
             __METHOD__
285 285
         );
@@ -301,12 +301,12 @@  discard block
 block discarded – undo
301 301
         } catch (SiteNotFoundException $e) {
302 302
             $siteId = 1;
303 303
         }
304
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
304
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
305 305
         // Create the dependency tags
306 306
         $dependency = new TagDependency([
307 307
             'tags' => [
308 308
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
309
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
309
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
310 310
             ],
311 311
         ]);
312 312
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
             Craft::t(
315 315
                 'retour',
316 316
                 'Cached redirect saved for {url}',
317
-                ['url' => $url]
317
+                [ 'url' => $url ]
318 318
             ),
319 319
             __METHOD__
320 320
         );
@@ -332,8 +332,8 @@  discard block
 block discarded – undo
332 332
         $result = null;
333 333
         foreach ($redirects as $redirect) {
334 334
             // Figure out what type of source matching to do
335
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
336
-            $redirectEnabled = (bool)$redirect['enabled'];
335
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
336
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
337 337
             if ($redirectEnabled === true) {
338 338
                 switch ($redirectSrcMatch) {
339 339
                     case 'pathonly':
@@ -346,11 +346,11 @@  discard block
 block discarded – undo
346 346
                         $url = $pathOnly;
347 347
                         break;
348 348
                 }
349
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
349
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
350 350
                 switch ($redirectMatchType) {
351 351
                     // Do a straight up match
352 352
                     case 'exactmatch':
353
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
353
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
354 354
                             $this->incrementRedirectHitCount($redirect);
355 355
                             $this->saveRedirectToCache($url, $redirect);
356 356
 
@@ -360,14 +360,14 @@  discard block
 block discarded – undo
360 360
 
361 361
                     // Do a regex match
362 362
                     case 'regexmatch':
363
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
363
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
364 364
                         if (preg_match($matchRegEx, $url) === 1) {
365 365
                             $this->incrementRedirectHitCount($redirect);
366 366
                             // If we're not associated with an EntryID, handle capture group replacement
367
-                            if ((int)$redirect['associatedElementId'] === 0) {
368
-                                $redirect['redirectDestUrl'] = preg_replace(
367
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
368
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
369 369
                                     $matchRegEx,
370
-                                    $redirect['redirectDestUrl'],
370
+                                    $redirect[ 'redirectDestUrl' ],
371 371
                                     $url
372 372
                                 );
373 373
                             }
@@ -386,7 +386,7 @@  discard block
 block discarded – undo
386 386
                                     'redirect' => &$redirect,
387 387
                                 ],
388 388
                             ];
389
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
389
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
390 390
                             if ($result) {
391 391
                                 $this->incrementRedirectHitCount($redirect);
392 392
                                 $this->saveRedirectToCache($url, $redirect);
@@ -402,7 +402,7 @@  discard block
 block discarded – undo
402 402
             Craft::t(
403 403
                 'retour',
404 404
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
405
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
405
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
406 406
             ),
407 407
             __METHOD__
408 408
         );
@@ -426,7 +426,7 @@  discard block
 block discarded – undo
426 426
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
427 427
             /** @var Plugin $plugin */
428 428
             if (method_exists($plugin, 'retourMatch')) {
429
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
429
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
430 430
             }
431 431
         }
432 432
 
@@ -447,12 +447,12 @@  discard block
 block discarded – undo
447 447
         }
448 448
         // Query the db table
449 449
         $query = (new Query())
450
-            ->from(['{{%retour_static_redirects}}'])
450
+            ->from([ '{{%retour_static_redirects}}' ])
451 451
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
452 452
         if ($siteId) {
453 453
             $query
454
-                ->where(['siteId' => $siteId])
455
-                ->orWhere(['siteId' => null]);
454
+                ->where([ 'siteId' => $siteId ])
455
+                ->orWhere([ 'siteId' => null ]);
456 456
         }
457 457
         if ($limit) {
458 458
             $query->limit($limit);
@@ -475,8 +475,8 @@  discard block
 block discarded – undo
475 475
     {
476 476
         // Query the db table
477 477
         $redirect = (new Query())
478
-            ->from(['{{%retour_static_redirects}}'])
479
-            ->where(['id' => $id])
478
+            ->from([ '{{%retour_static_redirects}}' ])
479
+            ->where([ 'id' => $id ])
480 480
             ->one();
481 481
 
482 482
         return $redirect;
@@ -494,16 +494,16 @@  discard block
 block discarded – undo
494 494
     {
495 495
         // Query the db table
496 496
         $query = (new Query())
497
-            ->from(['{{%retour_static_redirects}}'])
498
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
497
+            ->from([ '{{%retour_static_redirects}}' ])
498
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
499 499
             ;
500 500
         if ($siteId) {
501 501
             $query
502
-                ->andWhere(['or', [
502
+                ->andWhere([ 'or', [
503 503
                     'siteId' => $siteId,
504 504
                 ], [
505 505
                     'siteId' => null,
506
-                ]]);
506
+                ] ]);
507 507
         }
508 508
         $redirect = $query->one();
509 509
 
@@ -545,13 +545,13 @@  discard block
 block discarded – undo
545 545
     {
546 546
         if ($redirectConfig !== null) {
547 547
             $db = Craft::$app->getDb();
548
-            $redirectConfig['hitCount']++;
549
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
548
+            $redirectConfig[ 'hitCount' ]++;
549
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
550 550
             Craft::debug(
551 551
                 Craft::t(
552 552
                     'retour',
553 553
                     'Incrementing statistics for: {redirect}',
554
-                    ['redirect' => print_r($redirectConfig, true)]
554
+                    [ 'redirect' => print_r($redirectConfig, true) ]
555 555
                 ),
556 556
                 __METHOD__
557 557
             );
@@ -560,14 +560,14 @@  discard block
 block discarded – undo
560 560
                 $rowsAffected = $db->createCommand()->update(
561 561
                     '{{%retour_static_redirects}}',
562 562
                     [
563
-                        'hitCount' => $redirectConfig['hitCount'],
564
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
563
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
564
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
565 565
                     ],
566 566
                     [
567
-                        'id' => $redirectConfig['id'],
567
+                        'id' => $redirectConfig[ 'id' ],
568 568
                     ]
569 569
                 )->execute();
570
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
570
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
571 571
             } catch (Exception $e) {
572 572
                 Craft::error($e->getMessage(), __METHOD__);
573 573
             }
@@ -586,7 +586,7 @@  discard block
 block discarded – undo
586 586
                 Craft::t(
587 587
                     'retour',
588 588
                     'Error validating redirect {id}: {errors}',
589
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
589
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
590 590
                 ),
591 591
                 __METHOD__
592 592
             );
@@ -596,32 +596,32 @@  discard block
 block discarded – undo
596 596
         // Get the validated model attributes and save them to the db
597 597
         $redirectConfig = $redirect->getAttributes();
598 598
         // 0 for a siteId needs to be converted to null
599
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
600
-            $redirectConfig['siteId'] = null;
599
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
600
+            $redirectConfig[ 'siteId' ] = null;
601 601
         }
602 602
         // Throw an event to before saving the redirect
603 603
         $db = Craft::$app->getDb();
604 604
         // See if a redirect exists with this source URL already
605
-        if ((int)$redirectConfig['id'] === 0) {
605
+        if ((int) $redirectConfig[ 'id' ] === 0) {
606 606
             // Query the db table
607 607
             $redirect = (new Query())
608
-                ->from(['{{%retour_static_redirects}}'])
609
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
610
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
608
+                ->from([ '{{%retour_static_redirects}}' ])
609
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
610
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
611 611
                 ->one();
612 612
             // If it exists, update it rather than having duplicates
613 613
             if (!empty($redirect)) {
614
-                $redirectConfig['id'] = $redirect['id'];
614
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
615 615
             }
616 616
         }
617 617
         // Trigger a 'beforeSaveRedirect' event
618
-        $isNew = (int)$redirectConfig['id'] === 0;
618
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
619 619
         $event = new RedirectEvent([
620 620
             'isNew' => $isNew,
621
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
622
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
623
-            'matchType' => $redirectConfig['redirectSrcMatch'],
624
-            'redirectType' => $redirectConfig['redirectHttpCode'],
621
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
622
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
623
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
624
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
625 625
         ]);
626 626
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
627 627
         if (!$event->isValid) {
@@ -633,7 +633,7 @@  discard block
 block discarded – undo
633 633
                 Craft::t(
634 634
                     'retour',
635 635
                     'Updating existing redirect: {redirect}',
636
-                    ['redirect' => print_r($redirectConfig, true)]
636
+                    [ 'redirect' => print_r($redirectConfig, true) ]
637 637
                 ),
638 638
                 __METHOD__
639 639
             );
@@ -643,7 +643,7 @@  discard block
 block discarded – undo
643 643
                     '{{%retour_static_redirects}}',
644 644
                     $redirectConfig,
645 645
                     [
646
-                        'id' => $redirectConfig['id'],
646
+                        'id' => $redirectConfig[ 'id' ],
647 647
                     ]
648 648
                 )->execute();
649 649
             } catch (Exception $e) {
@@ -654,11 +654,11 @@  discard block
 block discarded – undo
654 654
                 Craft::t(
655 655
                     'retour',
656 656
                     'Creating new redirect: {redirect}',
657
-                    ['redirect' => print_r($redirectConfig, true)]
657
+                    [ 'redirect' => print_r($redirectConfig, true) ]
658 658
                 ),
659 659
                 __METHOD__
660 660
             );
661
-            unset($redirectConfig['id']);
661
+            unset($redirectConfig[ 'id' ]);
662 662
             // Create a new record
663 663
             try {
664 664
                 $db->createCommand()->insert(
@@ -671,15 +671,15 @@  discard block
 block discarded – undo
671 671
         }
672 672
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
673 673
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
674
-            $redirectConfig['redirectDestUrl'],
675
-            $redirectConfig['siteId']
674
+            $redirectConfig[ 'redirectDestUrl' ],
675
+            $redirectConfig[ 'siteId' ]
676 676
         );
677 677
         if ($testRedirectConfig !== null) {
678 678
             Craft::debug(
679 679
                 Craft::t(
680 680
                     'retour',
681 681
                     'Deleting redirect to prevent a loop: {redirect}',
682
-                    ['redirect' => print_r($testRedirectConfig, true)]
682
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
683 683
                 ),
684 684
                 __METHOD__
685 685
             );
@@ -687,7 +687,7 @@  discard block
 block discarded – undo
687 687
             try {
688 688
                 $db->createCommand()->delete(
689 689
                     '{{%retour_static_redirects}}',
690
-                    ['id' => $testRedirectConfig['id']]
690
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
691 691
                 )->execute();
692 692
             } catch (Exception $e) {
693 693
                 Craft::error($e->getMessage(), __METHOD__);
@@ -720,10 +720,10 @@  discard block
 block discarded – undo
720 720
      */
721 721
     public function excludeUri($uri): bool
722 722
     {
723
-        $uri = '/'.ltrim($uri, '/');
723
+        $uri = '/' . ltrim($uri, '/');
724 724
         if (!empty(Retour::$settings->excludePatterns)) {
725 725
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
726
-                $pattern = '`'.$excludePattern['pattern'].'`i';
726
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
727 727
                 if (preg_match($pattern, $uri) === 1) {
728 728
                     return true;
729 729
                 }
Please login to merge, or discard this patch.