Skip to content

Commit 058e39a

Browse files
authored
Merge pull request #356 from leonardfischer/clean-up-code
Clean up code
2 parents fb20773 + ded3dd5 commit 058e39a

File tree

12 files changed

+57
-101
lines changed

12 files changed

+57
-101
lines changed

src/Connectors/SQLiteConnector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function connect(array $config)
2222
// SQLite supports "in-memory" databases that only last as long as the owning
2323
// connection does. These are useful for tests or for short lifetime store
2424
// querying. In-memory databases may only have a single open connection.
25-
if ($config['database'] == ':memory:') {
25+
if ($config['database'] === ':memory:') {
2626
return $this->createConnection('sqlite::memory:', $config, $options);
2727
}
2828

src/Engines/MysqlEngine.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
class MysqlEngine extends SqliteEngine
2121
{
22+
/**
23+
* @param string $indexName
24+
* @return $this
25+
* @throws \Exception
26+
*/
2227
public function createIndex(string $indexName)
2328
{
2429
$this->setIndexName($indexName);
@@ -152,7 +157,7 @@ public function totalDocumentsInCollection()
152157
public function saveWordlist(Collection $stems)
153158
{
154159
$terms = [];
155-
$stems->map(function ($column, $key) use (&$terms) {
160+
$stems->map(function ($column) use (&$terms) {
156161
foreach ($column as $term) {
157162
if (array_key_exists($term, $terms)) {
158163
$terms[$term]['hits']++;
@@ -194,7 +199,7 @@ public function saveWordlist(Collection $stems)
194199
public function saveDoclist(array $terms, int $docId)
195200
{
196201
$insertRows = [];
197-
foreach ($terms as $key => $term) {
202+
foreach ($terms as $term) {
198203
$insertRows[] = '(' . $this->index->quote($term['id']) . ', ' . $this->index->quote($docId) . ', ' . $this->index->quote($term['hits']) . ')';
199204
}
200205

@@ -204,31 +209,6 @@ public function saveDoclist(array $terms, int $docId)
204209

205210
public function saveHitList(array $stems, int $docId, array $termsList)
206211
{
207-
return;
208-
$fieldCounter = 0;
209-
$fields = [];
210-
211-
$insert = "INSERT INTO {$this->indexName}_hitlist (term_id, doc_id, field_id, position, hit_count)
212-
VALUES (:term_id, :doc_id, :field_id, :position, :hit_count)";
213-
$stmt = $this->index->prepare($insert);
214-
215-
foreach ($stems as $field => $terms) {
216-
$fields[$fieldCounter] = $field;
217-
$positionCounter = 0;
218-
$termCounts = array_count_values($terms);
219-
foreach ($terms as $term) {
220-
if (isset($termsList[$term])) {
221-
$stmt->bindValue(':term_id', $termsList[$term]['id']);
222-
$stmt->bindValue(':doc_id', $docId);
223-
$stmt->bindValue(':field_id', $fieldCounter);
224-
$stmt->bindValue(':position', $positionCounter);
225-
$stmt->bindValue(':hit_count', $termCounts[$term]);
226-
$stmt->execute();
227-
}
228-
$positionCounter++;
229-
}
230-
$fieldCounter++;
231-
}
232212
}
233213

234214
public function delete(int $documentId)

src/Engines/RedisEngine.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function loadConfig(array $config)
6262
], $redisOptions);
6363
}
6464

65+
/**
66+
* @param string $indexName
67+
* @return $this
68+
* @throws Exception
69+
*/
6570
public function createIndex(string $indexName)
6671
{
6772
$this->flushIndex($indexName);
@@ -138,7 +143,7 @@ public function processDocument(Collection $row)
138143
$row->forget($this->getPrimaryKey());
139144
}
140145

141-
$stems = $row->map(function ($columnContent, $columnName) use ($row) {
146+
$stems = $row->map(function ($columnContent) {
142147
if (trim((string)$columnContent) === '') {
143148
return [];
144149
}
@@ -165,7 +170,7 @@ public function saveWordlist(Collection $stems)
165170
{
166171
$terms = [];
167172

168-
$stems->map(function ($column, $key) use (&$terms) {
173+
$stems->map(function ($column) use (&$terms) {
169174
foreach ($column as $term) {
170175
if (array_key_exists($term, $terms)) {
171176
$terms[$term]['num_hits']++;

src/Engines/SqliteEngine.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class SqliteEngine implements EngineInterface
4949

5050
/**
5151
* @param string $indexName
52-
*
53-
* @return TNTIndexer
52+
* @return $this
53+
* @throws \Exception
5454
*/
5555
public function createIndex(string $indexName)
5656
{
@@ -206,7 +206,7 @@ public function processDocument(Collection $row)
206206
$row->forget($this->getPrimaryKey());
207207
}
208208

209-
$stems = $row->map(function ($columnContent, $columnName) use ($row) {
209+
$stems = $row->map(function ($columnContent) {
210210
if (trim((string)$columnContent) === '') {
211211
return [];
212212
}
@@ -253,7 +253,8 @@ public function prepareStatementsForIndex()
253253
public function saveWordlist(Collection $stems)
254254
{
255255
$terms = [];
256-
$stems->map(function ($column, $key) use (&$terms) {
256+
257+
$stems->map(function ($column) use (&$terms) {
257258
foreach ($column as $term) {
258259
if (array_key_exists($term, $terms)) {
259260
$terms[$term]['hits']++;
@@ -269,7 +270,6 @@ public function saveWordlist(Collection $stems)
269270
});
270271

271272
foreach ($terms as $key => $term) {
272-
273273
try {
274274
$this->insertWordlistStmt->bindParam(":keyword", $key);
275275
$this->insertWordlistStmt->bindParam(":hits", $term['hits']);
@@ -317,13 +317,12 @@ public function saveDoclist(array $terms, int $docId)
317317
$insert = 'INSERT INTO doclist (term_id, doc_id, hit_count) VALUES (:id, :doc, :hits)';
318318
$stmt = $this->index->prepare($insert);
319319

320-
foreach ($terms as $key => $term) {
321-
320+
foreach ($terms as $term) {
322321
$stmt->bindValue(':id', $term['id']);
323322
$stmt->bindValue(':doc', $docId);
324323
$stmt->bindValue(':hits', $term['hits']);
325324
try {
326-
$res = $stmt->execute();
325+
$stmt->execute();
327326
} catch (\Exception $e) {
328327
//we have a duplicate
329328
echo $e->getMessage();
@@ -333,31 +332,6 @@ public function saveDoclist(array $terms, int $docId)
333332

334333
public function saveHitList(array $stems, int $docId, array $termsList)
335334
{
336-
return;
337-
$fieldCounter = 0;
338-
$fields = [];
339-
340-
$insert = "INSERT INTO hitlist (term_id, doc_id, field_id, position, hit_count)
341-
VALUES (:term_id, :doc_id, :field_id, :position, :hit_count)";
342-
$stmt = $this->index->prepare($insert);
343-
344-
foreach ($stems as $field => $terms) {
345-
$fields[$fieldCounter] = $field;
346-
$positionCounter = 0;
347-
$termCounts = array_count_values($terms);
348-
foreach ($terms as $term) {
349-
if (isset($termsList[$term])) {
350-
$stmt->bindValue(':term_id', $termsList[$term]['id']);
351-
$stmt->bindValue(':doc_id', $docId);
352-
$stmt->bindValue(':field_id', $fieldCounter);
353-
$stmt->bindValue(':position', $positionCounter);
354-
$stmt->bindValue(':hit_count', $termCounts[$term]);
355-
$stmt->execute();
356-
}
357-
$positionCounter++;
358-
}
359-
$fieldCounter++;
360-
}
361335
}
362336

363337
public function readDocumentsFromFileSystem()

src/Indexer/TNTIndexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function createIndex(string $indexName)
125125
public function setDatabaseHandle(PDO $dbh)
126126
{
127127
$this->dbh = $dbh;
128-
if ($this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') {
128+
if ($this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME) === 'mysql') {
129129
$this->dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
130130
}
131131
}

src/Stemmer/CroatianStemmer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,6 @@ public static function endsWith($haystack, $needle)
185185
'vaca' => 'vca',
186186
'saca' => 'sca',
187187
'sac' => 'sca',
188-
'naca' => 'nca',
189-
'nac' => 'nca',
190188
'raca' => 'rca',
191189
'rac' => 'rca',
192190
'aoca' => 'alca',

src/Stemmer/PorterStemmer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static function step1ab($word)
7575
*/
7676
private static function doPartA($word)
7777
{
78-
if (substr($word, -1) == 's') {
78+
if (substr($word, -1) === 's') {
7979

8080
self::replace($word, 'sses', 'ss')
8181
|| self::replace($word, 'ies', 'i')
@@ -127,7 +127,7 @@ private static function step1c($word)
127127
{
128128
$v = self::$regex_vowel;
129129

130-
if (substr($word, -1) == 'y' && preg_match("#$v+#", substr($word, 0, -1))) {
130+
if (substr($word, -1) === 'y' && preg_match("#$v+#", substr($word, 0, -1))) {
131131
self::replace($word, 'y', 'i');
132132
}
133133

@@ -266,7 +266,7 @@ private static function step4($word)
266266
break;
267267

268268
case 'o':
269-
if (substr($word, -4) == 'tion' || substr($word, -4) == 'sion') {
269+
if (substr($word, -4) === 'tion' || substr($word, -4) === 'sion') {
270270
self::replace($word, 'ion', '', 1);
271271
} else {
272272
self::replace($word, 'ou', '', 1);
@@ -306,7 +306,7 @@ private static function step4($word)
306306
private static function step5($word)
307307
{
308308
// Part a
309-
if (substr($word, -1) == 'e') {
309+
if (substr($word, -1) === 'e') {
310310
if (self::m(substr($word, 0, -1)) > 1) {
311311
self::replace($word, 'e', '');
312312

@@ -319,7 +319,7 @@ private static function step5($word)
319319
}
320320

321321
// Part b
322-
if (self::m($word) > 1 && self::doubleConsonant($word) && substr($word, -1) == 'l') {
322+
if (self::m($word) > 1 && self::doubleConsonant($word) && substr($word, -1) === 'l') {
323323
$word = substr($word, 0, -1);
324324
}
325325

src/Stemmer/PortugueseStemmer.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PortugueseStemmer implements StemmerInterface
3939
/**
4040
* UTF-8 Case lookup table
4141
*
42-
* This lookuptable defines the upper case letters to their correspponding
42+
* This lookup table defines the upper case letters to their corresponding
4343
* lower case letter in UTF-8
4444
*
4545
* @author Andreas Gohr <[email protected]>
@@ -71,23 +71,22 @@ class PortugueseStemmer implements StemmerInterface
7171
0x0066 => 0x0046, 0x00FD => 0x00DD, 0x0063 => 0x0043, 0x021B => 0x021A, 0x00EA => 0x00CA,
7272
0x03B9 => 0x0399, 0x017A => 0x0179, 0x00EF => 0x00CF, 0x01B0 => 0x01AF, 0x0065 => 0x0045,
7373
0x03BB => 0x039B, 0x03B8 => 0x0398, 0x03BC => 0x039C, 0x045C => 0x040C, 0x043F => 0x041F,
74-
0x044C => 0x042C, 0x00FE => 0x00DE, 0x00F0 => 0x00D0, 0x1EF3 => 0x1EF2, 0x0068 => 0x0048,
75-
0x00EB => 0x00CB, 0x0111 => 0x0110, 0x0433 => 0x0413, 0x012F => 0x012E, 0x00E6 => 0x00C6,
76-
0x0078 => 0x0058, 0x0161 => 0x0160, 0x016F => 0x016E, 0x03B1 => 0x0391, 0x0457 => 0x0407,
77-
0x0173 => 0x0172, 0x00FF => 0x0178, 0x006F => 0x004F, 0x043B => 0x041B, 0x03B5 => 0x0395,
78-
0x0445 => 0x0425, 0x0121 => 0x0120, 0x017E => 0x017D, 0x017C => 0x017B, 0x03B6 => 0x0396,
79-
0x03B2 => 0x0392, 0x03AD => 0x0388, 0x1E85 => 0x1E84, 0x0175 => 0x0174, 0x0071 => 0x0051,
80-
0x0437 => 0x0417, 0x1E0B => 0x1E0A, 0x0148 => 0x0147, 0x0105 => 0x0104, 0x0458 => 0x0408,
81-
0x014D => 0x014C, 0x00ED => 0x00CD, 0x0079 => 0x0059, 0x010B => 0x010A, 0x03CE => 0x038F,
82-
0x0072 => 0x0052, 0x0430 => 0x0410, 0x0455 => 0x0405, 0x0452 => 0x0402, 0x0127 => 0x0126,
83-
0x0137 => 0x0136, 0x012B => 0x012A, 0x03AF => 0x038A, 0x044B => 0x042B, 0x006C => 0x004C,
84-
0x03B7 => 0x0397, 0x0125 => 0x0124, 0x0219 => 0x0218, 0x00FB => 0x00DB, 0x011F => 0x011E,
85-
0x043E => 0x041E, 0x1E41 => 0x1E40, 0x03BD => 0x039D, 0x0107 => 0x0106, 0x03CB => 0x03AB,
86-
0x0446 => 0x0426, 0x00FE => 0x00DE, 0x00E7 => 0x00C7, 0x03CA => 0x03AA, 0x0441 => 0x0421,
87-
0x0432 => 0x0412, 0x010F => 0x010E, 0x00F8 => 0x00D8, 0x0077 => 0x0057, 0x011B => 0x011A,
88-
0x0074 => 0x0054, 0x006A => 0x004A, 0x045B => 0x040B, 0x0456 => 0x0406, 0x0103 => 0x0102,
89-
0x03BB => 0x039B, 0x00F1 => 0x00D1, 0x043D => 0x041D, 0x03CC => 0x038C, 0x00E9 => 0x00C9,
90-
0x00F0 => 0x00D0, 0x0457 => 0x0407, 0x0123 => 0x0122
74+
0x044C => 0x042C, 0x1EF3 => 0x1EF2, 0x0068 => 0x0048, 0x00EB => 0x00CB, 0x0111 => 0x0110,
75+
0x0433 => 0x0413, 0x012F => 0x012E, 0x00E6 => 0x00C6, 0x0078 => 0x0058, 0x0161 => 0x0160,
76+
0x016F => 0x016E, 0x03B1 => 0x0391, 0x0457 => 0x0407, 0x0173 => 0x0172, 0x00FF => 0x0178,
77+
0x006F => 0x004F, 0x043B => 0x041B, 0x03B5 => 0x0395, 0x0445 => 0x0425, 0x0121 => 0x0120,
78+
0x017E => 0x017D, 0x017C => 0x017B, 0x03B6 => 0x0396, 0x03B2 => 0x0392, 0x03AD => 0x0388,
79+
0x1E85 => 0x1E84, 0x0175 => 0x0174, 0x0071 => 0x0051, 0x0437 => 0x0417, 0x1E0B => 0x1E0A,
80+
0x0148 => 0x0147, 0x0105 => 0x0104, 0x0458 => 0x0408, 0x014D => 0x014C, 0x00ED => 0x00CD,
81+
0x0079 => 0x0059, 0x010B => 0x010A, 0x03CE => 0x038F, 0x0072 => 0x0052, 0x0430 => 0x0410,
82+
0x0455 => 0x0405, 0x0452 => 0x0402, 0x0127 => 0x0126, 0x0137 => 0x0136, 0x012B => 0x012A,
83+
0x03AF => 0x038A, 0x044B => 0x042B, 0x006C => 0x004C, 0x03B7 => 0x0397, 0x0125 => 0x0124,
84+
0x0219 => 0x0218, 0x00FB => 0x00DB, 0x011F => 0x011E, 0x043E => 0x041E, 0x1E41 => 0x1E40,
85+
0x03BD => 0x039D, 0x0107 => 0x0106, 0x03CB => 0x03AB, 0x0446 => 0x0426, 0x00E7 => 0x00C7,
86+
0x03CA => 0x03AA, 0x0441 => 0x0421, 0x0432 => 0x0412, 0x010F => 0x010E, 0x00F8 => 0x00D8,
87+
0x0077 => 0x0057, 0x011B => 0x011A, 0x0074 => 0x0054, 0x006A => 0x004A, 0x045B => 0x040B,
88+
0x0456 => 0x0406, 0x0103 => 0x0102, 0x00F1 => 0x00D1, 0x043D => 0x041D, 0x03CC => 0x038C,
89+
0x00E9 => 0x00C9, 0x0123 => 0x0122
9190
];
9291

9392
private static array $vowels = ['a', 'e', 'i', 'o', 'u', 'á', 'é', 'í', 'ó', 'ú', 'â', 'ê', 'ô'];
@@ -419,7 +418,7 @@ private static function step1(&$word, $r1Index, $r2Index, $rvIndex)
419418
$before = $position - 1;
420419
$letter = self::substr($word, $before, 1);
421420

422-
if ($letter == 'e') {
421+
if ($letter === 'e') {
423422
$word = preg_replace('#(iras|ira)$#u', 'ir', $word);
424423
}
425424
}
@@ -455,7 +454,7 @@ private static function step3(&$word, $rvIndex)
455454
if (self::searchIfInRv($word, ['i'], $rvIndex) !== false) {
456455
$letter = self::substr($word, -2, 1);
457456

458-
if ($letter == 'c') {
457+
if ($letter === 'c') {
459458
$word = self::substr($word, 0, -1);
460459
}
461460

src/Support/Expression.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
class Expression
66
{
7-
protected $operatorQueue;
8-
protected $tokenQueue;
9-
107
public function toPostfix($exp)
118
{
129
$postfix = [];

src/TNTFuzzyMatch.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public function makeVectorSameLength(array $str, array $pattern)
7272
{
7373
$j = 0;
7474
$max = max(count($pattern), count($str));
75-
$a = [];
7675
$b = [];
7776

7877
for ($i = 0; $i < $max && $j < $max; $i++) {

0 commit comments

Comments
 (0)