1 | <?php |
||
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) |
||
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) ? |
||
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()); |
||
49 | } |
||
50 | |||
51 | if ($result === null) { |
||
52 | throw RegexFailed::replace($pattern, $subject, static::lastPregError()); |
||
53 | } |
||
54 | |||
55 | return new static($pattern, $replacement, $subject, $result, $count); |
||
56 | } |
||
57 | |||
58 | protected static function doReplacement($pattern, $replacement, $subject, $limit): array |
||
66 | |||
67 | protected static function doReplacementWithCallable($pattern, callable $replacement, $subject, $limit): array |
||
79 | |||
80 | /** |
||
81 | * @return string|array |
||
82 | */ |
||
83 | public function result() |
||
87 | |||
88 | public function count(): int |
||
92 | } |
||
93 |
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.