Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit f863bd2

Browse files
author
Karel Wintersky
committed
1.11
- no PDO typehinting in constructor and methods
1 parent 24b1fab commit f863bd2

File tree

2 files changed

+135
-80
lines changed

2 files changed

+135
-80
lines changed

src/SphinxToolkit.php

Lines changed: 94 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Foolz\SphinxQL\Drivers\ConnectionInterface;
88
use Foolz\SphinxQL\Exception\ConnectionException;
99
use Foolz\SphinxQL\Exception\SphinxQLException;
10-
use PDO;
11-
use PDOException;
1210
use Arris\Toolkit\CLIConsole;
1311

1412
use Foolz\SphinxQL\Drivers\Mysqli\Connection;
@@ -29,12 +27,12 @@ class SphinxToolkit implements SphinxToolkitMysqliInterface, SphinxToolkitFoolzI
2927
private $rai_options;
3028

3129
/**
32-
* @var PDO
30+
* @var
3331
*/
3432
private $mysql_connection;
3533

3634
/**
37-
* @var PDO
35+
* @var
3836
*/
3937
private $sphinx_connection;
4038

@@ -43,7 +41,7 @@ class SphinxToolkit implements SphinxToolkitMysqliInterface, SphinxToolkitFoolzI
4341
*/
4442
private $logger;
4543

