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.

MatchAllResult::for()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Spatie\Regex;
4
5
use Exception;
6
use Spatie\Regex\Helpers\Arr;
7
8
class MatchAllResult extends RegexResult
9
{
10
    /** @var string */
11
    protected $pattern;
12
13
    /** @var string */
14
    protected $subject;
15
16
    /** @var bool */
17
    protected $hasMatch;
18
19
    /** @var array */
20
    protected $matches;
21
22 View Code Duplication
    public function __construct(string $pattern, string $subject, bool $result, 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...
23
    {
24
        $this->pattern = $pattern;
25
        $this->subject = $subject;
26
        $this->hasMatch = $result;
27
        $this->matches = $matches;
28
    }
29
30
    /**
31
     * @param string $pattern
32
     * @param string $subject
33
     *
34
     * @return static
35
     *
36
     * @throws \Spatie\Regex\RegexFailed
37
     */
38 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...
39
    {
40
        $matches = [];
41
42
        try {
43
            $result = preg_match_all($pattern, $subject, $matches);
44
        } catch (Exception $exception) {
45
            throw RegexFailed::match($pattern, $subject, $exception->getMessage());
46
        }
47
48
        if ($result === false) {
49
            throw RegexFailed::match($pattern, $subject, static::lastPregError());
50
        }
51
52
        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...
Bug introduced by
It seems like $matches can also be of type null; however, Spatie\Regex\MatchAllResult::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
53
    }
54
55
    public function hasMatch(): bool
56
    {
57
        return $this->hasMatch;
58
    }
59
60
    /**
61
     * @return \Spatie\Regex\MatchResult[]
62
     */
63
    public function results(): array
64
    {
65
        return Arr::map(Arr::transpose($this->matches), function ($match): MatchResult {
66
            return new MatchResult($this->pattern, $this->subject, true, $match);
67
        });
68
    }
69
}
70