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

InverseDocumentFrequency   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 20 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 10
loc 50
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 10 24 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: 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
}