$w ) { foreach ( $w as $one ) { if ( ! isset( $out[ $s ] ) ) { $out[ $s ] = array(); } if ( count( $out[ $s ] ) < 3 && ! in_array( $one, $out[ $s ], true ) ) { $out[ $s ][] = $one; } } } } ksort( $out, SORT_STRING ); return $out; } public static function from_source( $php, $where ) { $tokens = array_values( array_filter( token_get_all( (string) $php ), static function ( $t ) { return ! ( is_array( $t ) && in_array( $t[0], array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT ), true ) ); } ) ); $names = self::frame_names( $tokens ); $out = array(); $stack = array(); $key = null; $n = count( $tokens ); for ( $i = 0; $i < $n; $i++ ) { $t = $tokens[ $i ]; $id = is_array( $t ) ? $t[0] : $t; $prev = $i > 0 ? $tokens[ $i - 1 ] : null; if ( T_CONSTANT_ENCAPSED_STRING === $id ) { $next = $tokens[ $i + 1 ] ?? null; $lit = self::literal( $t[1] ); if ( is_array( $next ) && T_DOUBLE_ARROW === $next[0] ) { $key = $lit; $i++; continue; } $after = is_array( $next ) ? $next[0] : $next; $complete = in_array( $after, array( ',', ')', ']' ), true ); $in_array = ! empty( $stack ) && end( $stack )['array']; if ( $complete && $in_array && null !== $key && self::translated( $key, self::path( $stack ) ) ) { $out[ $lit ][] = self::where( $stack, $names, $where, $key ); } $key = null; continue; } if ( '(' === $id || '[' === $id ) { $pid = is_array( $prev ) ? $prev[0] : $prev; $is_array = ( '(' === $id && T_ARRAY === $pid ) || ( '[' === $id && ! in_array( $pid, array( T_VARIABLE, ']', ')', T_STRING, '}' ), true ) ); $stack[] = array( 'array' => $is_array, 'key' => $is_array ? $key : null, 'at' => $i ); $key = null; continue; } if ( ')' === $id || ']' === $id ) { array_pop( $stack ); $key = null; continue; } if ( ',' === $id ) { $key = null; continue; } if ( T_ARRAY === $id || T_DOUBLE_ARROW === $id ) { continue; } $key = null; } foreach ( $out as $s => $w ) { if ( '' === trim( (string) $s ) ) { unset( $out[ $s ] ); } } return $out; } private static function translated( $key, array $path ) { $parent = end( $path ); $grand = count( $path ) > 1 ? $path[ count( $path ) - 2 ] : ''; if ( 'labels' === $parent ) { return true; } if ( 'options' === $parent && 'param' === $grand ) { return true; } if ( in_array( $key, array( 'hint', 'tip' ), true ) ) { return in_array( 'figures', $path, true ); } if ( in_array( $key, array( 'lead', 'text' ), true ) ) { return in_array( 'guidance', $path, true ); } if ( 'label' === $key && 'format' === $parent ) { return false; } return in_array( $key, self::KEYS, true ); } private static function frame_names( array $tokens ) { $names = array(); $open = array(); $n = count( $tokens ); for ( $i = 0; $i < $n; $i++ ) { $t = $tokens[ $i ]; $id = is_array( $t ) ? $t[0] : $t; if ( '(' === $id || '[' === $id ) { $open[] = $i; } elseif ( ')' === $id || ']' === $id ) { array_pop( $open ); } elseif ( T_CONSTANT_ENCAPSED_STRING === $id && $open && is_array( $tokens[ $i + 1 ] ?? null ) && T_DOUBLE_ARROW === $tokens[ $i + 1 ][0] && in_array( self::literal( $t[1] ), array( 'id', 'key' ), true ) && is_array( $tokens[ $i + 2 ] ?? null ) && T_CONSTANT_ENCAPSED_STRING === $tokens[ $i + 2 ][0] && in_array( is_array( $tokens[ $i + 3 ] ?? null ) ? $tokens[ $i + 3 ][0] : ( $tokens[ $i + 3 ] ?? '' ), array( ',', ')', ']' ), true ) ) { $names[ end( $open ) ] = self::literal( $tokens[ $i + 2 ][1] ); } } return $names; } private static function where( array $stack, array $names, $file, $key ) { $report = ''; $inner = ''; foreach ( $stack as $f ) { if ( isset( $names[ $f['at'] ] ) ) { if ( '' === $report ) { $report = $names[ $f['at'] ]; } else { $inner = $names[ $f['at'] ]; } } } $parts = array( '' !== $report ? $report : $file ); if ( '' !== $inner ) { $parts[] = $inner; } $path = self::path( $stack ); if ( 'labels' === end( $path ) ) { $parts[] = 'badge'; } elseif ( '' === $inner || ! in_array( $key, array( 'label', 'header_tip' ), true ) ) { $parts[] = $key; } return implode( ':', $parts ); } private static function path( array $stack ) { $p = array(); foreach ( $stack as $f ) { if ( $f['array'] && null !== $f['key'] ) { $p[] = (string) $f['key']; } } return $p; } private static function literal( $src ) { $q = $src[0]; $body = substr( $src, 1, -1 ); if ( "'" === $q ) { return str_replace( array( '\\\\', "\\'" ), array( '\\', "'" ), $body ); } return stripcslashes( $body ); } public static function render( array $strings ) { $out = array(); $out[] = ' $wheres ) { $out[] = '// ' . implode( ', ', $wheres ); if ( preg_match_all( '/%(?:(\d+)\$)?[sdfu]/', (string) $s, $m ) ) { $ph = array_values( array_unique( $m[0] ) ); $out[] = '/* translators: ' . implode( ', ', $ph ) . ': ' . ( 1 === count( $ph ) ? 'the value' : 'the values' ) . ' the report fills in at render (' . implode( ', ', $wheres ) . '). */'; } $out[] = '__( ' . var_export( (string) $s, true ) . ", 'ai-eshop-optimizer' );"; } $out[] = ''; return implode( "\n", $out ); } }