Passed
Push — main ( 607587...1999b9 )
by Will
03:01
created

frameworks::get()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 36
ccs 30
cts 30
cp 1
crap 6
rs 8.8977
1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
class frameworks {
6
7
	/**
8
	 * Generates a configuration array for matching app frameworks
9
	 * 
10
	 * @return array<string,props> An array with keys representing the string to match, and values a props object defining how to generate the match and which properties to set
11
	 */
12 6
	public static function get() : array {
13 1
		return [
14 1
			'Electron/' => new props('start', fn (string $value) : array => [
15 5
				'framework' => 'Electron',
16 5
				'frameworkversion' => \explode('/', $value, 3)[1]
17 5
			]),
18 1
			'MAUI' => new props('start', [
19 1
				'framework' => 'MAUI'
20 1
			]),
21 1
			'Cordova' => new props('start', fn (string $value) : array => [
22 2
				'framework' => 'Cordova',
23 2
				'frameworkversion' => \explode('/', $value, 3)[1]
24 2
			]),
25 1
			'Alamofire/' => new props('start', fn (string $value) : array => [
26 1
				'framework' => 'Alamofire',
27 1
				'frameworkversion' => \explode('/', $value, 3)[1]
28 1
			]),
29 1
			'.NET CLR ' => new props('start', function (string $value, int $i, array $tokens) : array {
30
31
				// find latest version as often specifdies many versions
32 6
				$levels = [];
33 6
				for ($n = $i; $n < \count($tokens); $n++) { // may happen multiple times if there are multiple captures, but only look forward as only the first will be written
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
34 6
					if (\mb_stripos($tokens[$n], '.NET CLR ') === 0) {
35 6
						$ver = \array_map('\\intval', \explode('.', \mb_substr($tokens[$n], 9)));
36 6
						foreach ($ver AS $key => $item) {
37 6
							if ($item > ($levels[$key] ?? 0)) {
38 6
								$levels = $ver;
39 6
							} elseif ($item < ($levels[$key] ?? 0)) {
40 4
								break;
41
							}
42
						}
43
					}
44
				}
45 6
				return [
46 6
					'framework' => '.NET Common Language Runtime',
47 6
					'frameworkversion' => \implode('.', $levels)
48 6
				];
49 1
			}),
50 1
		];
51
	}
52
}