Completed
Push — master ( 56096b...601ee3 )
by Sergey
12s
created

RegEx   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 4 1
A prepare() 0 10 2
1
<?php
2
3
namespace Larapulse\Support\Handlers;
4
5
class RegEx
6
{
7
    const REGEX_CHARS_REPLACEMENT = [
8
        " "     => '\s',
9
        "\t"    => '\t',
10
        "\n"    => '\n',
11
        "\f"    => '\f',
12
        "\r"    => '\r',
13
        "\v"    => '\v',
14
        // "\h"    => '\h',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
15
        // "\R"    => '\R',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
16
    ];
17
18
    /**
19
     * Validate if RegEx statement is valid
20
     *
21
     * @param string $regexStatement
22
     *
23
     * @return bool
24
     */
25
    public static function isValid(string $regexStatement) : bool
26
    {
27
        return @preg_match($regexStatement, null) !== false;
28
    }
29
30
    /**
31
     * Prepare string to be safety used in regex
32
     *
33
     * @param string $string
34
     * @param string $quotes
35
     *
36
     * @return string
37
     */
38
    public static function prepare(string $string, $quotes = '/') : string
39
    {
40
        $string = $quotes ? preg_quote($string, $quotes) : $string;
41
42
        return str_replace(
43
            array_keys(self::REGEX_CHARS_REPLACEMENT),
44
            array_values(self::REGEX_CHARS_REPLACEMENT),
45
            $string
46
        );
47
    }
48
}
49