Completed
Pull Request — master (#28)
by Christopher
04:00 queued 53s
created

PatternTrait::processRegexSlashD()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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
    public $pattern = array();
12
13
    /**
14
     * @param string $newPatternToAdd
15
     * @param mixed  $processMultiCharacterEscape
16
     */
17
    protected function setPatternFacet($newPatternToAdd, $processMultiCharacterEscape = true)
18
    {
19
        if (null == self::$Letter) {
20
            self::init();
21
        }
22
        $newPatternToAdd = $this->processRegex($newPatternToAdd, $processMultiCharacterEscape);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $newPatternToAdd. This often makes code more readable.
Loading history...
23
        if (!$this->checkRegexValidPattern($newPatternToAdd)) {
24
            $newPatternToAdd = '/' . $newPatternToAdd . '/u';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $newPatternToAdd. This often makes code more readable.
Loading history...
25
            if (!$this->checkRegexValidPattern($newPatternToAdd)) {
26
                throw new \InvalidArgumentException('Invalid regex pattern provided: ' . get_class($this) .
27
                    'pattern is: ' . $newPatternToAdd);
28
            }
29
        }
30
        $this->pattern[] = $newPatternToAdd;
31
    }
32
33
    /**
34
     * @param string $patternToProcess
35
     * @param mixed  $processMultiCharacterEscape
36
     *
37
     * @return string
38
     */
39
    private function processRegex($patternToProcess, $processMultiCharacterEscape)
40
    {
41
        if (!$processMultiCharacterEscape) {
42
            return $patternToProcess;
43
        }
44
        if (null == self::$NameChar) {
45
            init();
46
        }
47
        $patternToProcess = $this->processRegexSlashS($patternToProcess);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
48
        $patternToProcess = $this->processRegexSlashI($patternToProcess);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
49
        $patternToProcess = $this->processRegexSlashC($patternToProcess);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
50
        $patternToProcess = $this->processRegexSlashD($patternToProcess);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
51
        $patternToProcess = $this->processRegexSlashW($patternToProcess);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
52
        return $patternToProcess;
53
    }
54
55 View Code Duplication
    private function processRegexSlashS($patternToProcess)
1 ignored issue
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        // Any character except those matched by '\s'.
58
        $patternToProcess = str_replace('\S', '[^\s]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
59
        // Whitespace, specifically '&#20;' (space), '\t' (tab), '\n' (newline) and '\r' (return).
1 ignored issue
show
Unused Code Comprehensibility introduced by
50% 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...
60
        $patternToProcess = str_replace('\s', '([\x{20}\t\n\r])', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
61
        return $patternToProcess;
62
    }
63
64 View Code Duplication
    private function processRegexSlashI($patternToProcess)
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66
        // Any character except those matched by '\i'.
67
        $patternToProcess = str_replace('\I', '[^\i]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
68
        // The first character in an XML identifier. Specifically, any letter, the character '_', or the character ':',
69
        // See the XML Recommendation for the complex specification of a letter. This character represents a subset of
70
        // letter that might appear in '\c'.
71
        $patternToProcess = str_replace('\i', '(' . self::$Letter . '|_|:)', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
72
        return $patternToProcess;
73
    }
74
75 View Code Duplication
    private function processRegexSlashC($patternToProcess)
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        // Any character except those matched by '\c'.
78
        $patternToProcess = str_replace('\C', '[^\c]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
79
        // Any character that might appear in the built-in NMTOKEN datatype.
80
        // See the XML Recommendation for the complex specification of a NameChar.
81
        $patternToProcess = str_replace('\c', '(' . self::$NameChar . ')', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
82
        return $patternToProcess;
83
84
    }
85
86 View Code Duplication
    private function processRegexSlashD($patternToProcess)
1 ignored issue
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        // Any character except those matched by '\d'.
89
        $patternToProcess = str_replace('\D', '[^(\d)]', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
90
        // Any Decimal digit. A shortcut for '\p{Nd}'.
91
        $patternToProcess = str_replace('\d', '\p{Nd)', $patternToProcess);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
92
        return $patternToProcess;
93
    }
94
95 View Code Duplication
    private function processRegexSlashW($patternToProcess)
1 ignored issue
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...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        // Any character except those matched by '\w'.
98
        $patternToProcess = str_replace('\W', '[^\w]', $patternToProcess);
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
99
        // Any character that might appear in a word. A shortcut for '[#X0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]'
100
        // (all characters except the set of "punctuation", "separator", and "other" characters).
101
        $patternToProcess = str_replace('\w', '([\x{0000}-\x{10FFFF}]-[\p{P}\p{Z}\p{C}])',
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
102
            $patternToProcess);
103
        return $patternToProcess;
104
105
    }
106
107
    /**
108
     * @param string $pattern
109
     */
110
    private function checkRegexValidPattern($pattern)
111
    {
112
        return !(false === @preg_match($pattern, null));
113
    }
114
115
    /**
116
     * @param string $v
117
     */
118
    private function checkPattern($v)
119
    {
120
        if (!empty($this->pattern)) {
121
            foreach ($this->pattern as $pattern) {
122
                if (!$this->matchesRegexPattern($pattern, $v)) {
123
                    throw new \InvalidArgumentException('Assigned value for ' . get_class($this) .
124
                        ' does not match pattern ' . $pattern);
125
                }
126
            }
127
        }
128
    }
129
130
    /**
131
     * Checks a pattern against a string.
132
     *
133
     * @param  string $pattern the regex pattern
134
     * @param  string $string  the string to check
135
     * @return bool   true if string matches pattern
136
     */
137
    private function matchesRegexPattern($pattern, $string)
138
    {
139
        $matches = null;
140
        return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]);
141
    }
142
}
143