Completed
Pull Request — master (#2)
by Stephen
13:19
created

WP_PHPUnit_Util_Getopt::__construct()   D

Complexity

Conditions 17
Paths 260

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 36
nc 260
nop 1
dl 0
loc 58
rs 4.738
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
 * Compatibility with PHPUnit 6+
8
 */
9
if ( class_exists( 'PHPUnit\Runner\Version' ) ) {
10
	require_once dirname( __FILE__ ) . '/phpunit6-compat.php';
11
}
12
13
$config_file_path = dirname( dirname( __FILE__ ) );
14
if ( ! file_exists( $config_file_path . '/wp-tests-config.php' ) ) {
15
	// Support the config file from the root of the develop repository.
16
	if ( basename( $config_file_path ) === 'phpunit' && basename( dirname( $config_file_path ) ) === 'tests' )
17
		$config_file_path = dirname( dirname( $config_file_path ) );
18
}
19
$config_file_path .= '/wp-tests-config.php';
20
21
/*
22
 * Globalize some WordPress variables, because PHPUnit loads this file inside a function
23
 * See: https://github.com/sebastianbergmann/phpunit/issues/325
24
 */
25
global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer, $wp_theme_directories;
26
27
if ( ! is_readable( $config_file_path ) ) {
28
	echo "ERROR: wp-tests-config.php is missing! Please use wp-tests-config-sample.php to create a config file.\n";
29
	exit( 1 );
30
}
31
require_once $config_file_path;
32
require_once dirname( __FILE__ ) . '/functions.php';
33
34
tests_reset__SERVER();
35
36
define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
37
define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );
38
39
define( 'WP_LANG_DIR', DIR_TESTDATA . '/languages' );
40
41
if ( ! defined( 'WP_TESTS_FORCE_KNOWN_BUGS' ) )
42
	define( 'WP_TESTS_FORCE_KNOWN_BUGS', false );
43
44
// Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only
45
define( 'DISABLE_WP_CRON', true );
46
47
define( 'WP_MEMORY_LIMIT', -1 );
48
define( 'WP_MAX_MEMORY_LIMIT', -1 );
49
50
define( 'REST_TESTS_IMPOSSIBLY_HIGH_NUMBER', 99999999 );
51
52
$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
53
54
// Should we run in multisite mode?
55
$multisite = '1' == getenv( 'WP_MULTISITE' );
56
$multisite = $multisite || ( defined( 'WP_TESTS_MULTISITE') && WP_TESTS_MULTISITE );
57
$multisite = $multisite || ( defined( 'MULTISITE' ) && MULTISITE );
58
59
// Override the PHPMailer
60
require_once( dirname( __FILE__ ) . '/mock-mailer.php' );
61
$phpmailer = new MockPHPMailer( true );
62
63
if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
64
	define( 'WP_DEFAULT_THEME', 'default' );
65
}
66
$wp_theme_directories = array( DIR_TESTDATA . '/themedir1' );
67
68
system( WP_PHP_BINARY . ' ' . escapeshellarg( dirname( __FILE__ ) . '/install.php' ) . ' ' . escapeshellarg( $config_file_path ) . ' ' . $multisite );
69
70
if ( $multisite ) {
71
	echo "Running as multisite..." . PHP_EOL;
72
	defined( 'MULTISITE' ) or define( 'MULTISITE', true );
73
	defined( 'SUBDOMAIN_INSTALL' ) or define( 'SUBDOMAIN_INSTALL', false );
74
	$GLOBALS['base'] = '/';
75
} else {
76
	echo "Running as single site... To run multisite, use -c tests/phpunit/multisite.xml" . PHP_EOL;
77
}
78
unset( $multisite );
79
80
$GLOBALS['_wp_die_disabled'] = false;
81
// Allow tests to override wp_die
82
tests_add_filter( 'wp_die_handler', '_wp_die_handler_filter' );
83
84
// Preset WordPress options defined in bootstrap file.
85
// Used to activate themes, plugins, as well as  other settings.
86
if(isset($GLOBALS['wp_tests_options'])) {
87
	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...
88
		$key = substr( current_filter(), strlen( 'pre_option_' ) );
89
		return $GLOBALS['wp_tests_options'][$key];
90
	}