46-
public function __construct(PDO $mysql_connection, PDO $sphinx_connection, LoggerInterface $logger = null)
44+
public function __construct($mysql_connection, $sphinx_connection, LoggerInterface $logger = null)
4745
{
4846
$this->mysql_connection = $mysql_connection;
4947
$this->sphinx_connection = $sphinx_connection;
@@ -79,10 +77,20 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
7977
{
8078
$mysql_connection = $this->mysql_connection;
8179
$sphinx_connection = $this->sphinx_connection;
80+
81+
if (empty($sphinx_index)) {
82+
throw new Exception("Requested update of undefined index", 1);
83+
}
84+
85+
if (empty($mysql_table)) {
86+
throw new Exception('Not defined source SQL table', 1);
87+
}
8288

8389
// проверяем, существует ли индекс
84-
if (! SphinxToolkitHelper::RTIndexCheckExist($this->sphinx_connection, $sphinx_index))
90+
if (! SphinxToolkitHelper::RTIndexCheckExist($this->sphinx_connection, $sphinx_index)) {
8591
throw new Exception("`{$sphinx_index}` not present", 1);
92+
}
93+
8694

8795
$chunk_size = $this->rai_options['chunk_length'];
8896

@@ -93,18 +101,21 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
93101
$total_count = SphinxToolkitHelper::MySQL_GetRowsCount($mysql_connection, $mysql_table, $condition);
94102
$total_updated = 0;
95103

96-
if ($this->rai_options['log_before_index'])
104+
if ($this->rai_options['log_before_index']) {
97105
CLIConsole::say("<font color='yellow'>[{$sphinx_index}]</font> index : ", false);
106+
}
98107

99-
if ($this->rai_options['log_total_rows_found'])
108+
if ($this->rai_options['log_total_rows_found']) {
100109
CLIConsole::say("<font color='green'>{$total_count}</font> elements found for rebuild.");
110+
}
101111

102112
// iterate chunks
103-
for ($i = 0; $i < ceil($total_count / $chunk_size); $i++) {
113+
for ($i = 0; $i < \ceil($total_count / $chunk_size); $i++) {
104114
$offset = $i * $chunk_size;
105115

106-
if ($this->rai_options['log_before_chunk'])
116+
if ($this->rai_options['log_before_chunk']) {
107117
CLIConsole::say("Rebuilding elements from <font color='green'>{$offset}</font>, <font color='yellow'>{$chunk_size}</font> count... " , false);
118+
}
108119

109120
$query_chunk_data = "SELECT * FROM {$mysql_table} ";
110121
$query_chunk_data.= $condition != '' ? " WHERE {$condition} " : '';
@@ -114,8 +125,9 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
114125

115126
// iterate inside chunk
116127
while ($item = $sth->fetch()) {
117-
if ($this->rai_options['log_rows_inside_chunk'])
128+
if ($this->rai_options['log_rows_inside_chunk']) {
118129
CLIConsole::say("{$mysql_table}: {$item['id']}");
130+
}
119131

120132
$update_set = $make_updateset_method($item);
121133

@@ -144,8 +156,9 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
144156
CLIConsole::say("I woke up!");
145157
}
146158
} // for
147-
if ($this->rai_options['log_after_index'])
159+
if ($this->rai_options['log_after_index']) {
148160
CLIConsole::say("Total updated <strong>{$total_updated}</strong> elements for <font color='yellow'>{$sphinx_index}</font> RT-index. <br>");
161+
}
149162

150163
return $total_updated;
151164
} // rebuildAbstractIndex
@@ -158,17 +171,21 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
158171

159172
$chunk_size = $this->rai_options['chunk_length'];
160173

161-
if (empty($sphinx_index))
174+
if (empty($sphinx_index)) {
162175
throw new Exception("SearchD index: `{$sphinx_index}` not defined", 1);
176+
}
163177

164-
if (empty($mysql_table))
178+
if (empty($mysql_table)) {
165179
throw new Exception("MySQL table: `{$mysql_table}` not defined", 1);
180+
}
166181

167-
if (! SphinxToolkitHelper::RTIndexCheckExist($this->sphinx_connection, $sphinx_index))
182+
if (! SphinxToolkitHelper::RTIndexCheckExist($this->sphinx_connection, $sphinx_index)) {
168183
throw new Exception("SearchD index: `{$sphinx_index}` not found", 1);
184+
}
169185

170-
if (! SphinxToolkitHelper::RTIndexCheckExist($this->mysql_connection, $mysql_table))
186+
if (! SphinxToolkitHelper::RTIndexCheckExist($this->mysql_connection, $mysql_table)) {
171187
throw new Exception("Source mysql table `{$mysql_table}` not found", 1);
188+
}
172189

173190
// truncate
174191
SphinxToolkitHelper::RTIndexTruncate($sphinx_connection, $sphinx_index);
@@ -177,18 +194,21 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
177194
$total_count = SphinxToolkitHelper::MySQL_GetRowsCount($mysql_connection, $mysql_table, $condition);
178195
$total_updated = 0;
179196

180-
if ($this->rai_options['log_before_index'])
197+
if ($this->rai_options['log_before_index']) {
181198
CLIConsole::say("<font color='yellow'>[{$sphinx_index}]</font> index : ", false);
199+
}
182200

183-
if ($this->rai_options['log_total_rows_found'])
201+
if ($this->rai_options['log_total_rows_found']) {
184202
CLIConsole::say("<font color='green'>{$total_count}</font> elements found for rebuild.");
203+
}
185204

186205
// iterate chunks
187206
for ($i = 0; $i < ceil($total_count / $chunk_size); $i++) {
188207
$offset = $i * $chunk_size;
189208

190-
if ($this->rai_options['log_before_chunk'])
209+
if ($this->rai_options['log_before_chunk']) {
191210
CLIConsole::say("Rebuilding elements from <font color='green'>{$offset}</font>, <font color='yellow'>{$chunk_size}</font> count... " , false);
211+
}
192212

193213
$query_chunk_data = " SELECT * FROM {$mysql_table} ";
194214
$query_chunk_data.= $condition != '' ? " WHERE {$condition} " : '';
@@ -202,8 +222,9 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
202222

203223
// iterate inside chunk
204224
while ($item = $sth->fetch()) {
205-
if ($this->rai_options['log_rows_inside_chunk'])
225+
if ($this->rai_options['log_rows_inside_chunk']) {
206226
CLIConsole::say("{$mysql_table}: {$item['id']}");
227+
}
207228

208229
$update_set = $make_updateset_method($item);
209230

@@ -228,8 +249,10 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
228249
CLIConsole::say("I woke up!");
229250
}
230251
} // for
231-
if ($this->rai_options['log_after_index'])
252+
253+
if ($this->rai_options['log_after_index']) {
232254
CLIConsole::say("Total updated <strong>{$total_updated}</strong> elements for <font color='yellow'>{$sphinx_index}</font> RT-index. <br>");
255+
}
233256

234257
return $total_updated;
235258
} // rebuildAbstractIndexMVA
@@ -251,22 +274,22 @@ private static function buildReplaceQueryMVA(string $table, array $dataset, arra
251274
{
252275
$query = "REPLACE INTO `{$table}` (";
253276

254-
$dataset_keys = array_keys($dataset);
277+
$dataset_keys = \array_keys($dataset);
255278

256-
$query .= implode(', ', array_map(function ($i){
279+
$query .= \implode(', ', \array_map(function ($i){
257280
return "`{$i}`";
258281
}, $dataset_keys));
259282

260283
$query .= " ) VALUES ( ";
261284

262-
$query .= implode(', ', array_map(function ($i) use ($mva_attributes, $dataset){
263-
return in_array($i, $mva_attributes) ? "({$dataset[$i]})" : ":{$i}";
285+
$query .= \implode(', ', \array_map(function ($i) use ($mva_attributes, $dataset){
286+
return \in_array($i, $mva_attributes) ? "({$dataset[$i]})" : ":{$i}";
264287
}, $dataset_keys));
265288

266289
$query .= " ) ";
267290

268-
$new_dataset = array_filter($dataset, function ($value, $key) use ($mva_attributes) {
269-
return !in_array($key, $mva_attributes);
291+
$new_dataset = \array_filter($dataset, function ($value, $key) use ($mva_attributes) {
292+
return !\in_array($key, $mva_attributes);
270293
}, ARRAY_FILTER_USE_BOTH);
271294

272295
return [
@@ -281,17 +304,17 @@ private static function buildReplaceQueryMVA(string $table, array $dataset, arra
281304
*/
282305
private static function buildReplaceQuery(string $table, array $dataset):string
283306
{
284-
$dataset_keys = array_keys($dataset);
307+
$dataset_keys = \array_keys($dataset);
285308

286309
$query = "REPLACE INTO `{$table}` (";
287310

288-
$query.= implode(', ', array_map(function ($i){
311+
$query.= \implode(', ', \array_map(function ($i){
289312
return "`{$i}`";
290313
}, $dataset_keys));
291314

292315
$query.= " ) VALUES ( ";
293316

294-
$query.= implode(', ', array_map(function ($i){
317+
$query.= \implode(', ', \array_map(function ($i){
295318
return ":{$i}";
296319
}, $dataset_keys));
297320

@@ -407,7 +430,9 @@ public static function createInstance()
407430
*/
408431
public static function rt_ReplaceIndex(string $index_name, array $updateset)
409432
{
410-
if (empty($updateset)) return null;
433+
if (empty($updateset)) {
434+
return null;
435+
}
411436

412437
return self::createInstance()
413438
->replace()
@@ -421,7 +446,9 @@ public static function rt_ReplaceIndex(string $index_name, array $updateset)
421446
*/
422447
public static function rt_UpdateIndex(string $index_name, array $updateset)
423448
{
424-
if (empty($updateset)) return null;
449+
if (empty($updateset)) {
450+
return null;
451+
}
425452

426453
return self::createInstance()
427454
->update($index_name)
@@ -435,7 +462,9 @@ public static function rt_UpdateIndex(string $index_name, array $updateset)
435462
*/
436463
public static function rt_DeleteIndex(string $index_name, string $field, $field_value = null)
437464
{
438-
if (is_null($field_value)) return null;
465+
if (is_null($field_value)) {
466+
return null;
467+
}
439468

440469
return self::createInstance()
441470
->delete()
@@ -449,7 +478,9 @@ public static function rt_DeleteIndex(string $index_name, string $field, $field_
449478
*/
450479
public static function rt_DeleteIndexMatch(string $index_name, string $field, $field_value = '')
451480
{
452-
if (is_null($field_value)) return null;
481+
if (is_null($field_value)) {
482+
return null;
483+
}
453484

454485
return self::createInstance()
455486
->delete()
@@ -463,7 +494,10 @@ public static function rt_DeleteIndexMatch(string $index_name, string $field, $f
463494
*/
464495
public static function rt_TruncateIndex(string $index_name, bool $is_reconfigure = true)
465496
{
466-
if (empty($index_name)) return false;
497+
if (empty($index_name)) {
498+
return false;
499+
}
500+
467501
$with = $is_reconfigure ? 'WITH RECONFIGURE' : '';
468502

469503
return (bool)self::createInstance()->query("TRUNCATE RTINDEX {$index_name} {$with}");
@@ -472,7 +506,7 @@ public static function rt_TruncateIndex(string $index_name, bool $is_reconfigure
472506
/**
473507
* @inheritDoc
474508
*/
475-
public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_source_table, string $sphinx_index, Closure $make_updateset_method, string $condition = '')
509+
public static function rt_RebuildAbstractIndex($pdo_connection, string $sql_source_table, string $sphinx_index, Closure $make_updateset_method, string $condition = '')
476510
{
477511
$chunk_size = self::$spql_options['chunk_length'];
478512

@@ -485,15 +519,21 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
485519

486520
$total_updated = 0;
487521

488-
if (self::$spql_options['log_before_index']) CLIConsole::say("<font color='yellow'>[{$sphinx_index}]</font> index : ", false);
522+
if (self::$spql_options['log_before_index']) {
523+
CLIConsole::say("<font color='yellow'>[{$sphinx_index}]</font> index : ", false);
524+
}
489525

490-
if (self::$spql_options['log_total_rows_found']) CLIConsole::say("<font color='green'>{$total_count}</font> elements found for rebuild.");
526+
if (self::$spql_options['log_total_rows_found']) {
527+
CLIConsole::say("<font color='green'>{$total_count}</font> elements found for rebuild.");
528+
}
491529

492530
// iterate chunks
493531
for ($i = 0; $i < ceil($total_count / $chunk_size); $i++) {
494532
$offset = $i * $chunk_size;
495533

496-
if (self::$spql_options['log_before_chunk']) CLIConsole::say("Rebuilding elements from <font color='green'>{$offset}</font>, <font color='yellow'>{$chunk_size}</font> count... " , false);
534+
if (self::$spql_options['log_before_chunk']) {
535+
CLIConsole::say("Rebuilding elements from <font color='green'>{$offset}</font>, <font color='yellow'>{$chunk_size}</font> count... " , false);
536+
}
497537

498538
$query_chunk_data = "SELECT * FROM {$sql_source_table} ";
499539
$query_chunk_data.= $condition != '' ? " WHERE {$condition} " : ' ';
@@ -503,7 +543,9 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
503543

504544
// iterate inside chunk
505545
while ($item = $sth->fetch()) {
506-
if (self::$spql_options['log_rows_inside_chunk']) CLIConsole::say("{$sql_source_table}: {$item['id']}");
546+
if (self::$spql_options['log_rows_inside_chunk']) {
547+
CLIConsole::say("{$sql_source_table}: {$item['id']}");
548+
}
507549

508550
$update_set = $make_updateset_method($item); // call closure
509551

@@ -527,7 +569,9 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
527569
}
528570
} // for
529571

530-
if (self::$spql_options['log_after_index']) CLIConsole::say("Total updated <strong>{$total_updated}</strong> elements for <font color='yellow'>{$sphinx_index}</font> RT-index. <br>");
572+
if (self::$spql_options['log_after_index']) {
573+
CLIConsole::say("Total updated <strong>{$total_updated}</strong> elements for <font color='yellow'>{$sphinx_index}</font> RT-index. <br>");
574+
}
531575

532576
return $total_updated;
533577
}
@@ -553,7 +597,9 @@ public static function spql_getDataSet(string $search_query, string $source_inde
553597
$found_dataset = [];
554598
$compiled_request = '';
555599

556-
if (empty($source_index)) return $found_dataset;
600+
if (empty($source_index)) {
601+
return $found_dataset;
602+
}
557603

558604
try {
559605
$search_request = self::createInstance()
@@ -570,12 +616,12 @@ public static function spql_getDataSet(string $search_query, string $source_inde
570616
->option('field_weights', $option_weight);
571617
}
572618

573-
if (!is_null($limit) && is_numeric($limit)) {
619+
if (!\is_null($limit) && \is_numeric($limit)) {
574620
$search_request = $search_request
575621
->limit($limit);
576622
}
577623

578-
if (strlen($search_query) > 0) {
624+
if (\strlen($search_query) > 0) {
579625
$search_request = $search_request
580626
->match(['title'], $search_query);
581627
}
@@ -595,7 +641,7 @@ public static function spql_getDataSet(string $search_query, string $source_inde
595641
" Error fetching data from `{$source_index}` : " . $e->getMessage(),
596642
[
597643
$e->getCode(),
598-
htmlspecialchars(urldecode($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])),
644+
\htmlspecialchars(\urldecode($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])),
599645
$search_request->getCompiled(),
600646
$meta
601647
]
@@ -616,7 +662,9 @@ public static function spql_getDataSet(string $search_query, string $source_inde
616662
*/
617663
private static function internal_ReplaceIndex(string $index_name, array $updateset)
618664
{
619-
if (empty($updateset)) return null;
665+
if (empty($updateset)) {
666+
return null;
667+
}
620668

621669
return self::getInstance(self::$spql_connection)
622670
->replace()

0 commit comments

Comments
 (0)