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

TranslatedRouteCommandContext   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 14.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 9
loc 62
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isSupportedLocale() 0 4 1
A getSupportedLocales() 0 4 1
A getI18nService() 0 4 1
A getBootstrapPath() 0 7 2
A makeLocaleRoutesPath() 9 10 2
A getLocaleEnvKey() 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 RichanFongdasen\I18n\I18nService;
6
7
trait TranslatedRouteCommandContext
8
{
9
    /**
10
     * Returns whether a given locale is supported.
11
     *
12
     * @param string $locale
13
     * 
14
     * @return bool
15
     */
16
    protected function isSupportedLocale($locale)
17
    {
18
        return $this->getI18nService()->getLocale($locale) !== null;
19
    }
20
21
    /**
22
     * @return string[]
23
     */
24
    protected function getSupportedLocales()
25
    {
26
        return $this->getI18nService()->getLocale()->toArray();
27
    }
28
29
    /**
30
     * @return \RichanFongdasen\I18n\I18nService
31
     */
32
    protected function getI18nService()
33
    {
34
        return app(I18nService::class);
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    protected function getBootstrapPath()
41
    {
42
        if (method_exists($this->laravel, 'bootstrapPath')) {
43
            return $this->laravel->bootstrapPath();
0 ignored issues
show
Bug introduced by
The property laravel 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...
44
        }
45
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'bootstrap';
46
    }
47
48
    /**
49
     * @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...
50
     * 
51
     * @return string
52
     */
53 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...
54
    {
55
        $path = $this->laravel->getCachedRoutesPath();
56
        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...
57
            return $path;
58
        }
59
        $path = substr($path, 0, -4).'_'.$locale.'.php';
60
61
        return $path;
62
    }
63
64
    protected function getLocaleEnvKey()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
65
    {
66
        return I18nService::ENV_ROUTE_KEY;
67
    }
68
}
69