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.

ReplaceResult   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 86
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A doReplacement() 0 8 1
A doReplacementWithCallable() 0 12 1
A result() 0 4 1
A count() 0 4 1
A for() 0 16 5
1
<?php
2
3
namespace Spatie\Regex;
4
5
use Exception;
6
7
class ReplaceResult extends RegexResult
8
{
9
    /** @var string|array */
10
    protected $pattern;
11
12
    /** @var string|array */
13
    protected $replacement;
14
15
    /** @var string|array */
16
    protected $subject;
17
18
    /** @var string|array */
19
    protected $result;
20
21
    /** @var int */
22
    protected $count;
23
24
    public function __construct($pattern, $replacement, $subject, $result, int $count)
25
    {
26
        $this->pattern = $pattern;
27
        $this->replacement = $replacement;
28
        $this->subject = $subject;
29
        $this->result = $result;
30
        $this->count = $count;
31
    }
32
33
    /**
34
     * @param string|array $pattern
35
     * @param string|array|callable $replacement
36
     * @param string|array $subject
37
     * @param int $limit
38
     *
39
     * @return \Spatie\Regex\ReplaceResult
40
     */
41
    public static function for($pattern, $replacement, $subject, $limit)
42
    {
43
        try {
44
            [$result, $count] = ! is_string($replacement) && is_callable($replacement) ?
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $count does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
45
                static::doReplacementWithCallable($pattern, $replacement, $subject, $limit) :
46
                static::doReplacement($pattern, $replacement, $subject, $limit);
47
        } catch (Exception $exception) {
48
            throw RegexFailed::replace($pattern, $subject, $exception->getMessage());
0 ignored issues
show
Bug introduced by
It seems like $pattern defined by parameter $pattern on line 41 can also be of type array; however, Spatie\Regex\RegexFailed::replace() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $subject defined by parameter $subject on line 41 can also be of type array; however, Spatie\Regex\RegexFailed::replace() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
        }
50
51
        if ($result === null) {
52
            throw RegexFailed::replace($pattern, $subject, static::lastPregError());
0 ignored issues
show
Bug introduced by
It seems like $pattern defined by parameter $pattern on line 41 can also be of type array; however, Spatie\Regex\RegexFailed::replace() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $subject defined by parameter $subject on line 41 can also be of type array; however, Spatie\Regex\RegexFailed::replace() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
53
        }
54
55
        return new static($pattern, $replacement, $subject, $result, $count);
56
    }
57
58
    protected static function doReplacement($pattern, $replacement, $subject, $limit): array
59
    {
60
        $count = 0;
61
62
        $result = preg_replace($pattern, $replacement, $subject, $limit, $count);
63
64
        return [$result, $count];
65
    }
66
67
    protected static function doReplacementWithCallable($pattern, callable $replacement, $subject, $limit): array
68
    {
69
        $replacement = function (array $matches) use ($pattern, $subject, $replacement) {
70
            return $replacement(new MatchResult($pattern, $subject, true, $matches));
71
        };
72
73
        $count = 0;
74
75
        $result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count);
76
77
        return [$result, $count];
78
    }
79
80
    /**
81
     * @return string|array
82
     */
83
    public function result()
84
    {
85
        return $this->result;
86
    }
87
88
    public function count(): int
89
    {
90
        return $this->count;
91
    }
92
}
93