Passed
Push — 1.x ( 885d6a...11c6d3 )
by Kevin
01:56
created

UrlMatches::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Zenstruck\Browser\Test\Constraint;
4
5
use PHPUnit\Framework\Constraint\Constraint;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Constraint\Constraint 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...
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 *
10
 * @internal
11
 */
12
final class UrlMatches extends Constraint
13
{
14
    private array $partsToMatch;
15
    private array $url;
16
17
    public function __construct(string $url, array $partsToMatch = [])
18
    {
19
        $this->partsToMatch = $partsToMatch;
20
        $this->url = $this->parseUrl($url);
21
    }
22
23
    public function toString(): string
24
    {
25
        return 'matches '.$this->exporter()->export($this->url);
26
    }
27
28
    protected function matches($other): bool
29
    {
30
        return $this->parseUrl($other) === $this->url;
31
    }
32
33
    protected function failureDescription($other): string
34
    {
35
        return parent::failureDescription($this->parseUrl($other));
36
    }
37
38
    private function parseUrl(string $url): array
39
    {
40
        $parts = \parse_url($url);
41
42
        if (empty($this->partsToMatch)) {
43
            return $parts;
44
        }
45
46
        foreach (\array_keys($parts) as $part) {
47
            if (!\in_array($part, $this->partsToMatch, true)) {
48
                unset($parts[$part]);
49
            }
50
        }
51
52
        return $parts;
53
    }
54
}
55