Skip to content

Commit b4cee22

Browse files
committed
used native PHP 8 functions
1 parent da090cb commit b4cee22

10 files changed

+19
-27
lines changed

src/Texy/HtmlElement.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ final public function validateAttrs(array $dtd): void
479479
if (
480480
!isset($allowed[$attr])
481481
&& (!isset($allowed['data-*'])
482-
|| substr((string) $attr, 0, 5) !== 'data-')
482+
|| !str_starts_with((string) $attr, 'data-'))
483483
) {
484484
unset($this->attrs[$attr]);
485485
}

src/Texy/Modifier.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private function parseStyle(string $s): void
226226

227227
$value = trim($pair[1]);
228228

229-
if (isset(self::$elAttrs[$prop]) || substr($prop, 0, 5) === 'data-') { // attribute
229+
if (isset(self::$elAttrs[$prop]) || str_starts_with($prop, 'data-')) { // attribute
230230
$this->attrs[$prop] = $value;
231231
} elseif ($value !== '') { // style
232232
$this->styles[$prop] = $value;

src/Texy/Modules/EmoticonModule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function solve(Texy\HandlerInvocation $invocation, string $emoticon, stri
9797
{
9898
$texy = $this->texy;
9999
$file = $this->icons[$emoticon];
100-
if (strpos($file, '.') === false) {
100+
if (!str_contains($file, '.')) {
101101
return $file;
102102
}
103103

src/Texy/Modules/HtmlModule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function patternTag(Texy\LineParser $parser, array $matches): HtmlElement
6767

6868
$isStart = $mEnd !== '/';
6969
$isEmpty = $mEmpty === '/';
70-
if (!$isEmpty && substr($mAttr, -1) === '/') { // uvizlo v $mAttr?
70+
if (!$isEmpty && str_ends_with($mAttr, '/')) { // uvizlo v $mAttr?
7171
$mAttr = substr($mAttr, 0, -1);
7272
$isEmpty = true;
7373
}
@@ -258,7 +258,7 @@ private function validateAttrs(HtmlElement $el, Texy\Texy $texy): bool
258258
}
259259

260260
if (isset($el->attrs['href'])) {
261-
if ($texy->linkModule->forceNoFollow && strpos($el->attrs['href'], '//') !== false) {
261+
if ($texy->linkModule->forceNoFollow && str_contains($el->attrs['href'], '//')) {
262262
if (isset($el->attrs['rel'])) {
263263
$el->attrs['rel'] = (array) $el->attrs['rel'];
264264
}

src/Texy/Modules/ImageModule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function solve(
252252
private function detectDimensions(Image $image): void
253253
{
254254
// absolute URL & security check for double dot
255-
if (!Helpers::isRelative($image->URL) || strpos($image->URL, '..') !== false) {
255+
if (!Helpers::isRelative($image->URL) || str_contains($image->URL, '..')) {
256256
return;
257257
}
258258

src/Texy/Modules/LinkModule.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public function factoryLink(string $dest, ?string $mMod, ?string $label): Link
242242
$this->checkLink($link);
243243
}
244244

245-
if (strpos((string) $link->URL, '%s') !== false) {
245+
if (str_contains((string) $link->URL, '%s')) {
246246
$link->URL = str_replace('%s', urlencode($texy->stringToText($label)), $link->URL);
247247
}
248248

@@ -288,7 +288,7 @@ public function solve(
288288
$el->attrs['href'] = Texy\Helpers::prependRoot($link->URL, $this->root);
289289

290290
// rel="nofollow"
291-
if ($nofollow || ($this->forceNoFollow && strpos($el->attrs['href'], '//') !== false)) {
291+
if ($nofollow || ($this->forceNoFollow && str_contains($el->attrs['href'], '//'))) {
292292
$el->attrs['rel'] = 'nofollow';
293293
}
294294
}

src/Texy/Modules/ParagraphModule.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ public function solve(
8282

8383
// check content type
8484
// block contains block tag
85-
if (strpos($content, $texy::CONTENT_BLOCK) !== false) {
85+
if (str_contains($content, $texy::CONTENT_BLOCK)) {
8686
$el->setName(null); // ignores modifier!
8787

8888
// block contains text (protected)
89-
} elseif (strpos($content, $texy::CONTENT_TEXTUAL) !== false) {
89+
} elseif (str_contains($content, $texy::CONTENT_TEXTUAL)) {
9090
// leave element p
9191

9292
// block contains text
9393
} elseif (preg_match('#[^\s' . Texy\Patterns::MARK . ']#u', $content)) {
9494
// leave element p
9595

9696
// block contains only replaced element
97-
} elseif (strpos($content, $texy::CONTENT_REPLACED) !== false) {
97+
} elseif (str_contains($content, $texy::CONTENT_REPLACED)) {
9898
if ($texy->nontextParagraph instanceof Texy\HtmlElement) {
9999
$el = (clone $texy->nontextParagraph)->setText($content);
100100
} else {
@@ -116,7 +116,7 @@ public function solve(
116116
}
117117

118118
// add <br>
119-
if (strpos($content, "\r") !== false) {
119+
if (str_contains($content, "\r")) {
120120
$key = $texy->protect('<br>', $texy::CONTENT_REPLACED);
121121
$content = str_replace("\r", $key, $content);
122122
}

src/Texy/Modules/TableModule.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private function finishPart(HtmlElement $elPart): void
271271
}
272272

273273
$text = rtrim((string) $elCell->text);
274-
if (strpos($text, "\n") !== false) {
274+
if (str_contains($text, "\n")) {
275275
// multiline parse as block
276276
// HACK: disable tables
277277
$this->disableTables = true;

src/Texy/Regexp.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ class Regexp
1717
public const ALL = 1;
1818
public const OFFSET_CAPTURE = 2;
1919

20-
private static array $messages = [
21-
PREG_INTERNAL_ERROR => 'Internal error',
22-
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
23-
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
24-
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
25-
5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
26-
];
27-
2820

2921
/**
3022
* Splits string by a regular expression.
@@ -35,7 +27,7 @@ public static function split(string $subject, string $pattern, int $flags = 0):
3527
$reFlags = (($flags & self::OFFSET_CAPTURE) ? PREG_SPLIT_OFFSET_CAPTURE : 0) | PREG_SPLIT_DELIM_CAPTURE;
3628
$res = preg_split($pattern, $subject, -1, $reFlags);
3729
if (preg_last_error()) { // run-time error
38-
trigger_error(@self::$messages[preg_last_error()], E_USER_WARNING);
30+
trigger_error(preg_last_error_msg(), E_USER_WARNING);
3931
}
4032

4133
return $res;
@@ -58,7 +50,7 @@ public static function match(string $subject, string $pattern, int $flags = 0, i
5850
? preg_match_all($pattern, $subject, $m, $reFlags | PREG_SET_ORDER, $offset)
5951
: preg_match($pattern, $subject, $m, $reFlags, $offset);
6052
if (preg_last_error()) { // run-time error
61-
trigger_error(@self::$messages[preg_last_error()], E_USER_WARNING);
53+
trigger_error(preg_last_error_msg(), E_USER_WARNING);
6254
} elseif ($res) {
6355
return $m;
6456
}
@@ -79,7 +71,7 @@ public static function replace(
7971
if (is_object($replacement) || is_array($replacement)) {
8072
$res = preg_replace_callback($pattern, $replacement, $subject);
8173
if ($res === null && preg_last_error()) { // run-time error
82-
trigger_error(@self::$messages[preg_last_error()], E_USER_WARNING);
74+
trigger_error(preg_last_error_msg(), E_USER_WARNING);
8375
}
8476

8577
return $res;
@@ -91,7 +83,7 @@ public static function replace(
9183

9284
$res = preg_replace($pattern, $replacement, $subject);
9385
if (preg_last_error()) { // run-time error
94-
trigger_error(@self::$messages[preg_last_error()], E_USER_WARNING);
86+
trigger_error(preg_last_error_msg(), E_USER_WARNING);
9587
}
9688

9789
return $res;

src/Texy/Texy.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function __construct()
187187
if (
188188
extension_loaded('mbstring')
189189
&& mb_get_info('func_overload') & 2
190-
&& substr(mb_get_info('internal_encoding'), 0, 1) === 'U'
190+
&& str_starts_with(mb_get_info('internal_encoding'), 'U')
191191
) {
192192
mb_internal_encoding('pass');
193193
trigger_error("Texy: mb_internal_encoding changed to 'pass'", E_USER_WARNING);
@@ -344,7 +344,7 @@ public function process(string $text, bool $singleLine = false): string
344344

345345
// replace tabs with spaces
346346
if ($this->tabWidth) {
347-
while (strpos($text, "\t") !== false) {
347+
while (str_contains($text, "\t")) {
348348
$text = Regexp::replace(
349349
$text,
350350
'#^([^\t\n]*+)\t#mU',

0 commit comments

Comments
 (0)