| Conditions | 20 |
| Paths | 1512 |
| Total Lines | 105 |
| Code Lines | 46 |
| 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 |
||
| 108 | public function validate($config, $context) |
||
| 109 | { |
||
| 110 | // ABNF definitions from RFC 3986 |
||
| 111 | $chars_sub_delims = '!$&\'()*+,;='; |
||
| 112 | $chars_gen_delims = ':/?#[]@'; |
||
| 113 | $chars_pchar = $chars_sub_delims . ':@'; |
||
| 114 | |||
| 115 | // validate host |
||
| 116 | if (!is_null($this->host)) { |
||
| 117 | $host_def = new HTMLPurifier_AttrDef_URI_Host(); |
||
| 118 | $this->host = $host_def->validate($this->host, $config, $context); |
||
| 119 | if ($this->host === false) { |
||
| 120 | $this->host = null; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | // validate scheme |
||
| 125 | // NOTE: It's not appropriate to check whether or not this |
||
| 126 | // scheme is in our registry, since a URIFilter may convert a |
||
| 127 | // URI that we don't allow into one we do. So instead, we just |
||
| 128 | // check if the scheme can be dropped because there is no host |
||
| 129 | // and it is our default scheme. |
||
| 130 | if (!is_null($this->scheme) && is_null($this->host) || $this->host === '') { |
||
| 131 | // support for relative paths is pretty abysmal when the |
||
| 132 | // scheme is present, so axe it when possible |
||
| 133 | $def = $config->getDefinition('URI'); |
||
| 134 | if ($def->defaultScheme === $this->scheme) { |
||
| 135 | $this->scheme = null; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // validate username |
||
| 140 | if (!is_null($this->userinfo)) { |
||
| 141 | $encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . ':'); |
||
| 142 | $this->userinfo = $encoder->encode($this->userinfo); |
||
| 143 | } |
||
| 144 | |||
| 145 | // validate port |
||
| 146 | if (!is_null($this->port)) { |
||
| 147 | if ($this->port < 1 || $this->port > 65535) { |
||
| 148 | $this->port = null; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | // validate path |
||
| 153 | $segments_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/'); |
||
| 154 | if (!is_null($this->host)) { // this catches $this->host === '' |
||
| 155 | // path-abempty (hier and relative) |
||
| 156 | // http://www.example.com/my/path |
||
| 157 | // //www.example.com/my/path (looks odd, but works, and |
||
| 158 | // recognized by most browsers) |
||
| 159 | // (this set is valid or invalid on a scheme by scheme |
||
| 160 | // basis, so we'll deal with it later) |
||
| 161 | // file:///my/path |
||
| 162 | // ///my/path |
||
| 163 | $this->path = $segments_encoder->encode($this->path); |
||
| 164 | } elseif ($this->path !== '') { |
||
| 165 | if ($this->path[0] === '/') { |
||
| 166 | // path-absolute (hier and relative) |
||
| 167 | // http:/my/path |
||
| 168 | // /my/path |
||
| 169 | if (strlen($this->path) >= 2 && $this->path[1] === '/') { |
||
| 170 | // This could happen if both the host gets stripped |
||
| 171 | // out |
||
| 172 | // http://my/path |
||
| 173 | // //my/path |
||
| 174 | $this->path = ''; |
||
| 175 | } else { |
||
| 176 | $this->path = $segments_encoder->encode($this->path); |
||
| 177 | } |
||
| 178 | } elseif (!is_null($this->scheme)) { |
||
| 179 | // path-rootless (hier) |
||
| 180 | // http:my/path |
||
| 181 | // Short circuit evaluation means we don't need to check nz |
||
| 182 | $this->path = $segments_encoder->encode($this->path); |
||
| 183 | } else { |
||
| 184 | // path-noscheme (relative) |
||
| 185 | // my/path |
||
| 186 | // (once again, not checking nz) |
||
| 187 | $segment_nc_encoder = new HTMLPurifier_PercentEncoder($chars_sub_delims . '@'); |
||
| 188 | $c = strpos($this->path, '/'); |
||
| 189 | if ($c !== false) { |
||
| 190 | $this->path = |
||
| 191 | $segment_nc_encoder->encode(substr($this->path, 0, $c)) . |
||
| 192 | $segments_encoder->encode(substr($this->path, $c)); |
||
| 193 | } else { |
||
| 194 | $this->path = $segment_nc_encoder->encode($this->path); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | // path-empty (hier and relative) |
||
| 199 | $this->path = ''; // just to be safe |
||
| 200 | } |
||
| 201 | |||
| 202 | // qf = query and fragment |
||
| 203 | $qf_encoder = new HTMLPurifier_PercentEncoder($chars_pchar . '/?'); |
||
| 204 | |||
| 205 | if (!is_null($this->query)) { |
||
| 206 | $this->query = $qf_encoder->encode($this->query); |
||
| 207 | } |
||
| 208 | |||
| 209 | if (!is_null($this->fragment)) { |
||
| 210 | $this->fragment = $qf_encoder->encode($this->fragment); |
||
| 211 | } |
||
| 212 | return true; |
||
| 213 | } |
||
| 317 |