1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Serhii Nekhaienko <[email protected]> |
4
|
|
|
* @license GPL |
5
|
|
|
* @copyright Serhii Nekhaienko © 2018 |
6
|
|
|
* @version 4.0.0 |
7
|
|
|
* @project browser-detector |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace EndorphinStudio\Detector; |
11
|
|
|
|
12
|
|
|
use EndorphinStudio\Detector\Data\Result; |
13
|
|
|
use EndorphinStudio\Detector\Exception\StorageException; |
14
|
|
|
use EndorphinStudio\Detector\Storage\StorageInterface; |
15
|
|
|
use Twig\Parser; |
16
|
|
|
|
17
|
|
|
class Detector |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var StorageInterface |
21
|
|
|
*/ |
22
|
|
|
private $dataProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return StorageInterface |
26
|
|
|
*/ |
27
|
|
|
public function getDataProvider(): StorageInterface |
28
|
|
|
{ |
29
|
|
|
return $this->dataProvider; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return Result |
34
|
|
|
*/ |
35
|
|
|
public function getResultObject(): Result |
36
|
|
|
{ |
37
|
|
|
return $this->resultObject; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Result |
42
|
|
|
*/ |
43
|
|
|
private $resultObject; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param StorageInterface $dataProvider |
47
|
|
|
*/ |
48
|
|
|
public function setDataProvider(StorageInterface $dataProvider) |
49
|
|
|
{ |
50
|
|
|
$this->dataProvider = $dataProvider; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private $ua; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Detector constructor. |
57
|
|
|
* @param string $dataProvider |
58
|
|
|
* @param string $format |
59
|
|
|
* @throws StorageException |
60
|
|
|
*/ |
61
|
|
|
public function __construct(string $dataProvider = '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', string $format = 'yaml') |
62
|
|
|
{ |
63
|
|
|
$dataDirectory = sprintf('%s/var/%s', dirname(__DIR__), $format); |
64
|
|
|
$dataProvider = new $dataProvider(); |
65
|
|
|
$dataProvider->setDataDirectory($dataDirectory); |
66
|
|
|
$this->setDataProvider($dataProvider); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function analyze(string $ua = 'ua') |
70
|
|
|
{ |
71
|
|
|
$this->ua = $ua === 'ua' ? $_SERVER['HTTP_USER_AGENT'] : $ua; |
72
|
|
|
$config = $this->getDataProvider()->getConfig(); |
73
|
|
|
$check = ['device', 'os', 'browser', 'robots']; |
74
|
|
|
$this->resultObject = new Result(); |
75
|
|
|
foreach ($check as $type) { |
76
|
|
|
$this->detect($config, $type); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
var_dump($this->resultObject); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function detect($config, $type) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function getPatternList($list, $type) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return array_key_exists($type, $list) ? $list[$type] : []; |
89
|
|
|
} |
90
|
|
|
} |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: