Completed
Push — add/sync-rest-options ( 3a27a6...110de2 )
by
unknown
08:34
created

Jetpack_Sync_Functions   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 107
Duplicated Lines 23.36 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 21
c 2
b 0
f 1
lcom 1
cbo 1
dl 25
loc 107
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 2
A sync() 0 3 1
A register() 0 3 1
A trigger_sync() 0 3 1
A sync_sometimes() 14 15 3
A sync_all() 11 11 3
A get_check_sum() 0 7 2
A get_query_string() 0 3 1
A values() 0 14 4
A get_function_to_sync() 0 3 1
A get() 0 9 2

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
class Jetpack_Sync_Functions {
4
5
	static $functions = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $functions.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
6
		'wp_version'              => array( 'Jetpack', 'get_wp_version' ),
7
		'wp_max_upload_size'      => 'wp_max_upload_size',
8
		'featured_images_enabled' => array( 'Jetpack', 'featured_images_enabled' ),
9
	);
10
11
	static $multi_site_functions = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $multi_site_functions.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
12
		'network_name'                        => array( 'Jetpack', 'network_name' ),
13
		'network_allow_new_registrations'     => array( 'Jetpack', 'network_allow_new_registrations' ),
14
		'network_add_new_users'               => array( 'Jetpack', 'network_add_new_users' ),
15
		'network_site_upload_space'           => array( 'Jetpack', 'network_site_upload_space' ),
16
		'network_upload_file_types'           => array( 'Jetpack', 'network_upload_file_types' ),
17
		'network_enable_administration_menus' => array( 'Jetpack', 'network_enable_administration_menus' ),
18
	);
19
20
	static $sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
21
22
	static function init() {
23
		if ( is_multisite() ) {
24
			self::$functions = array_merge( self::$functions, self::$multi_site_functions );
25
		}
26
	}
27
28
	static function sync() {
29
		return self::values( self::get_function_to_sync() );
30
	}
31
32
	static function register( $key, $function ) {
33
		self::$functions[ $key ] = $function;
34
	}
35
36
	static function trigger_sync( $key ) {
37
		self::$sync[] = $key;
38
	}
39
40 View Code Duplication
	static function sync_sometimes() {
41
		// Since there are option in the sync we know that things have changed.
42
		if ( ! empty ( self::$sync ) ) {
43
			return self::sync_all();
44
		}
45
46
		$values    = self::values( self::$functions );
47
		$check_sum = self::get_check_sum( $values );
48
49
		if ( Jetpack_Options::get_option( 'function_check_sum' ) !== $check_sum ) {
50
			return self::sync_all( $values, $check_sum );
51
		}
52
53
		return array();
54
	}
55
56 View Code Duplication
	static function sync_all( $values = null, $check_sum = null ) {
57
		if ( is_null( $values ) ) {
58
			$values = self::values( array_keys( self::$functions ) );
59
		}
60
		if ( is_null( $check_sum ) ) {
61
			$check_sum = self::get_check_sum( $values );
62
		}
63
		Jetpack_Options::update_option( 'function_check_sum', $check_sum );
64
65
		return $values;
66
	}
67
68
	static function get_check_sum( $values = null ) {
69
		if ( is_null( $values ) ) {
70
			$values = self::values();
71
		}
72
73
		return crc32( self::get_query_string( $values ) );
74
	}
75
76
	static function get_query_string( $values ) {
77
		return build_query( $values );
78
	}
79
80
	static function values( $sync = array() ) {
81
		$values = array();
82
		if ( ! empty( $sync ) ) {
83
			foreach ( $sync as $key  ) {
84
				$value = self::get( $key );
85
				if( ! is_null( $value ) ) {
86
					$values[ $key ] = $value;
87
				}
88
89
			}
90
		}
91
92
		return $values;
93
	}
94
95
	static function get_function_to_sync() {
96
		return array_unique( self::$sync );
97
	}
98
99
	static function get( $key ) {
100
101
		if ( is_callable( self::$functions[ $key ] ) ) {
102
			return call_user_func( self::$functions[ $key ] );
103
		}
104
105
		return null;
106
107
	}
108
109
}
110
111
112
113
114
115