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

dg / texy / 22262497275

21 Feb 2026 07:01PM UTC coverage: 93.057% (+0.7%) from 92.367%
22262497275

push

github

dg
added CLAUDE.md

2426 of 2607 relevant lines covered (93.06%)

0.93 hits per line

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

68.75
/src/Texy/Regexp.php
1
<?php declare(strict_types=1);
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
namespace Texy;
9

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

13

14
/**
15
 * Regular expression utilities with error handling.
16
 */
17
class Regexp
18
{
19
        public const ALL = 1;
20
        public const OFFSET_CAPTURE = 2;
21

22

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

37
                return $res;
×
38
        }
39

40

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

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

62
                return $empty;
1✔
63
        }
64

65

66
        /**
67
         * Perform a regular expression search and replace.
68
         * @param  string|string[]  $pattern
69
         * @param  string|\Closure(string[]): string|null  $replacement
70
         */
71
        public static function replace(
1✔
72
                string $subject,
73
                string|array $pattern,
74
                string|\Closure|null $replacement = null,
75
        ): string
76
        {
77
                if ($replacement instanceof \Closure) {
1✔
78
                        $res = preg_replace_callback($pattern, $replacement, $subject);
1✔
79
                        if ($res === null && preg_last_error()) { // run-time error
1✔
80
                                trigger_error(preg_last_error_msg(), E_USER_WARNING);
×
81
                        }
82

83
                        return $res ?? '';
1✔
84

85
                } elseif ($replacement === null && is_array($pattern)) {
1✔
86
                        $replacement = array_values($pattern);
1✔
87
                        $pattern = array_keys($pattern);
1✔
88
                }
89

90
                $res = preg_replace($pattern, $replacement ?? '', $subject);
1✔
91
                if (preg_last_error()) { // run-time error
1✔
92
                        trigger_error(preg_last_error_msg(), E_USER_WARNING);
×
93
                }
94

95
                return $res;
1✔
96
        }
97
}
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