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
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function getUserAgent() |
57
|
|
|
{ |
58
|
|
|
return $this->ua; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private $ua; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private $detectors; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Detector constructor. |
70
|
|
|
* @param string $dataProvider |
71
|
|
|
* @param string $format |
72
|
|
|
* @throws StorageException |
73
|
|
|
*/ |
74
|
|
|
public function __construct(string $dataProvider = '\\EndorphinStudio\\Detector\\Storage\\YamlStorage', string $format = 'yaml') |
75
|
|
|
{ |
76
|
|
|
$dataDirectory = sprintf('%s/var/%s', dirname(__DIR__), $format); |
77
|
|
|
$dataProvider = new $dataProvider(); |
78
|
|
|
$dataProvider->setDataDirectory($dataDirectory); |
79
|
|
|
$this->setDataProvider($dataProvider); |
80
|
|
|
$this->detectors = []; |
81
|
|
|
$this->resultObject = new Result(); |
82
|
|
|
$check = ['device', 'os', 'browser', 'robots']; |
83
|
|
|
foreach ($check as $detectionnType) { |
84
|
|
|
$className = sprintf('\\EndorphinStudio\\Detector\\Detection\\%s', ucfirst(sprintf('%sDetector', $detectionnType))); |
85
|
|
|
if(class_exists($className)) { |
86
|
|
|
$this->detectors[$detectionnType] = new $className(); |
87
|
|
|
$this->detectors[$detectionnType]->init($this); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function analyze(string $ua = 'ua') |
93
|
|
|
{ |
94
|
|
|
$this->ua = $ua === 'ua' ? $_SERVER['HTTP_USER_AGENT'] : $ua; |
95
|
|
|
$config = $this->getDataProvider()->getConfig(); |
|
|
|
|
96
|
|
|
foreach ($this->detectors as $detectionType => $detector) { |
97
|
|
|
$detector->detect($ua); |
98
|
|
|
} |
99
|
|
|
echo 'test'; |
100
|
|
|
// $this->resultObject = new Result(); |
|
|
|
|
101
|
|
|
// foreach ($check as $type) { |
102
|
|
|
// $this->detect($config, $type); |
103
|
|
|
// } |
104
|
|
|
// var_dump($this->resultObject); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function detect($config, $type) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getPatternList($list, $type) |
113
|
|
|
{ |
114
|
|
|
return array_key_exists($type, $list) ? $list[$type] : []; |
115
|
|
|
} |
116
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.