Passed
Branch develop (56c45f)
by Jens
02:42
created

InverseDocumentFrequency   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 22 3
1
<?php
2
/**
3
 * User: jensk
4
 * Date: 1-3-2017
5
 * Time: 11:41
6
 */
7
8
namespace CloudControl\Cms\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 \PDO $dbHandle
23
     * @param int $documentCount
24
     */
25
    public function __construct($dbHandle, $documentCount)
26
    {
27
        $this->dbHandle = $dbHandle;
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);
0 ignored issues
show
Bug introduced by
The method sqliteCreateFunction() does not exist on PDO. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $db->/** @scrutinizer ignore-call */ 
40
             sqliteCreateFunction('log', 'log', 1);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
        if (!$stmt = $db->prepare($sql)) {
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
        if ($result === false) {
55
            $errorInfo = $db->errorInfo();
56
            $errorMsg = $errorInfo[2];
57
            throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
58
        }
59
    }
60
}