Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| EntitySchemaCleaner | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
| cleanupParameters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| cleanupArrayOfStrings | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| trimWhitespaceAndControlChars | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| cleanAliasGroups | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| filterEmptyStrings | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace EntitySchema\DataAccess; |
| 6 | |
| 7 | /** |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | class EntitySchemaCleaner { |
| 11 | |
| 12 | /** |
| 13 | * @param string[] &$labels |
| 14 | * @param string[] &$descriptions |
| 15 | * @param array<string,string[]> &$aliasGroups |
| 16 | * @param string &$schemaText |
| 17 | */ |
| 18 | public static function cleanupParameters( |
| 19 | array &$labels, |
| 20 | array &$descriptions, |
| 21 | array &$aliasGroups, |
| 22 | string &$schemaText |
| 23 | ): void { |
| 24 | $labels = self::cleanupArrayOfStrings( $labels ); |
| 25 | $descriptions = self::cleanupArrayOfStrings( $descriptions ); |
| 26 | $aliasGroups = self::cleanAliasGroups( $aliasGroups ); |
| 27 | $schemaText = self::trimWhitespaceAndControlChars( $schemaText ); |
| 28 | } |
| 29 | |
| 30 | public static function cleanupArrayOfStrings( array $arrayOfStrings ): array { |
| 31 | foreach ( $arrayOfStrings as &$string ) { |
| 32 | $string = self::trimWhitespaceAndControlChars( $string ); |
| 33 | } |
| 34 | $arrayOfStrings = self::filterEmptyStrings( $arrayOfStrings ); |
| 35 | ksort( $arrayOfStrings ); |
| 36 | return $arrayOfStrings; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string $string The string to trim |
| 41 | * |
| 42 | * @return string The trimmed string after applying the regex |
| 43 | */ |
| 44 | public static function trimWhitespaceAndControlChars( string $string ): string { |
| 45 | return preg_replace( '/^[\p{Z}\p{Cc}\p{Cf}]+|[\p{Z}\p{Cc}\p{Cf}]+$/u', '', $string ); |
| 46 | } |
| 47 | |
| 48 | private static function cleanAliasGroups( array $aliasGroups ): array { |
| 49 | foreach ( $aliasGroups as &$aliasGroup ) { |
| 50 | $aliasGroup = self::cleanupArrayOfStrings( $aliasGroup ); |
| 51 | } |
| 52 | unset( $aliasGroup ); |
| 53 | foreach ( $aliasGroups as $languageCode => &$aliasGroup ) { |
| 54 | $aliasGroup = array_values( array_unique( $aliasGroup ) ); |
| 55 | if ( $aliasGroup === [] ) { |
| 56 | unset( $aliasGroups[$languageCode] ); |
| 57 | } |
| 58 | } |
| 59 | ksort( $aliasGroups ); |
| 60 | return $aliasGroups; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Remove keys with empty values/strings from the array |
| 65 | * |
| 66 | * Note: This does not renumber numeric keys! |
| 67 | * |
| 68 | * @param string[] $array |
| 69 | * |
| 70 | * @return string[] |
| 71 | */ |
| 72 | private static function filterEmptyStrings( array $array ): array { |
| 73 | foreach ( $array as $key => $value ) { |
| 74 | if ( $value === '' ) { |
| 75 | unset( $array[$key] ); |
| 76 | } |
| 77 | } |
| 78 | return $array; |
| 79 | } |
| 80 | |
| 81 | } |