Completed
Push — milestone/2.0 ( 0dfc11...693dca )
by
unknown
03:12
created

Theme_Options_Datastore   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 23.21 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 26
loc 112
ccs 0
cts 59
cp 0
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 1 1
A get_storage_array() 0 16 1
A save_key_value_pair() 0 3 1
A save_key_value_pair_with_autoload() 0 10 3
B save() 26 26 5
A delete() 0 16 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
namespace Carbon_Fields\Datastore;
4
5
use Carbon_Fields\Field\Field;
6
7
/**
8
 * Theme options datastore class.
9
 */
10
class Theme_Options_Datastore extends Key_Value_Datastore {
11
	/**
12
	 * Initialization tasks.
13
	 **/
14
	public function init() {}
15
16
	/**
17
	 * Get a raw database query results array for a field
18
	 *
19
	 * @param Field $field The field to retrieve value for.
20
	 * @param array $storage_key_patterns
21
	 * @return array<stdClass> Array of {key, value} objects
22
	 */
23
	protected function get_storage_array( Field $field, $storage_key_patterns ) {
24
		global $wpdb;
25
26
		$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`option_name`', $storage_key_patterns );
27
28
		$storage_array = $wpdb->get_results( '
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
29
			SELECT `option_name` AS `key`, `option_value` AS `value`
30
			FROM ' . $wpdb->options . '
31
			WHERE ' . $storage_key_comparisons . '
32
			ORDER BY `option_name` ASC
33
		' );
34
35
		$storage_array = apply_filters( 'crb_datastore_storage_array', $storage_array, $this, $storage_key_patterns );
36
37
		return $storage_array;
38
	}
39
40
	/**
41
	 * Save a single key-value pair to the database
42
	 *
43
	 * @param string $key
44
	 * @param string $value
45
	 */
46
	protected function save_key_value_pair( $key, $value ) {
47
		$this->save_key_value_pair_with_autoload( $key, $value, false );
48
	}
49
50
	/**
51
	 * Save a single key-value pair to the database with autoload
52
	 *
53
	 * @param string $key
54
	 * @param string $value
55
	 * @param bool $autoload
56
	 */
57
	protected function save_key_value_pair_with_autoload( $key, $value, $autoload = true ) {
58
		$autoload = $autoload ? 'yes': 'no';
59
		$notoptions = wp_cache_get( 'notoptions', 'options' );
60
		$notoptions[ $key ] = '';
61
		wp_cache_set( 'notoptions', $notoptions, 'options' );
62
63
		if ( ! add_option( $key, $value, null, $autoload ) ) {
64
			update_option( $key, $value, $autoload );
65
		}
66
	}
67
68
	/**
69
	 * Save the field value(s)
70
	 *
71
	 * @param Field $field The field to save.
72
	 */
73 View Code Duplication
	public function save( Field $field ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
		$value_set = $field->get_full_value();
75
76
		if ( empty( $value_set ) && $field->get_value_set()->keepalive() ) {
77
			$storage_key = $this->key_toolset->get_storage_key(
78
				$field->is_simple_root_field(),
79
				$this->get_full_hierarchy_for_field( $field ),
80
				$this->get_full_hierarchy_index_for_field( $field ),
81
				0,
82
				$this->key_toolset::KEEPALIVE_PROPERTY
83
			);
84
			$this->save_key_value_pair_with_autoload( $storage_key, '', $field->get_autoload() );
85
		}
86
		foreach ( $value_set as $value_group_index => $values ) {
1 ignored issue
show
Bug introduced by
The expression $value_set of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
87
			foreach ( $values as $property => $value ) {
88
				$storage_key = $this->key_toolset->get_storage_key(
89
					$field->is_simple_root_field(),
90
					$this->get_full_hierarchy_for_field( $field ),
91
					$this->get_full_hierarchy_index_for_field( $field ),
92
					$value_group_index,
93
					$property
94
				);
95
				$this->save_key_value_pair_with_autoload( $storage_key, $value, $field->get_autoload() );
96
			}
97
		}
98
	}
99
100
	/**
101
	 * Delete the field value(s)
102
	 *
103
	 * @param Field $field The field to delete.
104
	 */
105
	public function delete( Field $field ) {
106
		global $wpdb;
107
108
		$storage_key_patterns = $this->key_toolset->get_storage_key_deleter_patterns(
109
			is_a( $field, 'Carbon_Fields\\Field\\Complex_Field' ),
110
			$field->is_simple_root_field(),
111
			$this->get_full_hierarchy_for_field( $field ),
112
			$this->get_full_hierarchy_index_for_field( $field )
113
		);
114
		$storage_key_comparisons = $this->key_toolset->storage_key_patterns_to_sql( '`option_name`', $storage_key_patterns );
115
116
		$wpdb->query( '
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
117
			DELETE FROM ' . $wpdb->options . '
118
			WHERE ' . $storage_key_comparisons . '
119
		' );
120
	}
121
}
122