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