Completed
Push — 4.0 ( 73f7eb...90ad36 )
by Serhii
02:22
created

Tools::setWindowsConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @author Serhii Nekhaienko <[email protected]>
4
 * @license GPL
5
 * @copyright Serhii Nekhaienko &copy 2018
6
 * @version 4.0.0
7
 * @project browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector;
11
12
class Tools
13
{
14
    private static $windowsConfig;
15
16
    /**
17
     * @param mixed $windowsConfig
18
     */
19
    public static function setWindowsConfig($windowsConfig)
20
    {
21
        self::$windowsConfig = $windowsConfig;
22
    }
23
24
    public static function getVersion(string $phrase, string $ua): string
25
    {
26
        $version = static::getVersionPattern($phrase);
27
        $uaString = str_replace(' NT', '', $ua);
28
        if (preg_match($version, $uaString)) {
29
            preg_match($version, $uaString, $v);
30
            $version = $v[0];
31
            $version = preg_replace('/' . $phrase . '/', '', $version);
32
            $version = str_replace(';', '', $version);
33
            $version = str_replace(' ', '', $version);
34
            $version = str_replace('/', '', $version);
35
            $version = str_replace('_', '.', $version);
36
37
            if(preg_match('/ Windows /', $uaString)) {
38
                $version = static::getWindowsVersion($version);
39
            }
40
            return $version;
41
        }
42
        return "not available";
43
    }
44
45
    public static function getVersionPattern(string $phrase): string
46
    {
47
        return sprintf('/%s(\/| )[\w-._]{1,15}/', $phrase);
48
    }
49
50
    public static function getWindowsVersion(string $version)
51
    {
52
        $config = static::$windowsConfig;
0 ignored issues
show
Bug introduced by
Since $windowsConfig is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $windowsConfig to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
53
        return \array_key_exists($version, $config) ? $config[$version] : $version;
54
    }
55
56
    public static function getMethodName(string $name, string $type = 'set')
57
    {
58
        return sprintf('%s%s', $type, ucfirst($name));
59
    }
60
61
    public static function runSetter(&$object, string $key, $value)
62
    {
63
        $methodName = static::getMethodName($key);
64
        if (method_exists($object, $methodName)) {
65
            $object->$methodName($value);
66
        }
67
    }
68
69
    public static function runGetter(&$object, string $key)
70
    {
71
        $methodName = static::getMethodName($key, 'get');
72
        if (method_exists($object, $methodName)) {
73
            return $object->$methodName();
74
        }
75
        return null;
76
    }
77
78
    public static function resolvePath(array &$files, $path)
79
    {
80
        if (is_array($path)) {
81
            $files = \array_merge($files, $path);
82
        } else {
83
            $files[] = $path;
84
        }
85
        return \array_unique($files);
86
    }
87
}