SchemaController::save()   F
last analyzed

Complexity

Conditions 20
Paths 6150

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 0
c 0
b 0
f 0
cc 20
nc 6150
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file contains only one class.
4
 *
5
 * @package Tabulate
6
 * @file
7
 */
8
9
namespace WordPress\Tabulate\Controllers;
10
11
use \WordPress\Tabulate\DB\Database;
12
use \WordPress\Tabulate\DB\Table;
13
use \WordPress\Tabulate\DB\Column;
14
use \WordPress\Tabulate\Template;
15
16
/**
17
 * This controller handles creating, modifying, and deleting tables in the database.
18
 */
19
class SchemaController extends ControllerBase {
20
21
	/**
22
	 * View and edit the structure of the given table.
23
	 *
24
	 * @param string[] $args The request arguments.
25
	 * @return string
26
	 */
27
	public function index( $args ) {
28
		$template = new Template( 'table/schema.html' );
29
		if ( ! current_user_can( 'promote_users' ) ) {
30
			$template->add_notice( 'error', 'Only administrators are allowed to edit table structure.' );
31
		}
32
33
		$db = new Database( $this->wpdb );
34
		$template->action = 'structure';
35
		$template->tables = $db->get_tables();
36
		if ( isset( $args['table'] ) ) {
37
			$template->table = $db->get_table( $args['table'] );
38
			$template->record = $template->table->get_default_record();
0 ignored issues
show
Documentation introduced by Sam Wilson
The property table does not exist on object<WordPress\Tabulate\Template>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
39
		}
40
		$template->xtypes = Column::get_xtypes();
41
		return $template->render();
42
	}
43
44
	/**
45
	 * Add a new table and redirect to its schema-editing page.
46
	 *
47
	 * @param string[] $args The request arguments.
48
	 */
49
	public function newtable( $args ) {
50
		// Create table.
51
		$db = new Database( $this->wpdb );
52
		$table = $db->create_table( $args['new_table_name'] );
53
54
		// Redirect user with message.
55
		$template = new Template( 'table/schema.html' );
56
		$template->add_notice( 'updated', 'New table created' );
57
		$url = 'admin.php?page=tabulate&controller=schema&table=' . $table->get_name();
58
		wp_safe_redirect( admin_url( $url ) );
59
		exit;
60
	}
61
62
	/**
63
	 * Save modifications to a table's schema.
64
	 *
65
	 * @param string[] $args The request arguments.
66
	 */
67
	public function save( $args ) {
68
		if ( ! isset( $args['table'] ) || ! current_user_can( 'promote_users' ) ) {
69
			$url = admin_url( 'admin.php?page=tabulate' );
70
			wp_safe_redirect( $url );
71
			exit;
72
		}
73
		$db = new Database( $this->wpdb );
74
		$table = $db->get_table( $args['table'] );
75
		if ( isset( $args['delete'] ) ) {
76
			wp_safe_redirect( $table->get_url( 'delete', null, 'schema' ) );
0 ignored issues
show
Documentation introduced by Sam Wilson
null is of type null, but the function expects a array<integer,string>|boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
			exit;
78
		}
79
80
		// Rename.
81
		$new_name = $args['table'];
82
		if ( $table instanceof Table && ! empty( $args['new_name'] ) ) {
83
			$table->rename( $args['new_name'] );
84
			$new_name = $table->get_name();
85
		}
86
87
		// Set comment.
88
		if ( isset( $args['new_comment'] ) ) {
89
			$table->set_comment( $args['new_comment'] );
90
		}
91
92
		// Update columns.
93
		$previous_column_name = '';
94
		foreach ( $args['columns'] as $col_info ) {
95
			// Validate inputs.
96
			$old_col_name = isset( $col_info['old_name'] ) ? $col_info['old_name'] : null;
97
			$new_col_name = isset( $col_info['new_name'] ) ? $col_info['new_name'] : null;
98
			$xtype = isset( $col_info['xtype'] ) ? $col_info['xtype'] : null;
99
			$size = isset( $col_info['size'] ) ? wp_unslash( $col_info['size'] ) : null;
100
			$nullable = isset( $col_info['nullable'] );
101
			$default = isset( $col_info['default'] ) ? $col_info['default'] : null;
102
			$auto_increment = isset( $args['auto_increment'] ) && $args['auto_increment'] === $old_col_name;
103
			$unique = isset( $col_info['unique'] );
104
			$comment = isset( $col_info['comment'] ) ? $col_info['comment'] : null;
105
			$target_table = isset( $col_info['target_table'] ) ? $db->get_table( $col_info['target_table'] ) : null;
106
107
			// Change existing or insert new column.
108
			$altered = false;
109
			if ( $old_col_name ) {
110
				$col = $table->get_column( $col_info['old_name'] );
111
				if ( $col instanceof Column ) {
112
					$col->alter( $new_col_name, $xtype, $size, $nullable, $default, $auto_increment, $unique, $comment, $target_table, $previous_column_name );
113
					$altered = true;
114
				}
115
			}
116
			if ( ! $altered && ! empty( $new_col_name ) ) {
117
				$table->add_column( $new_col_name, $xtype, $size, $nullable, $default, $auto_increment, $unique, $comment, $target_table, $previous_column_name );
118
			}
119
120
			// Put the next column after this one.
121
			$previous_column_name = $new_col_name;
122
		}
123
124
		// Finish up.
125
		$template = new Template( 'table/schema.html' );
126
		$template->add_notice( 'updated', 'Schema updated.' );
127
		$url = admin_url( 'admin.php?page=tabulate&controller=schema&table=' . $new_name );
128
		wp_safe_redirect( $url );
129
		exit;
130
	}
131
132
	/**
133
	 * Delete (drop) a table.
134
	 *
135
	 * @param string[] $args The request arguments.
136
	 */
137
	public function delete( $args ) {
138
		$template = new Template( 'table/delete.html' );
139
		$db = new Database( $this->wpdb );
140
		$table = $db->get_table( $args['table'] );
141
142
		// Ask for confirmation.
143
		if ( ! isset( $args['confirm_deletion'] ) ) {
144
			$template->table = $table;
145
			return $template->render();
146
		}
147
148
		// Carry out deletion.
149
		$table->drop();
150
		$template->add_notice( 'updated', 'Table dropped.' );
151
		wp_safe_redirect( admin_url( 'admin.php?page=tabulate' ) );
152
		exit;
153
	}
154
}
155