1 | <?php |
||
2 | |||
3 | namespace Sludio\HelperBundle\Script\Repository; |
||
4 | |||
5 | use Sludio\HelperBundle\Script\Utils\Helper; |
||
6 | |||
7 | abstract class QuickInsertFunctions |
||
8 | { |
||
9 | public static $entityManager; |
||
10 | public static $connection; |
||
11 | protected static $mock = []; |
||
12 | protected static $metadata = []; |
||
13 | protected static $tableName; |
||
14 | protected static $identifier; |
||
15 | |||
16 | protected static function buildExtra($extra) |
||
17 | { |
||
18 | $methods = [ |
||
19 | 'GROUP BY', |
||
20 | 'HAVING', |
||
21 | 'ORDER BY', |
||
22 | ]; |
||
23 | $sql = ''; |
||
24 | |||
25 | foreach ($methods as $method) { |
||
26 | if (isset($extra[$method])) { |
||
27 | $sql .= ' '.$method.' '; |
||
28 | if (\is_array($extra[$method])) { |
||
29 | $sql .= implode(' ', $extra[$method]).' '; |
||
30 | } else { |
||
31 | $sql .= $extra[$method].' '; |
||
32 | } |
||
33 | } |
||
34 | } |
||
35 | Filters::getLimit($extra, $sql); |
||
36 | |||
37 | return Helper::oneSpace($sql); |
||
38 | } |
||
39 | |||
40 | protected static function buildWhere($tableName, array $where) |
||
41 | { |
||
42 | $whereSql = ''; |
||
43 | if (!empty($where)) { |
||
44 | $path = ' WHERE '; |
||
45 | foreach ($where as $key => $value) { |
||
46 | if (!\is_array($value)) { |
||
47 | if (isset(self::$mock[$tableName][$key])) { |
||
48 | $whereSql .= $path.self::$mock[$tableName][$key].' = '.self::slashes($tableName, $key, $value); |
||
49 | } else { |
||
50 | $whereSql .= $path.$key.' = '.self::slashes($tableName, $key, $value); |
||
0 ignored issues
–
show
|
|||
51 | } |
||
52 | } else { |
||
53 | $whereSql .= $path.$value[0]; |
||
54 | } |
||
55 | |||
56 | if ($value === reset($where)) { |
||
57 | $path = ' AND '; |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | return $whereSql; |
||
63 | } |
||
64 | |||
65 | protected static function slashes($tableName, $key, $value) |
||
66 | { |
||
67 | if ($value instanceof \DateTime) { |
||
68 | $result = addslashes(trim($value->format('Y-m-d H:i:s'))); |
||
69 | } else { |
||
70 | $result = self::numeric($tableName, $key, $value) ? (int)$value : "'".addslashes(trim($value))."'"; |
||
71 | } |
||
72 | |||
73 | $trim = trim($result); |
||
74 | if ($trim === '' || $trim === "''") { |
||
75 | $result = null; |
||
76 | } |
||
77 | |||
78 | return $result; |
||
79 | } |
||
80 | |||
81 | protected static function numeric($tableName, $key, $value) |
||
82 | { |
||
83 | $intTypes = [ |
||
84 | 'boolean', |
||
85 | 'integer', |
||
86 | 'longint', |
||
87 | ]; |
||
88 | $flip = []; |
||
89 | if (isset(self::$mock[$tableName])) { |
||
90 | $flip = array_flip(self::$mock[$tableName]); |
||
91 | } |
||
92 | |||
93 | if (isset(self::$metadata[$tableName], $flip[$key])) { |
||
94 | if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
||
95 | return true; |
||
96 | } |
||
97 | |||
98 | return false; |
||
99 | } |
||
100 | |||
101 | return \is_numeric($value) || \is_bool($value); |
||
102 | } |
||
103 | |||
104 | protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
||
105 | { |
||
106 | self::init($manager); |
||
107 | if (\is_object($object)) { |
||
108 | self::extract($object); |
||
109 | $tableName = self::$tableName; |
||
110 | $columns = self::$mock[$tableName] ?: []; |
||
111 | $type = 'object'; |
||
112 | } else { |
||
113 | $tableName = $object['table_name']; |
||
114 | unset($object['table_name']); |
||
115 | $type = 'table'; |
||
116 | $columns = array_keys($object) ?: []; |
||
117 | } |
||
118 | |||
119 | if (isset($extraFields[$tableName])) { |
||
120 | $columns = array_merge($columns, $extraFields[$tableName]); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | public static function init($manager = null) |
||
125 | { |
||
126 | if (self::$connection) { |
||
127 | return; |
||
128 | } |
||
129 | global $kernel; |
||
130 | |||
131 | if (\class_exists('\AppCache') && \AppCache::class === \get_class($kernel)) { |
||
0 ignored issues
–
show
The type
AppCache was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths
Loading history...
|
|||
132 | $kernel = $kernel->getKernel(); |
||
133 | } |
||
134 | $container = $kernel->getContainer(); |
||
135 | |||
136 | $manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
||
137 | if (\is_object($manager)) { |
||
138 | self::$entityManager = $manager; |
||
139 | } else { |
||
140 | self::$entityManager = $container->get('doctrine')->getManager($manager); |
||
141 | } |
||
142 | self::$connection = self::$entityManager->getConnection(); |
||
143 | } |
||
144 | |||
145 | protected static function extract($object) |
||
146 | { |
||
147 | self::init(false); |
||
148 | $data = Filters::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object))); |
||
149 | |||
150 | self::$mock = $data['mock']; |
||
151 | self::$tableName = $data['table']; |
||
152 | self::$metadata[$data['table']] = $data['meta']; |
||
153 | self::$identifier = $data['identifier']; |
||
154 | } |
||
155 | |||
156 | protected static function parseUpdateResult($object, $type, $id, $tableName, array $result = null) |
||
157 | { |
||
158 | $data = []; |
||
159 | if (!empty($result)) { |
||
160 | foreach ($result as $key => $value) { |
||
161 | $content = self::value($object, $key, $type, $tableName, false); |
||
162 | if ($id && !\in_array($content, [ |
||
163 | null, |
||
164 | $value, |
||
165 | ], true)) { |
||
166 | $data[$key] = $content; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | return $data; |
||
172 | } |
||
173 | |||
174 | protected static function value($object, $variable, $type, $tableName, $check = true) |
||
175 | { |
||
176 | $value = null; |
||
177 | if ($type === 'object') { |
||
178 | $value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}(); |
||
179 | } else { |
||
180 | if (isset($object[$variable])) { |
||
181 | $value = $object[$variable]; |
||
182 | } |
||
183 | } |
||
184 | |||
185 | if ($check) { |
||
186 | $value = self::slashes($tableName, $variable, $value); |
||
0 ignored issues
–
show
Are you sure the assignment to
$value is correct as self::slashes($tableName, $variable, $value) targeting Sludio\HelperBundle\Scri...ertFunctions::slashes() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
187 | } |
||
188 | |||
189 | return $value; |
||
190 | } |
||
191 | |||
192 | protected static function makeValues($tableName, array $data = []) |
||
193 | { |
||
194 | $values = ''; |
||
195 | if (!empty($data)) { |
||
196 | foreach ($data as $key => $value) { |
||
197 | $values .= self::slashes($tableName, $key, $value).','; |
||
0 ignored issues
–
show
Are you sure the usage of
self::slashes($tableName, $key, $value) targeting Sludio\HelperBundle\Scri...ertFunctions::slashes() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
198 | } |
||
199 | } |
||
200 | |||
201 | return \substr($values, 0, -1); |
||
202 | } |
||
203 | |||
204 | protected static function parsePersistColumns(array $columns = [], $object, $type, $tableName, &$idd) |
||
205 | { |
||
206 | $data = []; |
||
207 | foreach ($columns as $value => $key) { |
||
208 | $keys = [ |
||
209 | $key, |
||
210 | $value, |
||
211 | ]; |
||
212 | if (!Helper::multiple($keys)) { |
||
213 | $value = self::value($object, $value, $type, $tableName); |
||
0 ignored issues
–
show
Are you sure the assignment to
$value is correct as self::value($object, $value, $type, $tableName) targeting Sludio\HelperBundle\Scri...nsertFunctions::value() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
214 | if ($value !== null) { |
||
215 | $data[$key] = $value; |
||
216 | if ($key === self::$identifier) { |
||
217 | $idd = $value; |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | |||
223 | return $data; |
||
224 | } |
||
225 | } |
||
226 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.