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

InverseDocumentFrequency::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 1-3-2017
5
 * Time: 11:41
6
 */
7
8
namespace library\search\indexer;
9
10
11
class InverseDocumentFrequency
12
{
13
	/**
14
	 * @var \PDO
15
	 */
16
	protected $dbHandle;
17
	protected $documentCount;
18
19
	/**
20
	 * InverseDocumentFrequency constructor.
21
	 *
22
	 * @param resource $dbHandle
23
	 * @param int      $documentCount
24
	 */
25
	public function __construct($dbHandle, $documentCount)
26
	{
27
		$this->dbHandle = $dbHandle;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbHandle of type resource is incompatible with the declared type object<PDO> of property $dbHandle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
		$this->documentCount = $documentCount;
29
	}
30
31
	/**
32
	 * Formula to calculate:
33
	 * 		idf(t) = 1 + log ( totalDocuments / (documentsThatContainTheTerm + 1))
34
	 * @throws \Exception
35
	 */
36
	public function execute()
37
	{
38
		$db = $this->dbHandle;
39
		$db->sqliteCreateFunction('log', 'log', 1);
40
		$sql = '
41
		INSERT INTO inverse_document_frequency (term, inverseDocumentFrequency)
42
		SELECT DISTINCT term, (1+(log(:documentCount / COUNT(documentPath) + 1))) as inverseDocumentFrequency
43
					  FROM term_count
44
				  GROUP BY term
45
		';
46
47 View Code Duplication
		if (!$stmt = $db->prepare($sql)) {
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...
48
			$errorInfo = $db->errorInfo();
49
			$errorMsg = $errorInfo[2];
50
			throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
51
		}
52
		$stmt->bindValue(':documentCount', $this->documentCount);
53
		$result = $stmt->execute();
54 View Code Duplication
		if ($result === false) {
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...
55
			$errorInfo = $db->errorInfo();
56
			$errorMsg = $errorInfo[2];
57
			throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
58
		}
59
	}
60
}