GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( e33c02...9b7506 )
by Sebastian
01:43
created

MatchResult::groups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\Regex;
4
5
use Exception;
6
7
class MatchResult extends RegexResult
8
{
9
    /** @var string */
10
    protected $pattern;
11
12
    /** @var string */
13
    protected $subject;
14
15
    /** @var bool */
16
    protected $hasMatch;
17
18
    /** @var array */
19
    protected $matches;
20
21 View Code Duplication
    public function __construct(string $pattern, string $subject, bool $hasMatch, array $matches)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
    {
23
        $this->pattern = $pattern;
24
        $this->subject = $subject;
25
        $this->hasMatch = $hasMatch;
26
        $this->matches = $matches;
27
    }
28
29
    /**
30
     * @param string $pattern
31
     * @param string $subject
32
     *
33
     * @return static
34
     *
35
     * @throws \Spatie\Regex\RegexFailed
36
     */
37 View Code Duplication
    public static function for(string $pattern, string $subject)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $matches = [];
40
41
        try {
42
            $result = preg_match($pattern, $subject, $matches);
43
        } catch (Exception $exception) {
44
            throw RegexFailed::match($pattern, $subject, $exception->getMessage());
45
        }
46
47
        if ($result === false) {
48
            throw RegexFailed::match($pattern, $subject, static::lastPregError());
49
        }
50
51
        return new static($pattern, $subject, $result, $matches);
0 ignored issues
show
Documentation introduced by
$result is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54
    public function hasMatch(): bool
55
    {
56
        return $this->hasMatch;
57
    }
58
59
    /**
60
     * @return string|null
61
     */
62
    public function result()
63
    {
64
        return $this->matches[0] ?? null;
65
    }
66
67
    /**
68
     * @param string $default
69
     *
70
     * @return string
71
     */
72
    public function resultOr($default)
73
    {
74
        return $this->result() ?? $default;
75
    }
76
77
    /**
78
     * Match group by index or name.
79
     *
80
     * @param int|string $group
81
     *
82
     * @return string
83
     *
84
     * @throws RegexFailed
85
     */
86
    public function group($group): string
87
    {
88
        if (!isset($this->matches[$group])) {
89
            throw RegexFailed::groupDoesntExist($this->pattern, $this->subject, $group);
90
        }
91
92
        return $this->matches[$group];
93
    }
94
95
    /**
96
     * Return an array of the matches
97
     *
98
     * @return array
99
     */
100
    public function groups(): array
101
    {
102
        return $this->matches;
103
    }
104
105
    /**
106
     * Match group by index or return default value if group doesn't exist.
107
     *
108
     * @param int|string $group
109
     * @param string     $default
110
     *
111
     * @return string
112
     */
113
    public function groupOr($group, $default): string
114
    {
115
        try {
116
            return $this->group($group);
117
        } catch (RegexFailed $e) {
118
            return $default;
119
        }
120
    }
121
122
    /**
123
     * Match group by index or name.
124
     *
125
     * @param int|string $group
126
     *
127
     * @return string
128
     *
129
     * @throws RegexFailed
130
     */
131
    public function namedGroup($group): string
132
    {
133
        return $this->group($group);
134
    }
135
}
136