Completed
Push — master ( 7665c8...9af7b9 )
by Robbert
61:21 queued 52:46
created

mysql_workspaces_compiler::priv_sql_compile()   F

Complexity

Conditions 16
Paths 3584

Size

Total Lines 103
Code Lines 69

Duplication

Lines 11
Ratio 10.68 %

Code Coverage

Tests 47
CRAP Score 44.9353

Importance

Changes 0
Metric Value
cc 16
eloc 69
c 0
b 0
f 0
nc 3584
nop 1
dl 11
loc 103
ccs 47
cts 91
cp 0.5165
crap 44.9353
rs 2

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
  include_once($this->code."stores/modules/mysql_compiler.php");
3
4
class mysql_workspaces_compiler extends mysql_compiler {
5
6 47
	public function __construct (&$store, $tbl_prefix="") {
7 47
		debug("mysql_workspaces_compiler($tbl_prefix)", "store");
8 47
		$this->tbl_prefix=$tbl_prefix;
9 47
		$this->store=$store;
10 47
	}
11
12
	// mysql specific compiler function
13 47
	protected function priv_sql_compile($tree) {
14 47
		$this->custom_ref = 0;
15 47
		$this->custom_id = 0;
16 47
		$this->used_tables="";
17 47
		$this->compile_tree($tree);
18 47
		$nodes=$this->tbl_prefix."nodes";
19 47
		$objects=$this->tbl_prefix."objects";
20 47
		$properties=$this->tbl_prefix."prop_";
21 47
		$this->used_tables[$nodes]=$nodes;
22 47
		$this->used_tables[$objects]=$objects;
23 47 View Code Duplication
		if ($this->join_target_properties) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->join_target_properties of type array<*,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
24
			$this->used_tables[$properties."references as target_reference"] = $properties."references as target_reference";
25
			$this->used_tables["$nodes as target"] = "$nodes as target";
26
		}
27 47
		@reset($this->used_tables);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
28 47
		while (list($key, $val)=each($this->used_tables)) {
29 47
			if ($tables) {
30 47
				$tables.=", $key";
0 ignored issues
show
Bug introduced by
The variable $tables 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...
31 47
			} else {
32 47
				$tables="$key";
33
			}
34 47 View Code Duplication
			if ($this->select_tables[$key]) {
35 46
				if ($this->join_target_properties[$key]) {
36
					$prop_dep.=" and $val.object=target.object ";
0 ignored issues
show
Bug introduced by
The variable $prop_dep 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...
37
				} else {
38 46
					$prop_dep.=" and $val.object=$objects.id ";
39
				}
40 46
			}
41 47
		}
42
43 47
		$join = "";
44 47
		if (is_array($this->nls_join)) {
45
			reset($this->nls_join);
46
			while (list($key, $value)=each($this->nls_join)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
47
				$join .= $value;
48
			}
49
		}
50
51
52
53 47
		$query = " where $nodes.object=$objects.id $prop_dep";
54 47
		$query .= " and $nodes.path like '".str_replace('_','\\_',AddSlashes($this->path))."%' ";
55
56
57 47
		$query_string_layers = "";
58 47
		$layering = false;
59 47
		foreach ($this->layers as $lPath => $lId) {
0 ignored issues
show
Bug introduced by
The property layers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
			if ($lId) {
61
				$layering = true;
62
				if ($query_string_layers) {
63
					$query_string_layers .= " OR ";
64
				}
65
				$query_string_layers .= " ( $nodes.path like '".AddSlashes($lPath)."%' and $nodes.layer = ".((int)$lId)." ) ";
66
			}
67 47
		}
68 47
		if ($layering) {
69
			$query .= " and (
70
								( $query_string_layers )
71
							OR
72
								$nodes.layer = 0
73
								and $nodes.id NOT IN (
74
									select $nodes.id from $nodes where ( $query_string_layers )
75
								)
76
								and $nodes.path NOT IN (
77
									select $nodes.path from $nodes where ( $query_string_layers )
78
								)
79
						)
80
			";
81
		} else {
82 47
			$query .= " and $nodes.layer = 0 ";
83
		}
84
85 47
		if ($this->where_s) {
86 47
			$query.=" and ( $this->where_s ) ";
87 47
		}
88 47
		if ($this->where_s_ext) {
89
			$query .= " and ($this->where_s_ext) ";
90
		}
91
		/* do target join */
92 47
		if ($this->join_target_properties) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->join_target_properties of type array<*,string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
			$query .= " and $objects.id = target_reference.object ";
94
			$query .= " and target.path = target_reference.AR_path ";
95
		}
96
97 47
		if ($this->orderby_s) {
98
			$orderby = " order by $this->orderby_s, $nodes.parent ASC, $nodes.priority DESC, $nodes.path ASC ";
99
		} else {
100 47
			$orderby = " order by $nodes.parent ASC, $nodes.priority DESC, $nodes.path ASC ";
101
		}
102
103 47
		$select_query  = "select distinct($nodes.path), $nodes.id as nodeId, $nodes.layer as nodeLayer, $nodes.parent, $nodes.priority, ";
104 47
		$select_query .= "$objects.object, $objects.id, $objects.type, $objects.vtype, ";
105 47
		$select_query .= "UNIX_TIMESTAMP($objects.lastchanged) as lastchanged  ";
106 47
		$select_query .= "from ($tables) $join $query ";
107 47
		$select_query .= $orderby." ".$this->limit_s;
108
109 47
		$count_query   = "select count(distinct($objects.id)) as count from $tables ".$query;
110
111
		return array(
112 47
			"select_query" => $select_query,
113
			"count_query"  => $count_query
114 47
		);
115
	}
116
117
  }
118