Completed
Pull Request — master (#526)
by Michael
16:45 queued 06:57
created

QueryBrowser::executeQueryToTable()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 55
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 36
nop 1
dl 0
loc 55
ccs 0
cts 40
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

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
**********      English Wikipedia Account Request Interface      **********
4
***************************************************************************
5
** Wikipedia Account Request Graphic Design by Charles Melbye,           **
6
** which is licensed under a Creative Commons                            **
7
** Attribution-Noncommercial-Share Alike 3.0 United States License.      **
8
**                                                                       **
9
** All other code are released under the Public Domain                   **
10
** by the ACC Development Team.                                          **
11
**                                                                       **
12
** See CREDITS for the list of developers.                               **
13
***************************************************************************/
14
15
class QueryBrowser
16
{
17
	/**
18
	 * @var boolean
19
	 */
20
	public $numberedList = false;
21
22
	/**
23
	 * @var string
24
	 */
25
	public $numberedListTitle = "#";
26
27
	/**
28
	 * @var boolean|callable
29
	 */
30
	public $tableCallbackFunction = false;
31
32
	/**
33
	 * @var boolean|string[]
34
	 */
35
	public $overrideTableTitles = false;
36
37
	/**
38
	 * @var int
39
	 */
40
	public $rowFetchMode = PDO::FETCH_ASSOC;
41
42
	/**
43
	 * @param string $query
44
	 * @return string
45
	 */
46
	public function executeQueryToTable($query)
47
	{
48
		$out = "";
49
50
		$results = $this->executeQueryToArray($query);
51
52
		$out .= '<table class="table table-striped table-hover table-condensed"><tr>';
53
54
		if ($this->numberedList == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
55
			$out .= "<th>" . $this->numberedListTitle . "</th>";
56
		}
57
58
		if ($this->overrideTableTitles != false) {
59
			foreach ($this->overrideTableTitles as $value) {
60
				$out .= "<th>" . $value . "</th>"; 
61
			}
62
		}
63
		else {
64
			if (count($results) > 0) {
65
				foreach ($results[0] as $k => $v) {
66
					$out .= "<th>" . $k . "</th>"; 
67
				}
68
			}
69
		}
70
		$out .= "</tr>";
71
		
72
		
73
		$currentreq = 0;
74
		foreach ($results as $row) {
75
			$currentreq++;
76
			if (function_exists($this->tableCallbackFunction)) {
0 ignored issues
show
Bug introduced by
It seems like $this->tableCallbackFunction can also be of type callable; however, parameter $function_name of function_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
			if (function_exists(/** @scrutinizer ignore-type */ $this->tableCallbackFunction)) {
Loading history...
77
				$out .= call_user_func($this->tableCallbackFunction, $row, $currentreq);	
0 ignored issues
show
Bug introduced by
It seems like $this->tableCallbackFunction can also be of type boolean; however, parameter $function of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

77
				$out .= call_user_func(/** @scrutinizer ignore-type */ $this->tableCallbackFunction, $row, $currentreq);	
Loading history...
78
			}
79
			else {
80
				$out .= '<tr>';
81
                
82
				if ($this->numberedList == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
83
					$out .= "<th>" . $currentreq . "</th>";
84
				}
85
				
86
				
87
				foreach ($row as $cell) {
88
	
89
					$out .= "<td>" . $cell . "</td>";
90
				}
91
	
92
				
93
				$out .= "</tr>";
94
			}
95
			
96
		}
97
		
98
		$out .= "</table>";
99
		
100
		return $out;
101
	}
102
	
103
	public function executeQueryToArray($query)
104
	{
105
		$database = gGetDb();
106
        
107
		$statement = $database->prepare($query);
108
        
109
		$statement->execute();
110
        
111
		return $statement->fetchAll($this->rowFetchMode);
112
	}
113
	
114
}
115