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

DefaultUseStatementComparator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 33
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compare() 0 19 5
B getAscii() 0 9 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
}