Completed
Push — add/sync-rest-options ( f50eff...b51d37 )
by
unknown
17:58 queued 07:39
created

Jetpack_Options_Sync   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 16.36 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 10
c 2
b 0
f 2
lcom 3
cbo 0
dl 9
loc 55
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 2
A init_option() 0 5 1
A init_mock_option() 0 8 1
A delete_option() 0 3 1
A update_option() 9 9 2
A add_option() 0 3 1
A options_to_delete() 0 3 1
A options_to_sync() 0 3 1

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_Options_Sync {
4
5
	static $options_to_sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $options_to_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...
6
7
	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...
8
	static $delete = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $delete.

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...
9
10
	static function init() {
11
12
		foreach ( self::$options_to_sync as $option ) {
13
			self::init_option( $option );
14
		}
15
	}
16
17
	static function init_option( $option ) {
18
		add_action( "delete_option_{$option}", array( __CLASS__, 'delete_option' ) );
19
		add_action( "update_option_{$option}", array( __CLASS__, 'update_option' ) );
20
		add_action( "add_option_{$option}",    array( __CLASS__, 'add_option'   ) );
21
	}
22
23
	static function init_mock_option( $option, $callback ) {
24
		// The mock options get pre-fiexed with jetpack
25
		self::init_option(  'jetpack_' . $option );
26
27
		add_filter( 'pre_option_jetpack_'. $option, $callback );
28
		// This shouldn't happen but if it does we return the same as before.
29
		add_filter( 'option_jetpack_'. $option, $callback );
30
	}
31
32
	static function delete_option( $option ) {
33
		self::$delete[] = $option;
34
	}
35
36 View Code Duplication
	static function update_option() {
37
		$option = current_filter();
38
		$prefix = 'update_option_';
39
		if ( 0 !== strpos( $option, $prefix ) ) {
40
			return;
41
		}
42
		$option = substr( $option, strlen( $prefix ) );
43
		self::$sync[] = $option;
44
	}
45
46
	static function add_option( $option ) {
47
		self::$sync[] = $option;
48
	}
49
50
	static function options_to_delete() {
51
		return array_unique( self::$delete );
52
	}
53
54
	static function options_to_sync() {
55
		return array_unique( self::$sync );
56
	}
57
}