LocalesAreInstalled   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 87
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B check() 0 31 6
A message() 0 10 2
A __construct() 0 4 1
A name() 0 4 1
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use BeyondCode\SelfDiagnosis\SystemFunctions;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Collection;
8
9
class LocalesAreInstalled implements Check
10
{
11
    /** @var Collection */
12
    protected $missingLocales;
13
14
    /** @var string|null */
15
    protected $message;
16
17
    /** @var SystemFunctions */
18
    protected $systemFunctions;
19
20
    /**
21
     * LocalesAreInstalled constructor.
22
     *
23
     * @param SystemFunctions $systemFunctions
24
     */
25 24
    public function __construct(SystemFunctions $systemFunctions)
26
    {
27 24
        $this->systemFunctions = $systemFunctions;
28 24
    }
29
30
    /**
31
     * The name of the check.
32
     *
33
     * @param array $config
34
     * @return string
35
     */
36
    public function name(array $config): string
37
    {
38
        return trans('self-diagnosis::checks.locales_are_installed.name');
39
    }
40
41
    /**
42
     * Perform the actual verification of this check.
43
     *
44
     * @param array $config
45
     * @return bool
46
     */
47 24
    public function check(array $config): bool
48
    {
49 24
        $this->missingLocales = new Collection(Arr::get($config, 'required_locales', []));
50 24
        if ($this->missingLocales->isEmpty()) {
51 4
            return true;
52
        }
53
54 20
        if (!$this->systemFunctions->isFunctionAvailable('shell_exec')) {
55 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...
56 4
            return false;
57
        }
58
59 16
        if ($this->systemFunctions->isWindowsOperatingSystem()) {
60 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...
61 4
            return false;
62
        }
63
64 12
        $locales = $this->systemFunctions->callShellExec('locale -a');
65 12
        if ($locales === null || $locales === '') {
66 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...
67 4
            return false;
68
        }
69
70 8
        $locales = explode("\n" , $locales);
71
72 4
        $this->missingLocales = $this->missingLocales->reject(function ($loc) use ($locales) {
73 8
            return in_array($loc, $locales);
74 8
        });
75
76 8
        return $this->missingLocales->isEmpty();
77
    }
78
79
    /**
80
     * The error message to display in case the check does not pass.
81
     *
82
     * @param array $config
83
     * @return string
84
     */
85 16
    public function message(array $config): string
86
    {
87 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...
88 12
            return $this->message;
89
        }
90
91 4
        return trans('self-diagnosis::checks.locales_are_installed.message.missing_locales', [
92 4
            'locales' => $this->missingLocales->implode(PHP_EOL),
93
        ]);
94
    }
95
}
96