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 ( eefe90...cb5099 )
by Sebastian
05:58
created

MatchResult::for()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
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
     * Match group by index.
69
     *
70
     * @param int $index
71
     *
72
     * @return string
73
     *
74
     * @throws RegexFailed
75
     */
76 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...
77
    {
78
        if (! isset($this->matches[$index])) {
79
            throw RegexFailed::indexedGroupDoesntExist($this->pattern, $this->subject, $index);
80
        }
81
82
        return $this->matches[$index];
83
    }
84
85
    /**
86
     * Match group by name.
87
     *
88
     * @param string $groupName
89
     *
90
     * @return string
91
     *
92
     * @throws RegexFailed
93
     */
94 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...
95
    {
96
        if (! isset($this->matches[$groupName])) {
97
            throw RegexFailed::namedGroupDoesntExist($this->pattern, $this->subject, $groupName);
98
        }
99
100
        return $this->matches[$groupName];
101
    }
102
}
103