Completed
Pull Request — 2.x (#3632)
by Scott Kingsley
05:07
created

PodsMVFieldData   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A emit_script() 0 3 1
A json_encode_hex_tag() 0 14 2
1
<?php
2
3
/**
4
 * @package Pods
5
 */
6
class PodsMVFieldData {
7
8
	private $export_data = array();
9
10
	/**
11
	 *
12
	 * @param string $field_type
13
	 * @param array  $data
14
	 */
15
	public function __construct( $field_type, $data ) {
16
17
		$data[ 'fieldType' ] = $field_type;
18
		$this->export_data    = $data;
19
	}
20
21
	/**
22
	 * Sends direct output
23
	 */
24
	public function emit_script() { ?>
25
		<script type="application/json" class="pods-mv-field-data"><?php echo self::json_encode_hex_tag( $this->export_data ); ?></script>
26
	<?php }
27
28
	/**
29
	 * Provides PHP 5.2 support for the JSON_HEX_TAG param with json_encode
30
	 *
31
	 * @param string $data
32
	 *
33
	 * @return string
34
	 */
35
	private static function json_encode_hex_tag( $data ) {
36
37
		$search  = array( '<', '>' );
38
		$replace = array( '\u003C', '\u003E' );
39
40
		if ( defined( 'JSON_HEX_TAG' ) ) {
41
			$string = json_encode( $data, JSON_HEX_TAG );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_HEX_TAG.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
42
		} else {
43
			$string = json_encode( $data );
44
			$string = str_replace( $search, $replace, $string );
45
		}
46
47
		return $string;
48
	}
49
}
50