Completed
Push — development ( a57866...c9d234 )
by
unknown
02:32
created

Network_Container   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 13.85 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 9
loc 65
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A init() 0 6 2
A render() 9 9 3
A set_site_id() 0 16 3
A get_site_id() 0 3 1
A site_exists() 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
namespace Carbon_Fields\Container;
4
5
use Carbon_Fields\Datastore\Datastore;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Theme options container class.
10
 */
11
class Network_Container extends Theme_Options_Container {
12
	/**
13
	 * ID of the site the container is working with
14
	 *
15
	 * @see init()
16
	 * @var int
17
	 */
18
	protected $site_id;
19
20
	public function __construct( $id, $title, $type, $condition_collection, $condition_translator ) {
21
		parent::__construct( $id, $title, $type, $condition_collection, $condition_translator );
22
23
		if( !is_multisite() ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
24
			Incorrect_Syntax_Exception::raise( 'The ' . $title. ' is unavailable because your site is not multisite. ' );
25
			return ;
26
		}
27
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
28
29
		$this->set_datastore( Datastore::make( 'network' ), $this->has_default_datastore() );
30
31
		$this->set_site_id( SITE_ID_CURRENT_SITE );
32
	}
33
	
34
	public function init() {
35
		$registered = $this->register_page();
36
		if ( $registered ) {
37
			add_action( 'network_admin_menu', array( $this, '_attach' ) );
38
		}
39
	}
40
	
41 View Code Duplication
	public function render() {
0 ignored issues
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...
42
		$input = stripslashes_deep( $_GET );
43
		$request_settings_updated = isset( $input['settings-updated'] ) ? $input['settings-updated'] : '';
44
		if ( $request_settings_updated === 'true' ) {
45
			$this->notifications[] = __( 'Settings saved.', 'carbon-fields' );
46
		}
47
48
		include \Carbon_Fields\DIR . '/templates/Container/network.php';
49
	}
50
51
	public function set_site_id( $id ) {
52
		if( !function_exists( 'get_blog_status' ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
53
			return $this;
54
		}
55
56
		if( $this->site_exists( $id ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
57
			$this->site_id = $id;
58
		} else {
59
			Incorrect_Syntax_Exception::raise( 'The specified site id ' . $id . ' does not exist' );
60
			return $this;
61
		}
62
63
		$this->datastore->set_object_id( $this->get_site_id() );
64
65
		return $this;
66
	}
67
68
	protected function get_site_id() {
69
		return $this->site_id;
70
	} 
71
72
	protected function site_exists( $id ) {
73
		return ( bool ) get_blog_status( $id, 'domain' );
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(bool)" but found "( bool )"
Loading history...
74
	}
75
}
76