| Conditions | 9 |
| Paths | 11 |
| Total Lines | 51 |
| Code Lines | 21 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 84 | private function makeRegex() |
||
| 85 | { |
||
| 86 | if (empty($this->rule)) { |
||
| 87 | throw new InvalidRuleException("Empty rule"); |
||
| 88 | } |
||
| 89 | |||
| 90 | $regex = $this->rule; |
||
| 91 | |||
| 92 | // Check if the rule isn't already regexp |
||
| 93 | if ($this->startsWith($regex, '/') && $this->endsWith($regex, '/')) { |
||
| 94 | $this->regex = mb_substr($this->rule, 1, mb_strlen($this->rule) - 2); |
||
| 95 | |||
| 96 | if (empty($this->regex)) { |
||
| 97 | throw new InvalidRuleException("Empty rule"); |
||
| 98 | } |
||
| 99 | |||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // escape special regex characters |
||
| 104 | $regex = preg_replace("/([\\\.\$\+\?\{\}\(\)\[\]\/])/", "\\\\$1", $this->rule); |
||
| 105 | |||
| 106 | // Separator character ^ matches anything but a letter, a digit, or |
||
| 107 | // one of the following: _ - . %. The end of the address is also |
||
| 108 | // accepted as separator. |
||
| 109 | $regex = str_replace("^", "([^\w\d_\-.%]|$)", $regex); |
||
| 110 | |||
| 111 | // * symbol |
||
| 112 | $regex = str_replace("*", ".*", $regex); |
||
| 113 | |||
| 114 | // | in the end means the end of the address |
||
| 115 | if ($this->endsWith($regex, '|')) { |
||
| 116 | $regex = mb_substr($regex, 0, mb_strlen($regex) - 1) . '$'; |
||
| 117 | } |
||
| 118 | |||
| 119 | // || in the beginning means beginning of the domain name |
||
| 120 | if ($this->startsWith($regex, '||')) { |
||
| 121 | if (mb_strlen($regex) > 2) { |
||
| 122 | // http://tools.ietf.org/html/rfc3986#appendix-B |
||
| 123 | $regex = "^([^:\/?#]+:)?(\/\/([^\/?#]*\.)?)?" . mb_substr($regex, 2); |
||
| 124 | } |
||
| 125 | // | in the beginning means start of the address |
||
| 126 | } elseif ($this->startsWith($regex, '|')) { |
||
| 127 | $regex = '^' . mb_substr($regex, 1); |
||
| 128 | } |
||
| 129 | |||
| 130 | // other | symbols should be escaped |
||
| 131 | $regex = preg_replace("/\|(?![\$])/", "\|$1", $regex); |
||
| 132 | |||
| 133 | $this->regex = $regex; |
||
| 134 | } |
||
| 135 | |||
| 157 |