Use PHP 7 '??' operator instead of '?:' with 'isset()' where convenient
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 8c7e52a..72fda55 100644 (file)
@@ -121,7 +121,7 @@ function wfArrayDiff2_cmp( $a, $b ) {
        if ( is_string( $a ) && is_string( $b ) ) {
                return strcmp( $a, $b );
        } elseif ( count( $a ) !== count( $b ) ) {
-               return count( $a ) < count( $b ) ? -1 : 1;
+               return count( $a ) <=> count( $b );
        } else {
                reset( $a );
                reset( $b );
@@ -1516,7 +1516,7 @@ function wfBacktrace( $raw = null ) {
 
        $frames = array_map( function ( $frame ) use ( $frameFormat ) {
                $file = !empty( $frame['file'] ) ? basename( $frame['file'] ) : '-';
-               $line = isset( $frame['line'] ) ? $frame['line'] : '-';
+               $line = $frame['line'] ?? '-';
                $call = $frame['function'];
                if ( !empty( $frame['class'] ) ) {
                        $call = $frame['class'] . $frame['type'] . $call;
@@ -2241,7 +2241,7 @@ function wfShellExec( $cmd, &$retval = null, $environ = [],
        }
 
        $includeStderr = isset( $options['duplicateStderr'] ) && $options['duplicateStderr'];
-       $profileMethod = isset( $options['profileMethod'] ) ? $options['profileMethod'] : wfGetCaller();
+       $profileMethod = $options['profileMethod'] ?? wfGetCaller();
 
        try {
                $result = Shell::command( [] )
@@ -2670,6 +2670,28 @@ function wfGetPrecompiledData( $name ) {
        return false;
 }
 
+/**
+ * @since 1.32
+ * @param string[] $data Array with string keys/values to export
+ * @param string $header
+ * @return string PHP code
+ */
+function wfMakeStaticArrayFile( array $data, $header = 'Automatically generated' ) {
+       $format = "\t%s => %s,\n";
+       $code = "<?php\n"
+               . "// " . implode( "\n// ", explode( "\n", $header ) ) . "\n"
+               . "return [\n";
+       foreach ( $data as $key => $value ) {
+               $code .= sprintf(
+                       $format,
+                       var_export( $key, true ),
+                       var_export( $value, true )
+               );
+       }
+       $code .= "];\n";
+       return $code;
+}
+
 /**
  * Make a cache key for the local wiki.
  *