QueryFormatter::append()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 10
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Asparagus;
4
5
/**
6
 * Formatter for SPARQL queries
7
 *
8
 * @since 0.1
9
 *
10
 * @license GNU GPL v2+
11
 * @author Bene* < [email protected] >
12
 */
13
class QueryFormatter {
14
15
	/**
16
	 * @var string[]
17
	 */
18
	private $formattedParts;
19
20
	/**
21
	 * @var int
22
	 */
23
	private $indentationLevel;
24
25
	/**
26
	 * @var string[]
27
	 */
28
	private $replacements;
29
30
	/**
31
	 * Formats the given SPARQL string.
32
	 * Note that there have to be spaces before and after brackets and dots.
33
	 *
34
	 * @param string $sparql
35
	 * @return string
36
	 */
37
	public function format( $sparql ) {
38
		$this->formattedParts = array();
39
		$this->indentationLevel = 0;
40
		$this->replacements = array();
41
42
		$regexHelper = new RegexHelper();
43
		$sparql = $regexHelper->escapeSequences( $sparql, $this->replacements );
44
45
		foreach ( $this->split( $sparql ) as $part ) {
46
			if ( !empty( $this->formattedParts ) ) {
47
				$this->before( $part );
48
			}
49
50
			$this->indentation( $part );
51
			$this->append( $part );
52
			$this->after( $part );
53
		}
54
55
		$this->trimEnd();
56
		$this->formattedParts[] = "\n";
57
58
		return strtr( implode( $this->formattedParts ), $this->replacements );
0 ignored issues
show
Bug introduced by
It seems like $this->replacements can also be of type null; however, strtr() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
	}
60
61
	private function split( $string ) {
62
		return preg_split(
63
			'/(\W)/',
64
			$string,
65
			-1,
66
			PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
67
		);
68
	}
69
70
	private function before( $part ) {
71
		if ( in_array( $part, array( 'PREFIX', 'FILTER', 'OPTIONAL', 'LIMIT', 'GROUP', 'ORDER', 'HAVING', '}' ) ) ) {
72
			$this->trimEnd();
73
			$this->formattedParts[] = "\n";
74
		}
75
76
		if ( $part === 'SELECT' && $this->indentationLevel === 0 ) {
77
			$this->trimEnd();
78
			$this->formattedParts[] = "\n\n";
79
		}
80
81
		// UNION is the only part we want to have in-line with "}", ie. } UNION {
82
		if ( $part === 'UNION' || end( $this->formattedParts ) !== "\n" &&
83
			in_array( $part, array( '.', '=', '(', '<', '{', '?', '$' ) )
84
		) {
85
			$this->trimEnd();
86
			$this->append( ' ' );
87
		}
88
	}
89
90
	private function indentation( $part ) {
91
		if ( $part === '}' ) {
92
			$this->indentationLevel--;
93
		}
94
95
		if ( !ctype_space( $part ) && substr( end( $this->formattedParts ), 0, 1 ) === "\n" ) {
96
			$this->formattedParts[] = str_repeat( "\t", $this->indentationLevel );
97
		}
98
99
		if ( $part === '{' ) {
100
			$this->indentationLevel++;
101
		}
102
	}
103
104
	private function append( $part ) {
105
		if ( !ctype_space( $part ) ) {
106
			$this->formattedParts[] = $part;
107
		} else if ( // ctype_space( $part ) &&
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
			!ctype_space( end( $this->formattedParts ) ) &&
109
			end( $this->formattedParts ) !== '('
110
		) {
111
			$this->formattedParts[] = ' ';
112
		}
113
	}
114
115
	private function after( $part ) {
116
		if ( in_array( $part, array( '{', '}', '.' ) ) ) {
117
			$this->formattedParts[] = "\n";
118
		}
119
120
		if ( $part === ';' ) {
121
			$this->formattedParts[] = "\n\t";
122
		}
123
	}
124
125
	private function trimEnd() {
126
		while ( ctype_space( end( $this->formattedParts ) ) ) {
127
			array_pop( $this->formattedParts );
128
		}
129
	}
130
131
}
132