setNumberOfDays()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
class GoogleCustomSearchPage_RecordField extends LiteralField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    /**
7
     * total number days to search back
8
     * @var Int
9
     */
10
    protected $numberOfDays = 100;
11
12
    /**
13
     * how many days ago the data-analysis should end
14
     * @var Int
15
     */
16
    protected $endingDaysBack = 0;
17
18
    /**
19
     * minimum number of searches for the data to show up
20
     * @var Int
21
     */
22
    protected $minimumCount = 1;
23
24
    public function __construct($name, $title = "")
25
    {
26
        $totalNumberOfDaysBack = $this->numberOfDays + $this->endingDaysBack;
27
        $data = DB::query("
28
			SELECT COUNT(ID) myCount, \"Title\"
29
			FROM \"GoogleCustomSearchPage_Record\"
30
			WHERE Created > ( NOW() - INTERVAL ".$totalNumberOfDaysBack." DAY )
31
				AND Created < ( NOW() - INTERVAL ".$this->endingDaysBack." DAY )
32
			GROUP BY \"Title\"
33
			HAVING COUNT(\"ID\") >= $this->minimumCount
34
			ORDER BY myCount DESC
35
		");
36
        if (!$this->minimumCount) {
37
            $this->minimumCount++;
38
        }
39
        $content = "";
40
        if ($title) {
41
            $content .= "<h2>".$title."</h2>";
42
        }
43
        $content .= "
44
		<div id=\"SearchHistoryTableForCMS\">
45
			<h3>Search Phrases entered at least ".$this->minimumCount." times between ".date("Y-M-d", strtotime("-".$totalNumberOfDaysBack." days"))." and ".date("Y-M-d", strtotime("-".$this->endingDaysBack." days"))."</h3>
46
			<table class=\"highToLow\" style=\"width: 100%;\">";
47
        $list = array();
48
        foreach ($data as $key => $row) {
49
            if (!$key) {
50
                $maxWidth = $row["myCount"];
51
            }
52
            $multipliedWidthInPercentage = floor(($row["myCount"] / $maxWidth)* 100);
0 ignored issues
show
Bug introduced by
The variable $maxWidth does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
53
            $list[$row["myCount"]."-".$key] = $row["Title"];
54
            $content .='
55
				<tr>
56
					<td style="text-align: right; width: 30%; padding: 5px;">
57
						'.$row["Title"].'
58
					</td>
59
					<td style="background-color: silver;  padding: 5px; width: 70%;">
60
						<div style="width: '.$multipliedWidthInPercentage.'%; background-color: #0066CC; color: #fff;">'.$row["myCount"].'</div>
61
					</td>
62
				</tr>';
63
        }
64
        $content .= '
65
			</table>';
66
        //a - z
67
        asort($list);
68
        $content .= "
69
			<h3>A - Z with favourite links clicked</h3>
70
			<table class=\"aToz\" style=\"width: 100%;\">";
71
        foreach ($list as $key => $title) {
72
            $array = explode("-", $key);
73
            $multipliedWidthInPercentage = floor(($array[0] / $maxWidth)* 100);
74
            $titleData = DB::query("
75
				SELECT COUNT(ID) myCount, \"URL\"
76
				FROM \"GoogleCustomSearchPage_Record\"
77
				WHERE Created > ( NOW() - INTERVAL ".$totalNumberOfDaysBack." DAY )
78
					AND Created < ( NOW() - INTERVAL ".$this->endingDaysBack." DAY )
79
					AND \"Title\" = '".$title."'
80
				GROUP BY \"URL\"
81
				HAVING COUNT(\"ID\") >= $this->minimumCount
82
				ORDER BY myCount DESC
83
				LIMIT 3;
84
			");
85
            unset($urlArray);
86
            $urlArray = array();
87
            foreach ($titleData as $urlRow) {
88
                $urlArray[] = "<li>".$urlRow["myCount"].": ".$urlRow["URL"]."</li>";
89
            }
90
            $content .='
91
				<tr>
92
					<td style="text-align: right; width: 30%; padding: 5px;">'.$title.'</td>
93
					<td style="background-color: silver;  padding: 5px; width: 70%">
94
						<div style="width: '.$multipliedWidthInPercentage.'%; background-color: #0066CC; color: #fff;">'.trim($array[0]).'</div>
95
						<ul style="font-size: 10px">'.implode($urlArray).'</ul>
96
					</td>
97
				</tr>';
98
        }
99
        $content .= '
100
			</table>
101
		</div>';
102
        return parent::__construct($name, $content);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
103
    }
104
105
    /**
106
     * @param Int
107
     * @return Field
0 ignored issues
show
Documentation introduced by
Should the return type not be GoogleCustomSearchPage_RecordField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
108
     */
109
    public function setNumberOfDays($days)
110
    {
111
        $this->numberOfDays = intval($days);
112
        return $this;
113
    }
114
115
    /**
116
     * @param Int
117
     * @return Field
0 ignored issues
show
Documentation introduced by
Should the return type not be GoogleCustomSearchPage_RecordField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
118
     */
119
    public function setMinimumCount($count)
120
    {
121
        $this->minimumCount = intval($count);
122
        return $this;
123
    }
124
125
    /**
126
     * @param Int
127
     * @return Field
0 ignored issues
show
Documentation introduced by
Should the return type not be GoogleCustomSearchPage_RecordField?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
128
     */
129
    public function setEndingDaysBack($count)
130
    {
131
        $this->endingDaysBack = intval($count);
132
        return $this;
133
    }
134
}
135