Lines 70.00% 21 / 30
Methods 85.71% 12 / 14
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getStringBasic 85.71% 6 / 7 0.00% 0 / 1 3.03
 strlen 100.00% 1 / 1 100.00% 1 / 1 1
 substr 100.00% 2 / 2 100.00% 1 / 1 2
 strpos 100.00% 1 / 1 100.00% 1 / 1 1
 strtolower 100.00% 1 / 1 100.00% 1 / 1 1
 strtoupper 100.00% 1 / 1 100.00% 1 / 1 1
 strstr 100.00% 1 / 1 100.00% 1 / 1 1
 preg_match 100.00% 1 / 1 100.00% 1 / 1 1
 preg_match_all 100.00% 1 / 1 100.00% 1 / 1 1
 preg_split 100.00% 1 / 1 100.00% 1 / 1 1
 preg_replace_callback 100.00% 1 / 1 100.00% 1 / 1 1
 preg_replace 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Strings\AbstractString] isUTF8 20.00% 2 / 10 0.00% 0 / 1 4.05
29class StringBasic extends AbstractString
30{
31    /**
32     * Default encoding.
33     */
34    final public const string DEFAULT_ENCODING = 'UTF-8';
35
36    /**
37     * Instance.
38     */
39    private static ?StringBasic $stringBasic = null;
40
41    /**
42     * Constructor.
43     */
44    final private function __construct()
45    {
46    }
47
48    /**
49     * Create and return an instance.
50     */
51    public static function getStringBasic(string $language = 'en'): StringBasic
52    {
53        if (!self::$stringBasic instanceof StringBasic) {
54            self::$stringBasic = new self();
55            self::$stringBasic->encoding = self::DEFAULT_ENCODING;
56            self::$stringBasic->language = Language::isASupportedLanguage($language)
57                ? $language
58                : self::DEFAULT_LANGUAGE;
59        }
60
61        return self::$stringBasic;
62    }
63
64    /**
65     * Get string character count.
66     *
67     * @param string $str String
68     */
69    public static function strlen(string $str): int
70    {
71        return strlen($str);
72    }
73
74    /**
75     * Get a part of string.
76     *
77     * @param string   $str String
78     * @param int      $start Start
79     * @param int|null $length Length
80     */
81    public function substr(string $str, int $start, ?int $length = null): string
82    {
83        $length = null === $length ? strlen($str) : $length;
84
85        return substr($str, $start, $length);
86    }
87
88    /**
89     * Get position of the first occurrence of a string.
90     *
91     * @param string $haystack Haystack
92     * @param string $needle   Needle
93     * @param int    $offset   Offset
94     */
95    public function strpos(string $haystack, string $needle, int $offset = 0): int|false
96    {
97        return strpos($haystack, $needle, $offset);
98    }
99
100    /**
101     * Make a string lower case.
102     *
103     * @param string $str String
104     */
105    public function strtolower(string $str): string
106    {
107        return strtolower($str);
108    }
109
110    /**
111     * Make a string upper case.
112     *
113     * @param string $str String
114     */
115    public function strtoupper(string $str): string
116    {
117        return strtoupper($str);
118    }
119
120    /**
121     * Get occurrence of a string within another.
122     *
123     * @param string $haystack Haystack
124     * @param string $needle Needle
125     * @param bool   $part Part
126     */
127    public function strstr(string $haystack, string $needle, bool $part = false): string|false
128    {
129        return strstr($haystack, $needle, $part);
130    }
131
132    /**
133     * Match a regexp.
134     *
135     * @return int|false
136     */
137    /**
138     * @param array<array-key, mixed>|null $matches
139     */
140    public function preg_match(
141        string $pattern,
142        string $subject,
143        &$matches = null,
144        int $flags = 0,
145        int $offset = 0,
146    ): int|false { /* phpcs:ignore */
147        return preg_match($pattern, $subject, $matches, $flags, $offset);
148    }
149
150    /**
151     * Match a regexp globally.
152     *
153     * @param array<array-key, mixed>|null $matches
154     * @return int|false
155     */
156    /**
157     * @param array<array-key, mixed>|null $matches
158     */
159    public function preg_match_all(
160        string $pattern,
161        string $subject,
162        &$matches = null,
163        int $flags = 0,
164        int $offset = 0,
165    ): int|false { /* phpcs:ignore */
166        return preg_match_all($pattern, $subject, $matches, $flags, $offset);
167    }
168
169    /**
170     * Split string by a regexp.
171     */
172    public function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|bool
173    {
174        return preg_split($pattern, $subject, $limit, $flags);
175    }
176
177    /**
178     * Search and replace by a regexp using a callback.
179     *
180     * @param string|string[] $pattern
181     * @param callable(array<array-key, string>): string $callback
182     * @param string|string[] $subject
183     * @return string|string[]|null
184     */
185    public function preg_replace_callback(
186        string|array $pattern,
187        callable $callback,
188        string|array $subject,
189        int $limit = -1,
190        int &$count = 0,
191    ): string|array|null {
192        return preg_replace_callback($pattern, $callback, $subject, $limit, $count);
193    }
194
195    /**
196     * Search and replace by a regexp.
197     *
198     * @param string|string[] $pattern
199     * @param string|string[] $replacement
200     * @param string|string[] $subject
201     * @return string|string[]|null
202     */
203    public function preg_replace(
204        string|array $pattern,
205        string|array $replacement,
206        string|array $subject,
207        int $limit = -1,
208        int &$count = 0,
209    ): string|array|null {
210        return preg_replace($pattern, $replacement, $subject, $limit, $count);
211    }
212}

Inherited from phpMyFAQ\Strings\AbstractString

56    public static function isUTF8(string $str): bool
57    {
58        if (function_exists('mb_detect_encoding')) {
59            return (bool) mb_detect_encoding($str, self::DEFAULT_ENCODING, strict: true);
60        }
61
62        $regex =
63            '/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf]{2}|'
64            . '\xed[\x80-\x9f][\x80-\xbf]|'
65            . '[\xee-\xef][\x80-\xbf]{2}|'
66            . '\xf0[\x90-\xbf][\x80-\xbf]{2}|'
67            . '[\xf1-\xf3][\x80-\xbf]{3}|'
68            . '\xf4[\x80-\x8f][\x80-\xbf]{2})*$/';
69
70        return preg_match($regex, $str) === 1;
71    }