DefaultUseStatementComparator   A
last analyzed

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