Completed
Pull Request — master (#16)
by
unknown
01:12
created

LoadsTranslatedCachedRoutes   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 13.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 9
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadCachedRoutes() 0 24 2
A makeLocaleRoutesPath() 9 9 2
A getDefaultCachedRoutePath() 0 4 1
A getI18nService() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace RichanFongdasen\I18n\Traits;
4
5
use Illuminate\Support\Facades\Log;
6
use RichanFongdasen\I18n\I18nService;
7
8
/**
9
 * LoadsTranslatedCachedRoutes.
10
 *
11
 * Add this trait to your App\RouteServiceProvider to load
12
 * translated cached routes for the active locale, instead
13
 * of the default locale's routes (irrespective of active).
14
 */
15
trait LoadsTranslatedCachedRoutes
16
{
17
    /**
18
     * Load the cached routes for the application.
19
     *
20
     * @return void
21
     */
22
    protected function loadCachedRoutes()
23
    {
24
        $localization = $this->getI18nService();
25
26
        $locale = $localization->routePrefix();
27
28
        // First, try to load the routes specifically cached for this locale
29
        // if they do not exist, write a warning to the log and load the default
30
        // routes instead. Note that this is guaranteed to exist, because the
31
        // 'cached routes' check in the Application checks its existence.
32
33
        $path = $this->makeLocaleRoutesPath($locale);
34
35
        if (!file_exists($path)) {
36
37
            Log::warning("Routes cached, but no cached routes found for locale '{$locale}'!");
38
39
            $path = $this->getDefaultCachedRoutePath();
40
        }
41
42
        $this->app->booted(function () use ($path) {
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
            require $path;
44
        });
45
    }
46
47
    /**
48
     * Returns the path to the cached routes file for a given locale.
49
     *
50
     * @param string   $locale
0 ignored issues
show
Documentation introduced by
Should the type for parameter $locale not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
51
     * @param string[] $localeKeys
0 ignored issues
show
Documentation introduced by
There is no parameter named $localeKeys. Did you maybe mean $locale?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
52
     * 
53
     * @return string
54
     */
55 View Code Duplication
    protected function makeLocaleRoutesPath($locale = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $path = $this->getDefaultCachedRoutePath();
58
        if (!$locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
59
            return $path;
60
        }
61
62
        return substr($path, 0, -4).'_'.$locale.'.php';
63
    }
64
65
    /**
66
     * Returns the path to the standard cached routes file.
67
     *
68
     * @return string
69
     */
70
    protected function getDefaultCachedRoutePath()
71
    {
72
        return $this->app->getCachedRoutesPath();
73
    }
74
75
    /**
76
     * @return \RichanFongdasen\I18n\I18nService
77
     */
78
    protected function getI18nService()
79
    {
80
        return app(I18nService::class);
81
    }
82
}
83