Completed
Pull Request — master (#74)
by
unknown
06:53
created

LocalesAreInstalled   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 87
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A name() 0 4 1
A message() 0 10 2
B check() 0 31 6
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use BeyondCode\SelfDiagnosis\SystemFunctions;
6
use Illuminate\Support\Collection;
7
8
class LocalesAreInstalled implements Check
9
{
10
    /** @var Collection */
11
    protected $missingLocales;
12
13
    /** @var string|null */
14
    protected $message;
15
16
    /** @var SystemFunctions */
17
    protected $systemFunctions;
18
19
    /**
20
     * LocalesAreInstalled constructor.
21
     *
22
     * @param SystemFunctions $systemFunctions
23
     */
24 24
    public function __construct(SystemFunctions $systemFunctions)
25
    {
26 24
        $this->systemFunctions = $systemFunctions;
27 24
    }
28
29
    /**
30
     * The name of the check.
31
     *
32
     * @param array $config
33
     * @return string
34
     */
35
    public function name(array $config): string
36
    {
37
        return trans('self-diagnosis::checks.locales_are_installed.name');
38
    }
39
40
    /**
41
     * Perform the actual verification of this check.
42
     *
43
     * @param array $config
44
     * @return bool
45
     */
46 24
    public function check(array $config): bool
47
    {
48 24
        $this->missingLocales = new Collection(array_get($config, 'required_locales', []));
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated with message: Arr::get() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
49 24
        if ($this->missingLocales->isEmpty()) {
50 4
            return true;
51
        }
52
53 20
        if (!$this->systemFunctions->isFunctionAvailable('shell_exec')) {
54 4
            $this->message = trans('self-diagnosis::checks.locales_are_installed.message.shell_exec_not_available');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('self-diagnosis::c...ll_exec_not_available') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $message is declared as type string|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55 4
            return false;
56
        }
57
58 16
        if ($this->systemFunctions->isWindowsOperatingSystem()) {
59 4
            $this->message = trans('self-diagnosis::checks.locales_are_installed.message.cannot_run_on_windows');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('self-diagnosis::c...cannot_run_on_windows') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $message is declared as type string|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60 4
            return false;
61
        }
62
63 12
        $locales = $this->systemFunctions->callShellExec('locale -a');
64 12
        if ($locales === null || $locales === '') {
65 4
            $this->message = trans('self-diagnosis::checks.locales_are_installed.message.locale_command_not_available');
0 ignored issues
show
Documentation Bug introduced by
It seems like trans('self-diagnosis::c...command_not_available') can also be of type object<Illuminate\Contra...Translation\Translator> or array. However, the property $message is declared as type string|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
66 4
            return false;
67
        }
68
69 8
        $locales = explode("\n" , $locales);
70
71
        $this->missingLocales = $this->missingLocales->reject(function ($loc) use ($locales) {
72 8
            return in_array($loc, $locales);
73 8
        });
74
75 8
        return $this->missingLocales->isEmpty();
76
    }
77
78
    /**
79
     * The error message to display in case the check does not pass.
80
     *
81
     * @param array $config
82
     * @return string
83
     */
84 16
    public function message(array $config): string
85
    {
86 16
        if ($this->message) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->message of type string|null is loosely compared to true; 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...
87 12
            return $this->message;
88
        }
89
90 4
        return trans('self-diagnosis::checks.locales_are_installed.message.missing_locales', [
91 4
            'locales' => $this->missingLocales->implode(PHP_EOL),
92
        ]);
93
    }
94
}
95