Passed
Branch master (f496ba)
by stéphane
02:11
created

Regex   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B isDate() 0 16 8
A isNumber() 0 4 1
A isProperlyQuoted() 0 3 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Dallgoot\Yaml;
4
5
/**
6
 * Define Regex patterns as constants
7
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
8
class Regex
9
{
10
11
    const NULL  = "null";
12
    const FALSE = "false";
13
    const TRUE  = "true";
14
    const AN = "[\w ]+";
15
    const NUM = "-?[\d.e]+";
16
    const SIMPLE = "(?P<sv>".self::NULL."|".
17
                                  self::FALSE."|".
18
                                  self::TRUE."|".
19
                                  self::AN."|".
20
                                  self::NUM.")";
21
    private const seqForMap = "(?P<seq>\[(?:(?:(?P>sv)|(?P>seq)|(?P>map)),?\s*)+\])";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected SEQFORMAP).
Loading history...
22
    private const mapForSeq = "(?P<map>{\s*(?:".self::AN."\s*:\s*(?:(?P>sv)|(?P>seq)|(?P>map)),?\s*)+})";
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected MAPFORSEQ).
Loading history...
23
24
    const MAPPING  = "/(?P<map>{\s*(?:".self::AN."\s*:\s*(?:".self::SIMPLE."|".self::seqForMap."|(?P>map)),?\s*)+})/i";
25
    const SEQUENCE = "/(?P<seq>\[(?:(?:".self::SIMPLE."|".self::mapForSeq."|(?P>seq)),?\s*)+\])/i";
26
27
    const KEY  = '/^([[:alnum:]_][[:alnum:]_ -]*[ \t]*)(?::[ \t](.*)|:)$/';
28
    const ITEM = '/^-([ \t]+(.*))?$/';
29
30
31
    public static function isDate($v):bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
32
    {
33
        $d         = "\\d{4}([-\\/])\\d{2}\\1\\d{2}";
34
        $h         = "\\d{2}(:)\\d{2}\\2\\d{2}";
35
        $date      =  "/^$d$/";// 2002-12-14, 2002/12/14
36
        $canonical =  "/^$d(?:t| )$h\\.\\dz?$/im";// 2001-12-15T02:59:43.1Z
37
        $spaced    =  "/^$d(?:t| )$h\\.\\d{2} [-+]\\d$/im";// 2001-12-14 21:59:43.10 -5
38
        $iso8601   =  "/^$d(?:t| )$h\\.\\d{2}[-+]\\d{2}\\2\\d{2}/im";// 2001-12-14t21:59:43.10-05:00
39
        $matchDate      = preg_match($date, $v);
40
        $matchCanonical = preg_match($canonical, $v);
41
        $matchSpaced    = preg_match($spaced, $v);
42
        $matchIso       = preg_match($iso8601, $v);
43
        if (is_bool($matchDate) || is_bool($matchCanonical) || is_bool($matchSpaced) || is_bool($matchIso))
1 ignored issue
show
introduced by
The condition is_bool($matchIso) is always false.
Loading history...
introduced by
The condition is_bool($matchSpaced) is always false.
Loading history...
introduced by
The condition is_bool($matchCanonical) is always false.
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
44
          throw new \Exception("Regex date error");
45
46
        return $matchDate || $matchCanonical || $matchSpaced || $matchIso;
47
    }
48
49
    public static function isNumber(string $var):bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
50
    {
51
        //TODO: https://secure.php.net/manual/en/function.is-numeric.php
52
        return (bool) preg_match("/^((0o\d+)|(0x[\da-f]+)|([\d.]+e[-+]\d{1,2})|([-+]?(\d*\.?\d+)))$/i", $var);
53
    }
54
55
    public static function isProperlyQuoted(String $var):bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
56
    {
57
        return (bool) preg_match("/(['".'"]).*?(?<![\\\\])\1$/ms', $var);
58
    }
59
}
60