WP_SimplePie_Sanitize_KSES   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 27.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 11
loc 40
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B sanitize() 11 23 6

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
/**
3
 * Feed API: WP_SimplePie_Sanitize_KSES class
4
 *
5
 * @package WordPress
6
 * @subpackage Feed
7
 * @since 4.7.0
8
 */
9
10
/**
11
 * Core class used to implement SimpliePie feed sanitization.
12
 *
13
 * Extends the SimplePie_Sanitize class to use KSES, because
14
 * we cannot universally count on DOMDocument being available.
15
 *
16
 * @since 3.5.0
17
 *
18
 * @see SimplePie_Sanitize
19
 */
20
class WP_SimplePie_Sanitize_KSES extends SimplePie_Sanitize {
21
22
	/**
23
	 * WordPress SimplePie sanitization using KSES.
24
	 *
25
	 * Sanitizes the incoming data, to ensure that it matches the type of data expected, using KSES.
26
	 *
27
	 * @since 3.5.0
28
	 * @access public
29
	 *
30
	 * @param mixed   $data The data that needs to be sanitized.
31
	 * @param integer $type The type of data that it's supposed to be.
32
	 * @param string  $base Optional. The `xml:base` value to use when converting relative
33
	 *                      URLs to absolute ones. Default empty.
34
	 * @return mixed Sanitized data.
35
	 */
36
	public function sanitize( $data, $type, $base = '' ) {
37
		$data = trim( $data );
38 View Code Duplication
		if ( $type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML ) {
39
			if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>)/', $data)) {
40
				$type |= SIMPLEPIE_CONSTRUCT_HTML;
41
			}
42
			else {
43
				$type |= SIMPLEPIE_CONSTRUCT_TEXT;
44
			}
45
		}
46
		if ( $type & SIMPLEPIE_CONSTRUCT_BASE64 ) {
47
			$data = base64_decode( $data );
48
		}
49
		if ( $type & ( SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML ) ) {
50
			$data = wp_kses_post( $data );
51 View Code Duplication
			if ( $this->output_encoding !== 'UTF-8' ) {
52
				$data = $this->registry->call( 'Misc', 'change_encoding', array( $data, 'UTF-8', $this->output_encoding ) );
0 ignored issues
show
Bug introduced by
The property registry does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
53
			}
54
			return $data;
55
		} else {
56
			return parent::sanitize( $data, $type, $base );
57
		}
58
	}
59
}
60