Passed
Push — main ( e55150...6ce6d7 )
by Will
02:49
created

props::match()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 7.6666
cc 10
nc 10
nop 3
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
namespace hexydec\agentzero;
4
5
class props {
6
7
	protected string $type;
8
	protected array|\Closure $props;
9
10 1
	public function __construct(string $type, array|\Closure $props) {
11 1
		$this->type = $type;
12 1
		$this->props = $props;
13
	}
14
15 87
	public function match(\stdClass $obj, string $search, array $tokens) {
16 87
		$type = $this->type;
17 87
		$searchlower = \mb_strtolower($search);
18 87
		foreach ($tokens AS $i => $token) {
19 87
			$tokenlower = \mb_strtolower($token);
20
			switch ($type) {
21
22
				// match from start of string
23 87
				case 'start':
24 87
					if (\str_starts_with($tokenlower, $searchlower)) {
25 87
						$this->set($obj, $token, $i, $tokens, $search);
26
					}
27 87
					break;
28
29
				// match anywhere in the string
30 87
				case 'any':
31 87
					if (\str_contains($tokenlower, $searchlower)) {
32 85
						$this->set($obj, $token, $i, $tokens, $search);
33
					}
34 87
					break;
35
36
				// match end of token
37 87
				case 'end':
38 87
					if (\str_ends_with($tokenlower, $searchlower)) {
39 11
						$this->set($obj, $token, $i, $tokens, $search);
40
					}
41 87
					break;
42
43
				// match anywhere in the string
44 87
				case 'exact':
45 87
					if ($tokenlower === $searchlower) {
46 74
						$this->set($obj, $token, $i, $tokens, $search);
47 74
						break 2;
48
					} else {
49 87
						break;
50
					}
51
			}
52
		}
53
	}
54
55
	/**
56
	 * Sets parsed UA properties, and calls callbacks to generate properties and sets them to the output object
57
	 * 
58
	 * @param \stdClass $browser A stdClass object to which the properties will be set
59
	 * @param MatchValue|\Closure $props An array of properties or a Closure to generate properties
0 ignored issues
show
Bug introduced by
The type hexydec\agentzero\MatchValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
60
	 * @param string $value The current token value
61
	 * @param int $i The ID of the current token
62
	 * @param array<string> &$tokens The tokens array
63
	 * @return void
64
	 */
65 87
	public function set(\stdClass $obj, string $value, int $i, array $tokens, string $key) : void {
66 87
		$props = $this->props;
67 87
		if ($props instanceof \Closure) {
68 87
			$props = $props($value, $i, $tokens, $key);
69
		}
70 87
		if (\is_array($props)) {
71 87
			foreach ($props AS $key => $item) {
72 87
				if ($item !== null && !isset($obj->{$key})) {
73 87
					$obj->{$key} = $item;
74
				}
75
			}
76
		}
77
	}
78
}