WP_PHPUnit_Util_Getopt::__construct()   D
last analyzed

Complexity

Conditions 17
Paths 260

Size

Total Lines 57
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 36
nc 260
nop 1
dl 0
loc 57
rs 4.79
c 0
b 0
f 0

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
 * Installs WordPress for running the tests and loads WordPress and the test libraries
4
 */
5
6
7
$config_file_path = dirname( dirname( __FILE__ ) );
8
if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
9
	// Support the config file from the root of the develop repository.
10
	if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
11
			$config_file_path = dirname( dirname( $config_file_path ) );
12
	}
13
	}
14
$config_file_path .= '/wp-tests-config.php';
15
16
/*
17
 * Globalize some WordPress variables, because PHPUnit loads this file inside a function
18
 * See: https://github.com/sebastianbergmann/phpunit/issues/325
19
 */
20
global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer, $wp_theme_directories;
21
22
if ( !is_readable( $config_file_path ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
23
	die( "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n" );
24
}
25
require_once $config_file_path;
26
require_once dirname( __FILE__ ) . '/functions.php';
27
28
tests_reset__SERVER();
29
30
define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
31
define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
32
33
define( 'WP_LANG_DIR', DIR_TESTDATA . '/languages' );
34
35
if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) ) {
36
	define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
37
}
38
39
// Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only
40
define( 'DISABLE_WP_CRON', true );
41
42
define( 'WP_MEMORY_LIMIT', -1 );
43
define( 'WP_MAX_MEMORY_LIMIT', -1 );
44
45
define( 'REST_TESTS_IMPOSSIBLY_HIGH_NUMBER', 99999999 );
46
47
$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
48
49
// Should we run in multisite mode?
50
$multisite = '1' == getenv( 'WP_MULTISITE' );
51
$multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
52
$multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );
53
54
// Override the PHPMailer
55
require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
56
$phpmailer = new MockPHPMailer( true );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
57
58
if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
59
	define( 'WP_DEFAULT_THEME', 'default' );
60
}
61
$wp_theme_directories = array( DIR_TESTDATA . '/themedir1' );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
62
63
system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite );
64
65
if ( $multisite ) {
66
	echo "Running as multisite..." . PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
67
	defined( 'MULTISITE' ) or define( 'MULTISITE', true );
68
	defined( 'SUBDOMAIN_INSTALL' ) or define( 'SUBDOMAIN_INSTALL', false );
69
	$GLOBALS['base'] = '/';
70
} else {
71
	echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
72
}
73
unset( $multisite );
74
75
$GLOBALS['_wp_die_disabled'] = false;
76
// Allow tests to override wp_die
77
tests_add_filter( 'wp_die_handler', '_wp_die_handler_filter' );
78
79
// Preset WordPress options defined in bootstrap file.
80
// Used to activate themes, plugins, as well as  other settings.
81
if(isset($GLOBALS['wp_tests_options'])) {
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
No space before closing parenthesis is prohibited
Loading history...
82
	function wp_tests_options( $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
		$key = substr( current_filter(), strlen( 'pre_option_' ) );
84
		return $GLOBALS['wp_tests_options'][$key];
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
85
	}
86
87
	foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
88
		tests_add_filter( 'pre_option_'.$key, 'wp_tests_options' );
89
	}
90
}
91
92
// Load WordPress
93
require_once ABSPATH . '/wp-settings.php';
94
95
// Delete any default posts & related data
96
_delete_all_posts();
97
98
require dirname( __FILE__ ) . '/testcase.php';
99
require dirname( __FILE__ ) . '/testcase-rest-api.php';
100
require dirname( __FILE__ ) . '/testcase-rest-controller.php';
101
require dirname( __FILE__ ) . '/testcase-rest-post-type-controller.php';
102
require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
103
require dirname( __FILE__ ) . '/testcase-ajax.php';
104
require dirname( __FILE__ ) . '/testcase-canonical.php';
105
require dirname( __FILE__ ) . '/exceptions.php';
106
require dirname( __FILE__ ) . '/utils.php';
107
require dirname( __FILE__ ) . '/spy-rest-server.php';
108
109
/**
110
 * A child class of the PHP test runner.
111
 *
112
 * Used to access the protected longOptions property, to parse the arguments
113
 * passed to the script.
114
 *
115
 * If it is determined that phpunit was called with a --group that corresponds
116
 * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked
117
 * as #WP12345), then it is assumed that known bugs should not be skipped.
118
 *
119
 * If WP_TESTS_FORCE_KNOWN_BUGS is already set in wp-tests-config.php, then
120
 * how you call phpunit has no effect.
121
 */
122
class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
123
	protected $longOptions = array(
124
	  'exclude-group=',
125
	  'group=',
126
	);
127
	function __construct( $argv ) {
128
		array_shift( $argv );
129
		$options = array();
130
		while ( list( $i, $arg ) = each( $argv ) ) {
0 ignored issues
show
Unused Code introduced by
The assignment to $i is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
131
			try {
132
				if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
133
					PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
134
				}
135
			} catch ( PHPUnit_Framework_Exception $e ) {
0 ignored issues
show
Bug introduced by
The class PHPUnit_Framework_Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
136
				// Enforcing recognized arguments or correctly formed arguments is
137
				// not really the concern here.
138
				continue;
139
			}
140
		}
141
142
		$skipped_groups = array(
143
			'ajax' => true,
144
			'ms-files' => true,
145
			'external-http' => true,
146
		);
147
148
		foreach ( $options as $option ) {
149
			switch ( $option[0] ) {
150
				case '--exclude-group' :
151
					foreach ( $skipped_groups as $group_name => $skipped ) {
152
						$skipped_groups[ $group_name ] = false;
153
					}
154
					continue 2;
155
				case '--group' :
156
					$groups = explode( ',', $option[1] );
157
					foreach ( $groups as $group ) {
158
						if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
159
							WP_UnitTestCase::forceTicket( $group );
160
						}
161
					}
162
163
					foreach ( $skipped_groups as $group_name => $skipped ) {
164
						if ( in_array( $group_name, $groups ) ) {
165
							$skipped_groups[ $group_name ] = false;
166
						}
167
					}
168
					continue 2;
169
			}
170
		}
171
172
		$skipped_groups = array_filter( $skipped_groups );
173
		foreach ( $skipped_groups as $group_name => $skipped ) {
174
			echo sprintf( 'Not running %1$s tests. To execute these, use --group %1$s.', $group_name ) . PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
Loading history...
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
175
		}
176
177
		if ( ! isset( $skipped_groups['external-http'] ) ) {
178
			echo PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
179
			echo 'External HTTP skipped tests can be caused by timeouts.' . PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
180
			echo 'If this changeset includes changes to HTTP, make sure there are no timeouts.' . PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
181
			echo PHP_EOL;
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'PHP_EOL'
Loading history...
182
		}
183
    }
184
}
185
new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );
0 ignored issues
show
introduced by
Detected usage of a non-validated input variable: $_SERVER
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
186