Test Failed
Pull Request — master (#28)
by Christopher
03:22 queued 01:09
created

PatternTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 167
Duplicated Lines 16.17 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 2 Features 1
Metric Value
wmc 19
c 10
b 2
f 1
lcom 1
cbo 1
dl 27
loc 167
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setPatternFacet() 0 15 4
A processRegex() 0 15 3
A processRegexSlashS() 8 8 1
A processRegexSlashI() 0 11 1
A processRegexSlashC() 0 11 1
A processRegexSlashD() 8 8 1
A processRegexSlashW() 10 10 1
A checkRegexValidPattern() 0 4 1
A checkPattern() 0 11 4
A matchesRegexPattern() 0 5 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 bool   $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
    /**
56
     * @param string $patternToProcess
57
     *
58
     * @return string
59
     */
60 View Code Duplication
    private function processRegexSlashS($patternToProcess)
1 ignored issue
show
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...
61
    {
62
        // Any character except those matched by '\s'.
63
        $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...
64
        // 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...
65
        $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...
66
        return $patternToProcess;
67
    }
68
69
    /**
70
     * @param string $patternToProcess
71
     *
72
     * @return string
73
     */
74
    private function processRegexSlashI($patternToProcess)
75
    {
76
        // Any character except those matched by '\i'.
77
        $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...
78
        // The first character in an XML identifier. Specifically, any letter, the character '_', or the character ':',
79
        // See the XML Recommendation for the complex specification of a letter. This character represents a subset of
80
        // letter that might appear in '\c'.
81
        $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...
82
        $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...
83
        return $patternToProcess;
84
    }
85
86
    /**
87
     * @param string $patternToProcess
88
     *
89
     * @return string
90
     */
91
    private function processRegexSlashC($patternToProcess)
92
    {
93
        // Any character except those matched by '\c'.
94
        $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...
95
        // Any character that might appear in the built-in NMTOKEN datatype.
96
        // See the XML Recommendation for the complex specification of a NameChar.
97
        $patternToProcess = str_replace('\c-[:]', self::$Letter . '|' . self::$Digit . '|.|-|_|' . self::$CombiningChar . '|'
2 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $patternToProcess. This often makes code more readable.
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
98
            . self::$Extender, $patternToProcess);
99
        $patternToProcess = str_replace('\c', '(' . self::$NameChar . ')', $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...
100
        return $patternToProcess;
101
    }
102
103
    /**
104
     * @param string $patternToProcess
105
     *
106
     * @return string
107
     */
108 View Code Duplication
    private function processRegexSlashD($patternToProcess)
1 ignored issue
show
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...
109
    {
110
        // Any character except those matched by '\d'.
111
        $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...
112
        // Any Decimal digit. A shortcut for '\p{Nd}'.
113
        $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...
114
        return $patternToProcess;
115
    }
116
117
    /**
118
     * @param string $patternToProcess
119
     *
120
     * @return string
121
     */
122 View Code Duplication
    private function processRegexSlashW($patternToProcess)
1 ignored issue
show
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...
123
    {
124
        // Any character except those matched by '\w'.
125
        $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...
126
        // Any character that might appear in a word. A shortcut for '[#X0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]'
127
        // (all characters except the set of "punctuation", "separator", and "other" characters).
128
        $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...
129
            $patternToProcess);
130
        return $patternToProcess;
131
    }
132
133
    /**
134
     * @param string $pattern
135
     *
136
     * @return bool
137
     */
138
    private function checkRegexValidPattern($pattern)
139
    {
140
        return !(false === @preg_match($pattern, null));
141
    }
142
143
    /**
144
     * @param string $v
145
     */
146
    private function checkPattern($v)
147
    {
148
        if (!empty($this->pattern)) {
149
            foreach ($this->pattern as $pattern) {
150
                if (!$this->matchesRegexPattern($pattern, $v)) {
151
                    throw new \InvalidArgumentException('Assigned value for ' . get_class($this) .
152
                        ' does not match pattern ' . $pattern);
153
                }
154
            }
155
        }
156
    }
157
158
    /**
159
     * Checks a pattern against a string.
160
     *
161
     * @param  string $pattern the regex pattern
162
     * @param  string $string  the string to check
163
     * @return bool   true if string matches pattern
164
     */
165
    private function matchesRegexPattern($pattern, $string)
166
    {
167
        $matches = null;
168
        return (1 == preg_match($pattern, $string, $matches) && $string == $matches[0]);
169
    }
170
}
171