Completed
Push — feature/reorg ( 58a363...38f0cd )
by
unknown
104:53 queued 95:15
created

build-asset-cdn-json.php ➔ do_proc_open()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Script to build modules/photon-cdn/jetpack-manifest.php
4
 *
5
 * @package Jetpack
6
 */
7
8
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions.system_calls_proc_open, PHPCompatibility.ParameterValues.NewProcOpenCmdArray.Found, WordPress.WP.AlternativeFunctions
9
10
// The repo root path.
11
$jetpack_path = dirname( __DIR__ ) . '/';
12
13
// Build an iterator over all files in the repo that match the regex in the RegexIterator.
14
$directory = new RecursiveDirectoryIterator( $jetpack_path );
15
$iterator  = new RecursiveIteratorIterator( $directory );
16
$regex     = new RegexIterator( $iterator, '/^.+\.(css|js)$/i', RecursiveRegexIterator::GET_MATCH );
17
18
$ignore_paths = array(
19
	'_inc/client/',
20
	'extensions/',
21
	'logs/',
22
	'node_modules/',
23
	'tests/',
24
	'tools/',
25
	'vendor/',
26
);
27
28
$manifest = array();
29
foreach ( $regex as $path_to_file => $value ) {
30
	$path_from_repo_root = str_replace( $jetpack_path, '', $path_to_file );
31
32
	// Ignore top-level files.
33
	if ( false === strpos( $path_from_repo_root, '/' ) ) {
34
		continue;
35
	}
36
37
	// Ignore explicit ignore list.
38
	foreach ( $ignore_paths as $ignore_path ) {
39
		if ( 0 === strpos( $path_from_repo_root, $ignore_path ) ) {
40
			continue 2;
41
		}
42
	}
43
44
	$manifest[] = $path_from_repo_root;
45
}
46
47
/**
48
 * Wrapper for proc_open.
49
 *
50
 * @param array|string $cmd Command.
51
 * @param array        $pipes Output variable for pipes.
52
 * @return resource From `proc_open`.
53
 */
54
function do_proc_open( $cmd, &$pipes ) {
55
	global $jetpack_path;
56
57
	if ( is_array( $cmd ) && version_compare( PHP_VERSION, '7.4.0', '<' ) ) {
58
		// PHP <7.4 doesn't support an array, so convert it to a string.
59
		$cmd = implode( ' ', array_map( 'escapeshellarg', $cmd ) );
60
	}
61
	return proc_open(
62
		$cmd,
63
		array( array( 'pipe', 'r' ), array( 'pipe', 'w' ), STDERR ),
64
		$pipes,
65
		$jetpack_path
66
	);
67
}
68
69
// Make phpcs happy. These get set by the `do_proc_open` call.
70
$ignore_pipes  = null;
71
$include_pipes = null;
72
$exclude_pipes = null;
73
74
// Use .gitignore and .gitattributes to select files to include.
75
$ignore  = do_proc_open( array( 'git', 'check-ignore', '--stdin', '-v', '-n' ), $ignore_pipes );
0 ignored issues
show
Documentation introduced by
$ignore_pipes is of type null, but the function expects a array.

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...
76
$include = do_proc_open( array( 'git', 'check-attr', '--stdin', 'production-include' ), $include_pipes );
0 ignored issues
show
Documentation introduced by
$include_pipes is of type null, but the function expects a array.

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
$exclude = do_proc_open( array( 'git', 'check-attr', '--stdin', 'production-exclude' ), $exclude_pipes );
0 ignored issues
show
Documentation introduced by
$exclude_pipes is of type null, but the function expects a array.

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...
78
foreach ( $manifest as $i => $file ) {
79
	fwrite( $ignore_pipes[0], "$file\n" );
80
	$res = array_map( 'trim', explode( ':', fgets( $ignore_pipes[1] ) ) );
81
	if ( '' !== $res[0] ) {
82
		// File is ignored. Check if it's included anyway.
83
		fwrite( $include_pipes[0], "$file\n" );
84
		$res = array_map( 'trim', explode( ':', fgets( $include_pipes[1] ) ) );
85
		if ( 'production-include' === $res[1] && ( 'unspecified' === $res[2] || 'unset' === $res[2] ) ) {
86
			// File is not included. Skip it.
87
			unset( $manifest[ $i ] );
88
			continue;
89
		}
90
	}
91
92
	fwrite( $exclude_pipes[0], "$file\n" );
93
	$res = array_map( 'trim', explode( ':', fgets( $exclude_pipes[1] ) ) );
94
	if ( 'production-exclude' === $res[1] && 'unspecified' !== $res[2] && 'unset' !== $res[2] ) {
95
		// File is excluded. Skip it.
96
		unset( $manifest[ $i ] );
97
	}
98
}
99
fclose( $ignore_pipes[0] );
100
fclose( $ignore_pipes[1] );
101
fclose( $include_pipes[0] );
102
fclose( $include_pipes[1] );
103
fclose( $exclude_pipes[0] );
104
fclose( $exclude_pipes[1] );
105
proc_close( $ignore );
106
proc_close( $include );
107
proc_close( $exclude );
108
109
sort( $manifest );
110
$export = var_export( $manifest, true ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
111
112
file_put_contents(
113
	$jetpack_path . 'modules/photon-cdn/jetpack-manifest.php',
114
	"<?php
115
// This file is autogenerated by bin/build-asset-cdn-json.php
116
117
\$assets = $export;\n"
118
);
119