Passed
Pull Request — main (#22)
by Andrey
28:59 queued 16:23
created

OS   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 42
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isDarwin() 0 3 1
A isUnix() 0 3 2
A isLinux() 0 3 1
A isSolaris() 0 3 1
A isUnknown() 0 3 1
A family() 0 5 3
A isBSD() 0 3 1
A isWindows() 0 3 1
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Facades\Helpers\Str;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\Str. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
 * @see https://www.php.net/manual/ru/reserved.constants.php
9
 */
10
final class OS
11
{
12
    public function isUnix(): bool
13
    {
14
        return ! $this->isWindows() && ! $this->isUnknown();
15
    }
16
17
    public function isWindows(): bool
18
    {
19
        return $this->family() === 'windows';
20
    }
21
22
    public function isBSD(): bool
23
    {
24
        return $this->family() === 'bsd';
25
    }
26
27
    public function isDarwin(): bool
28
    {
29
        return $this->family() === 'darwin';
30
    }
31
32
    public function isSolaris(): bool
33
    {
34
        return $this->family() === 'solaris';
35
    }
36
37
    public function isLinux(): bool
38
    {
39
        return $this->family() === 'linux';
40
    }
41
42
    public function isUnknown(): bool
43
    {
44
        return $this->family() === 'unknown';
45
    }
46
47
    public function family(bool $lower = true): string
48
    {
49
        $family = version_compare(PHP_VERSION, '7.2', '<') ? PHP_OS : PHP_OS_FAMILY;
50
51
        return $lower ? Str::lower($family) : $family;
52
    }
53
}
54