| Total Complexity | 40 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Str often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Str, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Str |
||
| 19 | { |
||
| 20 | use \Aimeos\Macro\Macroable; |
||
| 21 | |||
| 22 | |||
| 23 | private static $node; |
||
| 24 | private static $seq = 0; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns the sub-string after the given needle. |
||
| 29 | * |
||
| 30 | * @param mixed $str Stringable value |
||
| 31 | * @param mixed $needles String or strings to search for |
||
| 32 | * @return string|null Sub-string after the needle or NULL if needle is not part of the string |
||
| 33 | */ |
||
| 34 | public static function after( $str, $needles ) : ?string |
||
| 50 | } |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns the sub-string before the given needle. |
||
| 55 | * |
||
| 56 | * @param mixed $str Stringable value |
||
| 57 | * @param mixed $needles String or strings to search for |
||
| 58 | * @return string|null Sub-string before the needle or NULL if needle is not part of the string |
||
| 59 | */ |
||
| 60 | public static function before( $str, $needles ) : ?string |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * Replaces special HTML characters by their entities. |
||
| 81 | * |
||
| 82 | * @param mixed $str Stringable value |
||
| 83 | * @param int $flags Which characters to encode |
||
| 84 | * @return string String which isn't interpreted as HTML and save to include in HTML documents |
||
| 85 | */ |
||
| 86 | public static function decode( $str, int $flags = ENT_SUBSTITUTE | ENT_QUOTES | ENT_HTML5 ) : string |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Tests if the strings ends with the needle. |
||
| 94 | * |
||
| 95 | * @param mixed $str Stringable value |
||
| 96 | * @param array|string|null $needles String/character or list thereof to compare with |
||
| 97 | * @return bool TRUE if string ends with needle, FALSE if not |
||
| 98 | */ |
||
| 99 | public static function ends( $str, $needles ) : bool |
||
| 100 | { |
||
| 101 | $str = (string) $str; |
||
| 102 | |||
| 103 | if( $str === '' || $needles == null ) { |
||
|
|
|||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | foreach( (array) $needles as $needle ) |
||
| 108 | { |
||
| 109 | $needle = (string) $needle; |
||
| 110 | |||
| 111 | if( $needle !== '' && substr_compare( $str, $needle, -strlen( $needle ) ) === 0 ) { |
||
| 112 | return true; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * Replaces special HTML characters by their entities. |
||
| 122 | * |
||
| 123 | * @param mixed $str Stringable value |
||
| 124 | * @param int $flags Which characters to encode |
||
| 125 | * @return string String which isn't interpreted as HTML and save to include in HTML documents |
||
| 126 | */ |
||
| 127 | public static function html( $str, int $flags = ENT_SUBSTITUTE | ENT_QUOTES | ENT_HTML5 ) : string |
||
| 128 | { |
||
| 129 | return htmlspecialchars( (string) $str, $flags, 'UTF-8' ); |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Tests if the strings contains all of the needles. |
||
| 135 | * |
||
| 136 | * @param mixed $str Stringable value |
||
| 137 | * @param string|array|null $needles String/character or list thereof to search for |
||
| 138 | * @return bool TRUE if string contains all needles, FALSE if not |
||
| 139 | */ |
||
| 140 | public static function in( $str, $needles ) : bool |
||
| 141 | { |
||
| 142 | $str = (string) $str; |
||
| 143 | |||
| 144 | if( $needles == null ) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | |||
| 148 | foreach( (array) $needles as $needle ) |
||
| 149 | { |
||
| 150 | $needle = (string) $needle; |
||
| 151 | |||
| 152 | if( $needle === '' || strpos( $str, $needle ) === false ) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Transforms the string into a suitable URL segment. |
||
| 163 | * |
||
| 164 | * @param mixed $str Stringable value |
||
| 165 | * @param string $lang Two letter ISO language code |
||
| 166 | * @param string $sep Separator between the words |
||
| 167 | * @return string String suitable for an URL segment |
||
| 168 | */ |
||
| 169 | public static function slug( $str, string $lang = 'en', string $sep = '-' ) : string |
||
| 170 | { |
||
| 171 | if( $fcn = static::macro( 'slug' ) ) { |
||
| 172 | return $fcn( $str, $lang, $sep ); |
||
| 173 | } |
||
| 174 | |||
| 175 | $str = \voku\helper\ASCII::to_ascii( (string) $str, $lang ); |
||
| 176 | $str = strtolower( preg_replace( '/[^A-Za-z0-9\_\-\~\.]+/', $sep, $str ) ); |
||
| 177 | return trim( preg_replace( '/(' . preg_quote( $sep, '/' ) . '){2,}/', $sep, $str ), $sep ); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Tests if the strings contains at least one of the needles. |
||
| 183 | * |
||
| 184 | * @param mixed $str Stringable value |
||
| 185 | * @param array $needles Strings or characters to search for |
||
| 186 | * @return bool TRUE if string contains at least one needle, FALSE if it contains none |
||
| 187 | */ |
||
| 188 | public static function some( $str, array $needles ) : bool |
||
| 202 | } |
||
| 203 | |||
| 204 | |||
| 205 | /** |
||
| 206 | * Tests if the strings starts with the needle. |
||
| 207 | * |
||
| 208 | * @param mixed $str Stringable value |
||
| 209 | * @param array|string|null $needles String/character or list thereof to compare with |
||
| 210 | * @return bool TRUE if string starts with needle, FALSE if not |
||
| 211 | */ |
||
| 212 | public static function starts( $str, $needles ) : bool |
||
| 230 | } |
||
| 231 | |||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns a date formatted according to the given format string. |
||
| 235 | * |
||
| 236 | * @param string $format DateTime format string but with format characters preceded by "%" |
||
| 237 | * @return string String with date/time according to the format string |
||
| 238 | */ |
||
| 239 | public static function strtime( string $format ) : string |
||
| 240 | { |
||
| 241 | $format = str_replace( ['%M', '%S'], ['%i', '%s'], $format ); |
||
| 242 | return trim( date_create()->format( str_replace( '\\%\\', '', '\\' . join( '\\', str_split( $format ) ) ) ) ); |
||
| 243 | } |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Generates a unique ID string suitable as global identifier |
||
| 248 | * |
||
| 249 | * The ID is similar to an UUID and is as unique as an UUID but it's human |
||
| 250 | * readable string is only 20 bytes long. Additionally, the unique ID is |
||
| 251 | * optimized for being used as primary key in databases. |
||
| 252 | * |
||
| 253 | * @return string Global unique ID of 20 bytes length |
||
| 254 | */ |
||
| 255 | public static function uid() : string |
||
| 286 | } |
||
| 287 | } |
||
| 288 |