Test Failed
Pull Request — master (#26)
by Christopher
02:51
created

PatternTrait::ProcessRegex()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 15
nc 3
nop 2
1
<?php
2
namespace AlgoWeb\xsdTypes\Facets;
3
4
trait PatternTrait
5
{
6
    use XMLPatterns;
7
    /**
8
     * @Exclude
9
     * @var array defines the exact sequence of characters that are acceptable
10
     */
11
    private $pattern = array();
12
13
    /**
14
     * @param string $regexPatternToAdd
15
     * @param mixed  $ProcessMultiCharacterEscape
16
     */
17
    protected function setPatternFacet($regexPatternToAdd, $ProcessMultiCharacterEscape = true)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $ProcessMultiCharacterEscape is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
18
    {
19
        if (null == self::$Letter) {
20
            self::init();
21
        }
22
        $regexPatternToAdd = $this->ProcessRegex($regexPatternToAdd, $ProcessMultiCharacterEscape);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPatternToAdd. This often makes code more readable.
Loading history...
23
        if (!$this->checkRegexValidPattern($regexPatternToAdd)) {
24
            $regexPatternToAdd = '/' . $regexPatternToAdd . '/';
25
            if (!$this->checkRegexValidPattern($regexPatternToAdd)) {
26
                throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this));
27
            }
28
        }
29
        $this->pattern[] = $regexPatternToAdd;
30
    }
31
32
    private function ProcessRegex($regexPattern, $ProcessMultiCharacterEscape)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style Naming introduced by
The parameter $ProcessMultiCharacterEscape is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
33
    {
34
        if (!$ProcessMultiCharacterEscape) {
35
            return $regexPattern;
36
        }
37
        if (null == self::$NameChar) {
38
            init();
39
        }
40
        
41
        $regexPattern = str_replace('\S', '[^\s]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
42
        /**
43
         * \x{20} matches the character   with index 2016 (3210 or 408) literally (case sensitive)
44
         * \t matches a tab character (ASCII 9)
45
         * \n matches a line-feed (newline) character (ASCII 10)
46
         * \r matches a carriage return (ASCII 13).
47
         */
48
        $regexPattern = str_replace('\s', '[\x{20}\t\n\r]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
49
        $regexPattern = str_replace('\I', '[^\i]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
50
        $regexPattern = str_replace('\i', self::$Letter.'|_|:', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
51
        $regexPattern = str_replace('\c', self::$NameChar, $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
52
        $regexPattern = str_replace('\D', '[^\d]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
53
        $regexPattern = str_replace('\d', '\p{Nd}', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
54
        $regexPattern = str_replace('\W', '[^\w]', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
55
        $regexPattern = str_replace('\w', '[\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}] ', $regexPattern);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $regexPattern. This often makes code more readable.
Loading history...
56
        return $regexPattern;
57
    }
58
59
    /**
60
     * @param string $pattern
61
     */
62
    private function checkRegexValidPattern($pattern)
63
    {
64
        return !(false === @preg_match($pattern, null));
65
    }
66
67
    /**
68
     * @param string $v
69
     */
70
    private function checkPattern($v)
71
    {
72
        if (!empty($this->pattern)) {
73
            foreach ($this->pattern as $pattern) {
74
                if (!$this->matchesRegexPattern($pattern, $v)) {
75
                    throw new \InvalidArgumentException('Assigned value for ' . get_class($this) .
76
                        ' does not match pattern ' . $pattern);
77
                }
78
            }
79
        }
80
    }
81
82
    /**
83
     * Checks a pattern against a string.
84
     *
85
     * @param  string $pattern the regex pattern
86
     * @param  string $string  the string to check
87
     * @return bool   true if string matches pattern
88
     */
89
    private function matchesRegexPattern($pattern, $string)
90
    {
91
        $matches = null;
92
        return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]);
93
    }
94
}
95