Completed
Pull Request — 2.x (#3632)
by Scott Kingsley
05:01
created

Pods_CLI_Command::add()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 6
nop 2
dl 25
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 5 and the first side effect is on line 141.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

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

Loading history...
2
/**
3
 * Implements Pods command for WP-CLI
4
 */
5
class Pods_CLI_Command extends WP_CLI_Command {
6
7
	/**
8
	 *
9
	 *
10
	 * @synopsis --pod=<pod> --<field>=<value>
11
	 */
12 View Code Duplication
	function add( $args, $assoc_args ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
13
14
		$pod  = $assoc_args['pod'];
15
		$item = pods_var_raw( 'item', $assoc_args );
16
17
		unset( $assoc_args['pod'] );
18
19
		if ( isset( $assoc_args['item'] ) ) {
20
			unset( $assoc_args['item'] );
21
		}
22
23
		if ( ! empty( $assoc_args ) ) {
24
			$id = pods( $pod, $item )->save( $assoc_args );
25
26
			if ( 0 < $id ) {
27
				WP_CLI::success( __( 'Pod item added', 'pods' ) );
28
				WP_CLI::line( "ID: {$id}" );
29
			} else {
30
				WP_CLI::error( __( 'Error saving pod item', 'pods' ) );
31
			}
32
		} else {
33
			WP_CLI::error( __( 'No data sent for saving', 'pods' ) );
34
		}
35
36
	}
37
38
	/**
39
	 *
40
	 *
41
	 * @synopsis --pod=<pod> [--item=<item>] --<field>=<value>
42
	 */
43 View Code Duplication
	function save( $args, $assoc_args ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
44
45
		$pod  = $assoc_args['pod'];
46
		$item = pods_var_raw( 'item', $assoc_args );
47
48
		unset( $assoc_args['pod'] );
49
50
		if ( isset( $assoc_args['item'] ) ) {
51
			unset( $assoc_args['item'] );
52
		}
53
54
		if ( ! empty( $assoc_args ) ) {
55
			$id = pods( $pod, $item )->save( $assoc_args );
56
57
			if ( 0 < $id ) {
58
				WP_CLI::success( __( 'Pod item saved', 'pods' ) );
59
				WP_CLI::line( "ID: {$id}" );
60
			} else {
61
				WP_CLI::error( __( 'Error saving pod item', 'pods' ) );
62
			}
63
		} else {
64
			WP_CLI::error( __( 'No data sent for saving', 'pods' ) );
65
		}
66
67
	}
68
69
	/**
70
	 *
71
	 *
72
	 * @synopsis --pod=<pod> --item=<item>
73
	 */
74 View Code Duplication
	function duplicate( $args, $assoc_args ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
75
76
		$id = pods( $assoc_args['pod'], $assoc_args['item'] )->duplicate();
77
78
		if ( 0 < $id ) {
79
			WP_CLI::success( __( 'Pod item duplicated', 'pods' ) );
80
			WP_CLI::line( "New ID: {$id}" );
81
		} else {
82
			WP_CLI::error( __( 'Error duplicating pod item', 'pods' ) );
83
		}
84
85
	}
86
87
	/**
88
	 *
89
	 *
90
	 * @synopsis --pod=<pod> --item=<item>
91
	 */
92
	function delete( $args, $assoc_args ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
94
		$deleted = pods( $assoc_args['pod'], $assoc_args['item'] )->delete();
95
96
		if ( $deleted ) {
97
			WP_CLI::success( __( 'Pod item deleted', 'pods' ) );
98
		} else {
99
			WP_CLI::error( __( 'Error deleting pod item', 'pods' ) );
100
		}
101
102
	}
103
104
	/**
105
	 *
106
	 *
107
	 * @synopsis --pod=<pod> --file=<file> [--item=<item>] [--format=<format>] [--fields=<fields>] [--depth=<depth>]
108
	 */
109
	/*function export ( $args, $assoc_args ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
111
		$params = array(
112
			'fields' => pods_var_raw( 'fields', $assoc_args ),
113
			'depth' => pods_var_raw( 'depth', $assoc_args )
114
		);
115
116
		$data = pods( $assoc_args[ 'pod' ], $assoc_args[ 'item' ] )->export( $params, null, pods_var_raw( 'format', $assoc_args ) );
117
118
		// @todo write to file
119
120
		// @todo success message
121
122
	}*/
123
124
	/**
125
	 *
126
	 *
127
	 * @synopsis --pod=<pod> --file=<file> [--format=<format>] [--numeric-mode=<numeric_mode>]
128
	 */
129
	/*function import ( $args, $assoc_args ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
131
		$data = array(); // @todo get data from file
132
133
		$ids = pods_api( $assoc_args[ 'pod' ] )->import( $data, (boolean) pods_var_raw( 'numeric_mode', $assoc_args, false, null, true ), pods_var_raw( 'format', $assoc_args ) );
134
135
		// @todo success message
136
137
	}*/
138
139
}
140
141
WP_CLI::add_command( 'pods', 'Pods_CLI_Command' );
142