Completed
Push — update/build-sort-by-deps ( e2d820...b39fe2 )
by
unknown
58:05 queued 47:05
created

get-build-order.php ➔ is_falsey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env php
2
<?php
3
/**
4
 * Tool to list projects in dependency order for build.
5
 *
6
 * @package automattic/jetpack
7
 */
8
9
// phpcs:disable WordPress.WP.AlternativeFunctions, WordPress.PHP.DiscouragedPHPFunctions, WordPress.Security.EscapeOutput.OutputNotEscaped
10
11
ob_start();
12
require_once __DIR__ . '/find-project-deps.php';
13
ob_end_clean();
14
15
$debug_color = getenv( 'CI' ) ? '34' : '1;30';
16
17
/**
18
 * Test if a variable is falsey.
19
 *
20
 * @param mixed $v Variable to test.
21
 * @return bool
22
 */
23
function is_falsey( $v ) {
24
	return ! $v;
25
}
26
27
// We look for packages that have no outgoing dependencies, collect then and remove them from the dependency graph, then repeat.
28
// This is basically Kahn's algorithm with some steps interleaved.
29
$deps = get_dependencies();
30
unset( $deps['monorepo'] );
31
$output = array();
32
while ( $deps ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $deps of type string[][] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
	$ok = array_keys( array_filter( $deps, 'is_falsey' ) );
34
	if ( ! $ok ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $ok of type array<integer|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
		if ( getenv( 'CI' ) ) {
36
			$lf = '%%0A';
37
			fprintf( STDERR, '::error::The dependency graph contains a cycle!' );
38
			$end = "\n";
39
		} else {
40
			$lf = "\n";
41
			fprintf( STDERR, "\e[1;37;41mThe dependency graph contains a cycle!\e[0m" );
42
			$end = '';
43
		}
44
		fprintf( STDERR, " Involved dependencies are:${lf}" );
45
		$l = 0;
46
		foreach ( $deps as $k => $v ) {
47
			$l = max( $l, strlen( $k ) );
48
		}
49
		foreach ( $deps as $k => $v ) {
50
			fprintf( STDERR, "  %${l}s -> %s${lf}", $k, implode( ' ', $v ) );
51
		}
52
		fprintf( STDERR, $end );
53
		exit( 1 );
54
	}
55
	fprintf( STDERR, "\e[${debug_color}mReady at this step: %s\e[0m\n", implode( ' ', $ok ) );
56
57
	$output = array_merge( $output, $ok );
58
	foreach ( $ok as $slug ) {
59
		unset( $deps[ $slug ] );
60
	}
61
	foreach ( $deps as &$v ) {
62
		$v = array_diff( $v, $ok );
63
	}
64
	unset( $v );
65
}
66
67
echo implode( "\n", $output ) . "\n";
68