Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 12 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
MhchemUtil | |
0.00% |
0 / 12 |
|
0.00% |
0 / 4 |
156 | |
0.00% |
0 / 1 |
issetJS | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
isRegex | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isAssoc | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
concatArray | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | /** |
3 | * Copyright (c) 2023 Johannes Stegmüller |
4 | * |
5 | * This file is a port of mhchemParser originally authored by Martin Hensel in javascript/typescript. |
6 | * The original license for this software can be found in the accompanying LICENSE.mhchemParser-ts.txt file. |
7 | */ |
8 | |
9 | namespace MediaWiki\Extension\Math\WikiTexVC\Mhchem; |
10 | |
11 | /** |
12 | * Some utility classes mostly for creating similar functionalities |
13 | * like in javascript in PHP. |
14 | * |
15 | * concatArray method here has the same functionality as concatArray (~l.194) |
16 | * in mhchemParser.js by Martin Hensel. |
17 | * |
18 | * @author Johannes Stegmüller |
19 | * @license GPL-2.0-or-later |
20 | */ |
21 | class MhchemUtil { |
22 | |
23 | /** |
24 | * The input is used as boolean operator in a javascript-type if condition, |
25 | * example: "if(input)" |
26 | * output has the same boolean results as an if-condition in javascript. |
27 | * arrays as input have to be used like this "issetJS($arr["b"] ?? null);" |
28 | * properties as input have to be used like this "issetJS($inst->prop ?? null);" |
29 | * @param mixed|null $input input to be checked in a javascript-type if condition |
30 | * @return bool indicator if input is populated |
31 | */ |
32 | public static function issetJS( $input ): bool { |
33 | if ( $input === 0 || $input == "" ) { |
34 | return false; |
35 | } |
36 | return true; |
37 | } |
38 | |
39 | /** |
40 | * Checks if the incoming string is containing a regex pattern. |
41 | * @param string $input string to verify |
42 | * @param string $subject subject to check, usually empty string |
43 | * @return bool true if regex pattern, false if not |
44 | */ |
45 | public static function isRegex( string $input, string $subject = "" ): bool { |
46 | /** |
47 | * Ignoring preg_match phpcs error here, since this is the fastest variant: 582 ms for MMLmhchemTestLocal, |
48 | * 835 ms for the try catch version of this, 735 ms for the version deactivating error handler |
49 | * during function call. |
50 | * See: https://stackoverflow.com/questions/16039362/how-can-i-suppress-phpcs-warnings-using-comments |
51 | */ |
52 | |
53 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
54 | return !( @preg_match( $input, $subject ) === false ); |
55 | } |
56 | |
57 | /** |
58 | * Checks if an array is an associative array |
59 | * @param array $array to check |
60 | * @return bool true if associative, otherwise false |
61 | */ |
62 | public static function isAssoc( array $array ): bool { |
63 | return ( $array !== array_values( $array ) ); |
64 | } |
65 | |
66 | /** |
67 | * @param array &$a |
68 | * @param mixed|null $b |
69 | */ |
70 | public static function concatArray( &$a, $b ) { |
71 | if ( self::issetJS( $b ) ) { |
72 | if ( is_array( $b ) && ( self::isAssoc( $b ) ) ) { |
73 | $a[] = $b; |
74 | } elseif ( is_array( $b ) && !self::isAssoc( $b ) ) { |
75 | foreach ( $b as $value ) { |
76 | $a[] = $value; |
77 | } |
78 | } else { |
79 | $a[] = $b; |
80 | } |
81 | } |
82 | } |
83 | } |