Regex   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matchesPattern() 0 5 1
A getMatchesFromPattern() 0 9 2
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Johnny Mast <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axiom\Rivescript\Traits;
12
13
/**
14
 * Str trait
15
 *
16
 * A collection of regex helpers.
17
 *
18
 * PHP version 7.4 and higher.
19
 *
20
 * @category Core
21
 * @package  Traits
22
 * @author   Johnny Mast <[email protected]>
23
 * @license  https://opensource.org/licenses/MIT MIT
24
 * @link     https://github.com/axiom-labs/rivescript-php
25
 * @since    0.4.0
26
 */
27
trait Regex
28
{
29
30
    /**
31
     * Does the source have any matches?
32
     *
33
     * @param string $pattern The pattern to match.
34
     * @param string $source  The source to match in.
35
     *
36
     * @return bool
37
     */
38
    protected function matchesPattern(string $pattern, string $source): bool
39
    {
40
        preg_match_all($pattern, $source, $matches);
41
42
        return isset($matches[0][0]);
43
    }
44
45
    /**
46
     * Get the regular expression matches from the source.
47
     *
48
     * @param string $pattern The pattern to match.
49
     * @param string $source  The source to match in.
50
     *
51
     * @return array[]|bool
52
     */
53
    protected function getMatchesFromPattern(string $pattern, string $source)
54
    {
55
        if ($this->matchesPattern($pattern, $source)) {
56
            preg_match_all($pattern, $source, $matches, PREG_SET_ORDER);
57
58
            return $matches;
59
        }
60
61
        return false;
62
    }
63
}
64