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 ( 715b22...26de0f )
by Sebastian
01:52
created

MatchResult::group()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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
    public function __construct(string $pattern, string $subject, bool $hasMatch, array $matches)
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...
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
38
    {
39
        $matches = [];
40
41
        try {
42
            $result = preg_match($pattern, $subject, $matches);
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $result.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
43
        } catch (Exception $exception) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $exception.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
44
            throw RegexFailed::match($pattern, $subject, $exception->getMessage());
45
        }
46
47
        if ($result === false) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $result.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $pattern.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

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.
79
     *
80
     * @param int $index
81
     *
82
     * @return string
83
     *
84
     * @throws RegexFailed
85
     */
86 View Code Duplication
    public function group(int $index): string
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...
87
    {
88
        if (! isset($this->matches[$index])) {
89
            throw RegexFailed::indexedGroupDoesntExist($this->pattern, $this->subject, $index);
90
        }
91
92
        return $this->matches[$index];
93
    }
94
95
    /**
96
     * Match group by index or return default value if group doesn't exist.
97
     *
98
     * @param int $index
99
     * @param string $default
100
     *
101
     * @return string
102
     *
103
     * @throws RegexFailed
104
     */
105
    public function groupOr(int $index, $default): string
106
    {
107
        try {
108
            return $this->group($index);
109
        } catch (RegexFailed $e) {
110
            return $default;
111
        }
112
    }
113
114
    /**
115
     * Match group by name.
116
     *
117
     * @param string $groupName
118
     *
119
     * @return string
120
     *
121
     * @throws RegexFailed
122
     */
123 View Code Duplication
    public function namedGroup(string $groupName): string
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...
124
    {
125
        if (! isset($this->matches[$groupName])) {
126
            throw RegexFailed::namedGroupDoesntExist($this->pattern, $this->subject, $groupName);
127
        }
128
129
        return $this->matches[$groupName];
130
    }
131
}
132