Passed
Push — develop ( 169afe...f2bd80 )
by Jens
02:39
created

TermCount   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 15.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 90
rs 10
c 1
b 0
f 0
wmc 13
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 9 2
A applyFilters() 9 9 2
B storeDocumentTermCount() 0 27 5
A executeStoreDocumentTermCount() 5 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * User: jensk
4
 * Date: 1-3-2017
5
 * Time: 10:22
6
 */
7
8
namespace library\search\indexer;
9
10
11
use library\search\DocumentTokenizer;
12
use library\search\Indexer;
13
use library\storage\JsonStorage;
14
15
class TermCount
16
{
17
	protected $dbHandle;
18
	protected $documents;
19
	protected $filters;
20
	protected $storage;
21
22
	/**
23
	 * TermCount constructor.
24
	 *
25
	 * @param resource $dbHandle
26
	 * @param array $documents
27
	 * @param array $filters
28
	 * @param JsonStorage $jsonStorage
29
	 */
30
	public function __construct($dbHandle, $documents, $filters, $jsonStorage)
31
	{
32
		$this->dbHandle = $dbHandle;
33
		$this->documents = $documents;
34
		$this->filters = $filters;
35
		$this->storage = $jsonStorage;
36
	}
37
38
	public function execute()
39
	{
40
		foreach ($this->documents as $document) {
41
			$tokenizer = new DocumentTokenizer($document, $this->storage);
42
			$tokens = $tokenizer->getTokens();
43
			$documentTermCount = $this->applyFilters($tokens);
44
			$this->storeDocumentTermCount($document, $documentTermCount);
45
		}
46
	}
47
48 View Code Duplication
	protected function applyFilters($tokens)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
	{
50
		foreach ($this->filters as $filterName) {
51
			$filterClassName = '\library\search\filters\\' . $filterName;
52
			$filter = new $filterClassName($tokens);
53
			$tokens = $filter->getFilterResults();
54
		}
55
		return $tokens;
56
	}
57
58
	protected function storeDocumentTermCount($document, $documentTermCount)
59
	{
60
		$db = $this->dbHandle;
61
		$sqlStart = '
62
			INSERT INTO `term_count` (`documentPath`, `term`, `count`, `field`)
63
				 VALUES ';
64
		$sql = $sqlStart;
65
		$values = array();
66
		$quotedDocumentPath = $db->quote($document->path);
0 ignored issues
show
Bug introduced by
The method quote cannot be called on $db (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
		$i = 0;
68
		foreach ($documentTermCount as $field => $countArray) {
69
			$quotedField = $db->quote($field);
0 ignored issues
show
Bug introduced by
The method quote cannot be called on $db (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
70
			foreach ($countArray as $term => $count) {
71
				$values[] = $quotedDocumentPath . ', ' . $db->quote($term) . ', ' . $db->quote($count) . ', ' . $quotedField;
0 ignored issues
show
Bug introduced by
The method quote cannot be called on $db (of type resource).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
72
				$i += 1;
73
				if ($i >= Indexer::SQLITE_MAX_COMPOUND_SELECT) {
74
					$this->executeStoreDocumentTermCount($values, $sql, $db);
75
					$values = array();
76
					$sql = $sqlStart;
77
					$i = 0;
78
				}
79
			}
80
		}
81
		if (count($values) != 0) {
82
			$this->executeStoreDocumentTermCount($values, $sql, $db);
83
		}
84
	}
85
86
	/**
87
	 * @param $values
88
	 * @param $sql
89
	 * @param $db
90
	 *
91
	 * @throws \Exception
92
	 */
93
	protected function executeStoreDocumentTermCount($values, $sql, $db)
94
	{
95
		$sql .= '(' . implode('),' . PHP_EOL . '(', $values) . ');';
96
97
		$stmt = $db->prepare($sql);
98 View Code Duplication
		if ($stmt === false || !$stmt->execute()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
			$errorInfo = $db->errorInfo();
100
			$errorMsg = $errorInfo[2];
101
			throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
102
		}
103
	}
104
}