Completed
Push — master ( d2fca4...0a5cf4 )
by
unknown
03:14
created

EcommerceSearchHistoryFormField::Field()   D

Complexity

Conditions 15
Paths 192

Size

Total Lines 90
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
dl 0
loc 90
rs 4.597
c 0
b 0
f 0
eloc 50
nc 192
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class EcommerceSearchHistoryFormField 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
     * total number days to search back.
7
     *
8
     * @var int
9
     */
10
    protected $numberOfDays = 100;
11
12
    /**
13
     * how many days ago the data-analysis should end.
14
     *
15
     * @var int
16
     */
17
    protected $endingDaysBack = 0;
18
19
    /**
20
     * minimum number of searches for the data to show up.
21
     *
22
     * @var int
23
     */
24
    protected $minimumCount = 30;
25
26
    /**
27
     * maximum number of searches for the data to show up.
28
     *
29
     * @var int
30
     */
31
    protected $maxRows = 20;
32
33
    /**
34
     *
35
     * @var bool
36
     */
37
    protected $addTitle = true;
38
39
    /**
40
     *
41
     * @var bool
42
     */
43
    protected $addAtoZ = true;
44
45
    /**
46
     * minimum number of searches for the data to show up.
47
     *
48
     * @var bool
49
     */
50
    protected $showMoreLink = false;
51
52
    public function __construct($name, $title = '')
53
    {
54
        return parent::__construct($name, $title);
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...
55
    }
56
57
    /**
58
     * @param int
59
     *
60
     * @return EcommerceSearchHistoryFormField
61
     */
62
    public function setNumberOfDays($days)
63
    {
64
        $this->numberOfDays = intval($days);
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param int
71
     *
72
     * @return EcommerceSearchHistoryFormField
73
     */
74
    public function setMinimumCount($count)
75
    {
76
        $this->minimumCount = intval($count);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param int
83
     *
84
     * @return EcommerceSearchHistoryFormField
85
     */
86
    public function setShowMoreLink($b)
87
    {
88
        $this->showMoreLink = $b;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param int
95
     *
96
     * @return EcommerceSearchHistoryFormField
97
     */
98
    public function setEndingDaysBack($count)
99
    {
100
        $this->endingDaysBack = intval($count);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param int
107
     *
108
     * @return EcommerceSearchHistoryFormField
109
     */
110
    public function setMaxRows($number)
111
    {
112
        $this->maxRows = $number;
113
114
        return $this;
115
    }
116
117
    /**
118
     * @param bool
119
     *
120
     * @return EcommerceSearchHistoryFormField
121
     */
122
    public function setAddTitle($b)
123
    {
124
        $this->addTitle = $b;
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param bool
131
     *
132
     * @return EcommerceSearchHistoryFormField
133
     */
134
    public function setAddAtoZ($b)
135
    {
136
        $this->addAtoZ = $b;
137
138
        return $this;
139
    }
140
141
142
    public function FieldHolder($properties = array())
143
    {
144
        return $this->Field($properties);
145
    }
146
147
    public function Field($properties = array())
148
    {
149
        $title = $this->getContent();
150
        $totalNumberOfDaysBack = $this->numberOfDays + $this->endingDaysBack;
151
        $data = DB::query('
152
            SELECT COUNT(ID) myCount, "Title"
153
            FROM "SearchHistory"
154
            WHERE Created > ( NOW() - INTERVAL '.$totalNumberOfDaysBack.' DAY )
155
                AND Created < ( NOW() - INTERVAL '.$this->endingDaysBack." DAY )
156
            GROUP BY \"Title\"
157
            HAVING COUNT(\"ID\") >= $this->minimumCount
158
            ORDER BY myCount DESC
159
            LIMIT ".$this->maxRows."
160
        ");
161
        if (!$this->minimumCount) {
162
            ++$this->minimumCount;
163
        }
164
        $content = '';
165
        $tableContent = '';
166
        if ($title && $this->addTitle) {
167
            $content .= '<h3>'.$title.'</h3>';
168
        }
169
        $content .= '
170
        <div id="SearchHistoryTableForCMS">
171
            <h3>
172
                Search Phrases'
173
                .($this->minimumCount > 1 ? ', entered at least '.$this->minimumCount.' times' : '')
174
                .($this->maxRows < 1000 ? ', limited to '.$this->maxRows.' entries, ' : '')
175
                .' between '.date('j-M-Y', strtotime('-'.$totalNumberOfDaysBack.' days')).' and '.date('j-M-Y', strtotime('-'.$this->endingDaysBack.' days')).'
176
            </h3>';
177
        $count = 0;
178
        if ($data && count($data)) {
179
            $tableContent .= '
180
                <table class="highToLow" style="widht: 100%">';
181
            $list = array();
182
            foreach ($data as $key => $row) {
183
                $count++;
184
                //for the highest count, we work out a max-width
185
                if (!$key) {
186
                    $maxWidth = $row['myCount'];
187
                }
188
                $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...
189
                $list[$row['myCount'].'-'.$key] = $row['Title'];
190
                $tableContent .= '
191
                    <tr>
192
                        <td style="text-align: right; width: 30%; padding: 5px;">'.$row['Title'].'</td>
193
                        <td style="background-color: silver;  padding: 5px; width: 70%;">
194
                            <div style="width: '.$multipliedWidthInPercentage.'%; background-color: #C51162; color: #fff;">'.$row['myCount'].'</div>
195
                        </td>
196
                    </tr>';
197
            }
198
            $tableContent .= '
199
                </table>';
200
            if ($count && $this->addAtoZ) {
201
                asort($list);
202
                $tableContent .= '
203
                    <h3>A - Z</h3>
204
                    <table class="aToz" style="widht: 100%">';
205
                foreach ($list as $key => $title) {
206
                    $array = explode('-', $key);
207
                    $multipliedWidthInPercentage = floor(($array[0] / $maxWidth) * 100);
208
                    $tableContent .= '
209
                        <tr>
210
                            <td style="text-align: right; width: 30%; padding: 5px;">'.$title.'</td>
211
                            <td style="background-color: silver;  padding: 5px; width: 70%">
212
                                <div style="width: '.$multipliedWidthInPercentage.'%; background-color: #004D40; color: #fff;">'.trim($array[0]).'</div>
213
                            </td>
214
                        </tr>';
215
                }
216
                $tableContent .= '
217
                    </table>';
218
            }
219
        }
220
        if ($count === 0) {
221
            //we replace table content here...
222
            $tableContent = '<p class="warning message">No searches found.</p>';
223
        }
224
        $content .= $tableContent;
225
        if ($this->showMoreLink) {
226
            $content .= '
227
            <p>
228
                <a href="/dev/tasks/EcommerceTaskReviewSearches/">Query more resuts</a>
229
            </p>';
230
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
231
        }
232
        $content .= '
233
        </div>';
234
235
        return $content;
236
    }
237
}
238