• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / texy / 20980727294

14 Jan 2026 03:00AM UTC coverage: 92.376%. Remained the same
20980727294

push

github

dg
added CLAUDE.md

2387 of 2584 relevant lines covered (92.38%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.97
/src/Texy/Regexp.php
1
<?php
2

3
/**
4
 * This file is part of the Texy! (https://texy.nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Texy;
11

12
use function array_keys, array_values, is_array, is_object, preg_last_error, preg_last_error_msg, preg_match, preg_match_all, preg_replace, preg_replace_callback, preg_split, strlen;
13
use const PREG_OFFSET_CAPTURE, PREG_SET_ORDER, PREG_SPLIT_DELIM_CAPTURE, PREG_SPLIT_OFFSET_CAPTURE;
14

15

16
class Regexp
17
{
18
        public const ALL = 1;
19
        public const OFFSET_CAPTURE = 2;
20

21

22
        /**
23
         * Splits string by a regular expression.
24
         * @param  int  $flags  OFFSET_CAPTURE
25
         * @return list<string|array{string, int}>
26
         */
27
        public static function split(string $subject, string $pattern, int $flags = 0): array
28
        {
29
                $reFlags = (($flags & self::OFFSET_CAPTURE) ? PREG_SPLIT_OFFSET_CAPTURE : 0) | PREG_SPLIT_DELIM_CAPTURE;
×
30
                $res = preg_split($pattern, $subject, -1, $reFlags);
×
31
                if (preg_last_error()) { // run-time error
×
32
                        trigger_error(preg_last_error_msg(), E_USER_WARNING);
×
33
                }
34

35
                return $res;
×
36
        }
37

38

39
        /**
40
         * Performs a regular expression match.
41
         * @param  int  $flags  OFFSET_CAPTURE, ALL
42
         */
43
        public static function match(string $subject, string $pattern, int $flags = 0, int $offset = 0): mixed
1✔
44
        {
45
                $empty = $flags & self::ALL ? [] : null;
1✔
46
                if ($offset > strlen($subject)) {
1✔
47
                        return $empty;
×
48
                }
49

50
                $reFlags = ($flags & self::OFFSET_CAPTURE) ? PREG_OFFSET_CAPTURE : 0;
1✔
51
                $res = $flags & self::ALL
1✔
52
                        ? preg_match_all($pattern, $subject, $m, $reFlags | PREG_SET_ORDER, $offset)
1✔
53
                        : preg_match($pattern, $subject, $m, $reFlags, $offset);
1✔
54
                if (preg_last_error()) { // run-time error
1✔
55
                        trigger_error(preg_last_error_msg(), E_USER_WARNING);
×
56
                } elseif ($res) {
1✔
57
                        return $m;
1✔
58
                }
59

60
                return $empty;
1✔
61
        }
62

63

64
        /**
65
         * Perform a regular expression search and replace.
66
         * @param  string|callable(array<int|string, string>): ?string  $replacement
67
         */
68
        public static function replace(
1✔
69
                string $subject,
70
                string|array $pattern,
71
                string|callable|null $replacement = null,
72
        ): string
73
        {
74
                if (is_object($replacement) || is_array($replacement)) {
1✔
75
                        $res = preg_replace_callback($pattern, $replacement, $subject);
1✔
76
                        if ($res === null && preg_last_error()) { // run-time error
1✔
77
                                trigger_error(preg_last_error_msg(), E_USER_WARNING);
×
78
                        }
79

80
                        return $res;
1✔
81

82
                } elseif ($replacement === null && is_array($pattern)) {
1✔
83
                        $replacement = array_values($pattern);
1✔
84
                        $pattern = array_keys($pattern);
1✔
85
                }
86

87
                $res = preg_replace($pattern, $replacement, $subject);
1✔
88
                if (preg_last_error()) { // run-time error
1✔
89
                        trigger_error(preg_last_error_msg(), E_USER_WARNING);
×
90
                }
91

92
                return $res;
1✔
93
        }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc