1
|
|
|
<?php |
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
3
|
|
|
/** |
4
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
5
|
|
|
* on 23.03.16 at 11:45 |
6
|
|
|
*/ |
7
|
|
|
namespace samsoncms\api\generator\analyzer; |
8
|
|
|
|
9
|
|
|
use samsonframework\orm\DatabaseInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generic entities metadata analyzer. |
13
|
|
|
* |
14
|
|
|
* @package samsoncms\api\analyzer |
15
|
|
|
*/ |
16
|
|
|
class Generic |
17
|
|
|
{ |
18
|
|
|
/** @var array Collection of all parsed metadata by identifiers */ |
19
|
|
|
public static $metadata = []; |
20
|
|
|
|
21
|
|
|
/** @var DatabaseInterface */ |
22
|
|
|
protected $database; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generator constructor. |
26
|
|
|
* @param DatabaseInterface $database Database instance |
27
|
|
|
*/ |
28
|
|
|
public function __construct(DatabaseInterface $database) |
29
|
|
|
{ |
30
|
|
|
$this->database = $database; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \samsoncms\api\generator\metadata\Generic[] |
35
|
|
|
*/ |
36
|
|
|
public function analyze() |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get correct entity name. |
43
|
|
|
* |
44
|
|
|
* @param string $navigationName Original navigation entity name |
45
|
|
|
* @return string Correct PHP-supported entity name |
46
|
|
|
*/ |
47
|
|
|
protected function entityName($navigationName) |
48
|
|
|
{ |
49
|
|
|
return ucfirst($this->getValidName($this->transliterated($navigationName))); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Remove all wrong characters from entity name |
54
|
|
|
* |
55
|
|
|
* @param string $navigationName Original navigation entity name |
56
|
|
|
* |
57
|
|
|
* @return string Correct PHP-supported entity name |
58
|
|
|
*/ |
59
|
|
|
protected function getValidName($navigationName) |
60
|
|
|
{ |
61
|
|
|
return preg_replace('/(^\d*)|([^\w\d_])/', '', $navigationName); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get correct field name. |
66
|
|
|
* |
67
|
|
|
* @param string $fieldName Original field name |
68
|
|
|
* |
69
|
|
|
* @return string Correct PHP-supported field name |
70
|
|
|
*/ |
71
|
|
|
protected function fieldName($fieldName) |
72
|
|
|
{ |
73
|
|
|
return $fieldName = lcfirst($this->transliterated($fieldName)); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
/** |
76
|
|
|
* Get correct full entity name with name space. |
77
|
|
|
* |
78
|
|
|
* @param string $navigationName Original navigation entity name |
79
|
|
|
* @param string $namespace Namespace |
80
|
|
|
* @return string Correct PHP-supported entity name |
81
|
|
|
*/ |
82
|
|
|
protected function fullEntityName($navigationName, $namespace = __NAMESPACE__) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
return '\samsoncms\api\generated\\'.$this->entityName($navigationName); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get additional field type in form of Field constant name |
89
|
|
|
* by database additional field type identifier. |
90
|
|
|
* |
91
|
|
|
* @param integer $fieldType Additional field type identifier |
92
|
|
|
* @return string Additional field type constant |
93
|
|
|
*/ |
94
|
|
|
protected function additionalFieldType($fieldType) |
95
|
|
|
{ |
96
|
|
|
return 'Field::'.$this->constantNameByValue($fieldType); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get class constant name by its value. |
101
|
|
|
* |
102
|
|
|
* @param string $value Constant value |
103
|
|
|
* @param string $className Class name |
104
|
|
|
* |
105
|
|
|
* @return string Full constant name |
106
|
|
|
*/ |
107
|
|
|
protected function constantNameByValue($value, $className = Field::ENTITY) |
108
|
|
|
{ |
109
|
|
|
// Get array where class constants are values and their values are keys |
110
|
|
|
$nameByValue = array_flip((new \ReflectionClass($className))->getConstants()); |
111
|
|
|
|
112
|
|
|
// Try to find constant by its value |
113
|
|
|
if (null !== $nameByValue[$value]) { |
114
|
|
|
// Return constant name |
115
|
|
|
return $nameByValue[$value]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return ''; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Transliterate string to english. |
123
|
|
|
* |
124
|
|
|
* @param string $string Source string |
125
|
|
|
* @return string Transliterated string |
126
|
|
|
*/ |
127
|
|
|
protected function transliterated($string) |
128
|
|
|
{ |
129
|
|
|
return str_replace( |
130
|
|
|
' ', |
131
|
|
|
'', |
132
|
|
|
ucwords(iconv("UTF-8", "UTF-8//IGNORE", strtr($string, array( |
133
|
|
|
"'" => "", |
134
|
|
|
"`" => "", |
135
|
|
|
"-" => " ", |
136
|
|
|
"_" => " ", |
137
|
|
|
"а" => "a", "А" => "a", |
138
|
|
|
"б" => "b", "Б" => "b", |
139
|
|
|
"в" => "v", "В" => "v", |
140
|
|
|
"г" => "g", "Г" => "g", |
141
|
|
|
"д" => "d", "Д" => "d", |
142
|
|
|
"е" => "e", "Е" => "e", |
143
|
|
|
"ж" => "zh", "Ж" => "zh", |
144
|
|
|
"з" => "z", "З" => "z", |
145
|
|
|
"и" => "i", "И" => "i", |
146
|
|
|
"й" => "y", "Й" => "y", |
147
|
|
|
"к" => "k", "К" => "k", |
148
|
|
|
"л" => "l", "Л" => "l", |
149
|
|
|
"м" => "m", "М" => "m", |
150
|
|
|
"н" => "n", "Н" => "n", |
151
|
|
|
"о" => "o", "О" => "o", |
152
|
|
|
"п" => "p", "П" => "p", |
153
|
|
|
"р" => "r", "Р" => "r", |
154
|
|
|
"с" => "s", "С" => "s", |
155
|
|
|
"т" => "t", "Т" => "t", |
156
|
|
|
"у" => "u", "У" => "u", |
157
|
|
|
"ф" => "f", "Ф" => "f", |
158
|
|
|
"х" => "h", "Х" => "h", |
159
|
|
|
"ц" => "c", "Ц" => "c", |
160
|
|
|
"ч" => "ch", "Ч" => "ch", |
161
|
|
|
"ш" => "sh", "Ш" => "sh", |
162
|
|
|
"щ" => "sch", "Щ" => "sch", |
163
|
|
|
"ъ" => "", "Ъ" => "", |
164
|
|
|
"ы" => "y", "Ы" => "y", |
165
|
|
|
"ь" => "", "Ь" => "", |
166
|
|
|
"э" => "e", "Э" => "e", |
167
|
|
|
"ю" => "yu", "Ю" => "yu", |
168
|
|
|
"я" => "ya", "Я" => "ya", |
169
|
|
|
"і" => "i", "І" => "i", |
170
|
|
|
"ї" => "yi", "Ї" => "yi", |
171
|
|
|
"є" => "e", "Є" => "e" |
172
|
|
|
) |
173
|
|
|
) |
174
|
|
|
) |
175
|
|
|
) |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
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.