Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

DefaultUseStatementComparator::compare()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 12
nc 6
nop 2
crap 5
1
<?php
2
namespace gossi\codegen\generator\comparator;
3
4
use phootwork\lang\Comparator;
5
6
/**
7
 * Default use statement comparator
8
 * 
9
 * Compares use statements case-sensitive, with lower-case beeing sorted first
10
 */
11
class DefaultUseStatementComparator implements Comparator {
12
13 1
	public function compare($a, $b) {
14
		// find first difference
15 1
		$cmp1 = null;
16 1
		$cmp2 = null;
17 1
		$min = min(strlen($a), strlen($b));
18 1
		for ($i = 0; $i < $min; $i++) {
19 1
			if ($a[$i] != $b[$i]) {
20 1
				$cmp1 = $a[$i];
21 1
				$cmp2 = $b[$i];
22 1
				break;
23
			}
24
		}
25
		
26 1
		if ($cmp1 === null && $cmp2 === null) {
27 1
			return 0;
28
		}
29
		
30 1
		return $this->getAscii($cmp1) - $this->getAscii($cmp2);
31
	}
32
	
33 1
	private function getAscii($str) {
34 1
		$ord = ord($str);
35 1
		if ($ord >= 65 && $ord <= 90) {
36 1
			$ord += 32;
37 1
		} else if ($ord >= 97 && $ord <= 122) {
38 1
			$ord -= 32;
39
		}
40 1
		return $ord;
41
	}
42
43
}