91
92
	foreach ( array_keys( $GLOBALS['wp_tests_options'] ) as $key ) {
93
		tests_add_filter( 'pre_option_'.$key, 'wp_tests_options' );
94
	}
95
}
96
97
// Load WordPress
98
require_once ABSPATH . '/wp-settings.php';
99
100
// Delete any default posts & related data
101
_delete_all_posts();
102
103
require dirname( __FILE__ ) . '/testcase.php';
104
require dirname( __FILE__ ) . '/testcase-rest-api.php';
105
require dirname( __FILE__ ) . '/testcase-rest-controller.php';
106
require dirname( __FILE__ ) . '/testcase-rest-post-type-controller.php';
107
require dirname( __FILE__ ) . '/testcase-xmlrpc.php';
108
require dirname( __FILE__ ) . '/testcase-ajax.php';
109
require dirname( __FILE__ ) . '/testcase-canonical.php';
110
require dirname( __FILE__ ) . '/exceptions.php';
111
require dirname( __FILE__ ) . '/utils.php';
112
require dirname( __FILE__ ) . '/spy-rest-server.php';
113
114
/**
115
 * A child class of the PHP test runner.
116
 *
117
 * Used to access the protected longOptions property, to parse the arguments
118
 * passed to the script.
119
 *
120
 * If it is determined that phpunit was called with a --group that corresponds
121
 * to an @ticket annotation (such as `phpunit --group 12345` for bugs marked
122
 * as #WP12345), then it is assumed that known bugs should not be skipped.
123
 *
124
 * If WP_TESTS_FORCE_KNOWN_BUGS is already set in wp-tests-config.php, then
125
 * how you call phpunit has no effect.
126
 */
127
class WP_PHPUnit_Util_Getopt extends PHPUnit_Util_Getopt {
128
	protected $longOptions = array(
129
	  'exclude-group=',
130
	  'group=',
131
	);
132
	function __construct( $argv ) {
133
		array_shift( $argv );
134
		$options = array();
135
		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...
136
			try {
137
				if ( strlen( $arg ) > 1 && $arg[0] === '-' && $arg[1] === '-' ) {
138
					PHPUnit_Util_Getopt::parseLongOption( substr( $arg, 2 ), $this->longOptions, $options, $argv );
139
				}
140
			}
141
			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...
142
				// Enforcing recognized arguments or correctly formed arguments is
143
				// not really the concern here.
144
				continue;
145
			}
146
		}
147
148
		$skipped_groups = array(
149
			'ajax' => true,
150
			'ms-files' => true,
151
			'external-http' => true,
152
		);
153
154
		foreach ( $options as $option ) {
155
			switch ( $option[0] ) {
156
				case '--exclude-group' :
157
					foreach ( $skipped_groups as $group_name => $skipped ) {
158
						$skipped_groups[ $group_name ] = false;
159
					}
160
					continue 2;
161
				case '--group' :
162
					$groups = explode( ',', $option[1] );
163
					foreach ( $groups as $group ) {
164
						if ( is_numeric( $group ) || preg_match( '/^(UT|Plugin)\d+$/', $group ) ) {
165
							WP_UnitTestCase::forceTicket( $group );
166
						}
167
					}
168
169
					foreach ( $skipped_groups as $group_name => $skipped ) {
170
						if ( in_array( $group_name, $groups ) ) {
171
							$skipped_groups[ $group_name ] = false;
172
						}
173
					}
174
					continue 2;
175
			}
176
		}
177
178
		$skipped_groups = array_filter( $skipped_groups );
179
		foreach ( $skipped_groups as $group_name => $skipped ) {
180
			echo sprintf( 'Not running %1$s tests. To execute these, use --group %1$s.', $group_name ) . PHP_EOL;
181
		}
182
183
		if ( ! isset( $skipped_groups['external-http'] ) ) {
184
			echo PHP_EOL;
185
			echo 'External HTTP skipped tests can be caused by timeouts.' . PHP_EOL;
186
			echo 'If this changeset includes changes to HTTP, make sure there are no timeouts.' . PHP_EOL;
187
			echo PHP_EOL;
188
		}
189
    }
190
}
191
new WP_PHPUnit_Util_Getopt( $_SERVER['argv'] );
192