Completed
Push — add/amp-pwa-experiment ( efea12 )
by
unknown
11:53
created

AMP_Audio_Sanitizer   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 89
Duplicated Lines 16.85 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 15
loc 89
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_scripts() 0 7 2
C sanitize() 15 41 8
D filter_attributes() 0 32 10

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
require_once( AMP__ROOT__ . '/includes/sanitizers/class-amp-base-sanitizer.php' );
4
5
class AMP_Audio_Sanitizer extends AMP_Base_Sanitizer {
6
	public static $tag = 'audio';
7
8
	private static $script_slug = 'amp-audio';
9
	private static $script_src = 'https://cdn.ampproject.org/v0/amp-audio-0.1.js';
10
11
	public function get_scripts() {
12
		if ( ! $this->did_convert_elements ) {
13
			return array();
14
		}
15
16
		return array( self::$script_slug => self::$script_src );
17
	}
18
19
	public function sanitize() {
20
		$nodes = $this->dom->getElementsByTagName( self::$tag );
21
		$num_nodes = $nodes->length;
22
		if ( 0 === $num_nodes ) {
23
			return;
24
		}
25
26
		for ( $i = $num_nodes - 1; $i >= 0; $i-- ) {
27
			$node = $nodes->item( $i );
28
			$old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $node );
29
30
			$new_attributes = $this->filter_attributes( $old_attributes );
31
32
			$new_node = AMP_DOM_Utils::create_node( $this->dom, 'amp-audio', $new_attributes );
33
34
			// TODO: `source` does not have closing tag, and DOMDocument doesn't handle it well.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
35 View Code Duplication
			foreach ( $node->childNodes as $child_node ) {
36
				$new_child_node = $child_node->cloneNode( true );
37
				$old_child_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array( $new_child_node );
38
				$new_child_attributes = $this->filter_attributes( $old_child_attributes );
39
40
				// Only append source tags with a valid src attribute
41
				if ( ! empty( $new_child_attributes['src'] ) && 'source' === $new_child_node->tagName ) {
42
					$new_node->appendChild( $new_child_node );
43
				}
44
			}
45
46
			// If the node has at least one valid source, replace the old node with it.
47
			// Otherwise, just remove the node.
48
			//
49
			// TODO: Add a fallback handler.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
50
			// See: https://github.com/ampproject/amphtml/issues/2261
51 View Code Duplication
			if ( 0 === $new_node->childNodes->length && empty( $new_attributes['src'] ) ) {
52
				$node->parentNode->removeChild( $node );
53
			} else {
54
				$node->parentNode->replaceChild( $new_node, $node );
55
			}
56
57
			$this->did_convert_elements = true;
58
		}
59
	}
60
61
	private function filter_attributes( $attributes ) {
62
		$out = array();
63
64
		foreach ( $attributes as $name => $value ) {
65
			switch ( $name ) {
66
				case 'src':
67
					$out[ $name ] = $this->maybe_enforce_https_src( $value );
68
					break;
69
70
				case 'width':
71
				case 'height':
72
					$out[ $name ] = $this->sanitize_dimension( $value, $name );
73
					break;
74
75
				case 'class':
76
					$out[ $name ] = $value;
77
					break;
78
				case 'loop':
79
				case 'muted':
80
				case 'autoplay':
81
					if ( 'false' !== $value ) {
82
						$out[ $name ] = '';
83
					}
84
					break;
85
86
				default;
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
87
					break;
88
			}
89
		}
90
91
		return $out;
92
	}
93
}
94