Skip to content

Commit

Permalink
used native PHP 8 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 5, 2023
1 parent c499d18 commit bf7bae4
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Texy/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ final public function validateAttrs(array $dtd): void
if (
!isset($allowed[$attr])
&& (!isset($allowed['data-*'])
|| substr((string) $attr, 0, 5) !== 'data-')
|| !str_starts_with((string) $attr, 'data-'))
) {
unset($this->attrs[$attr]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ private function parseStyle(string $s): void

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

if (isset(self::$elAttrs[$prop]) || substr($prop, 0, 5) === 'data-') { // attribute
if (isset(self::$elAttrs[$prop]) || str_starts_with($prop, 'data-')) { // attribute
$this->attrs[$prop] = $value;
} elseif ($value !== '') { // style
$this->styles[$prop] = $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modules/EmoticonModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function solve(Texy\HandlerInvocation $invocation, string $emoticon, stri
{
$texy = $this->texy;
$file = $this->icons[$emoticon];
if (strpos($file, '.') === false) {
if (!str_contains($file, '.')) {
return $file;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Texy/Modules/HtmlModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function patternTag(Texy\LineParser $parser, array $matches): HtmlElement

$isStart = $mEnd !== '/';
$isEmpty = $mEmpty === '/';
if (!$isEmpty && substr($mAttr, -1) === '/') { // uvizlo v $mAttr?
if (!$isEmpty && str_ends_with($mAttr, '/')) { // uvizlo v $mAttr?
$mAttr = substr($mAttr, 0, -1);
$isEmpty = true;
}
Expand Down Expand Up @@ -258,7 +258,7 @@ private function validateAttrs(HtmlElement $el, Texy\Texy $texy): bool
}

if (isset($el->attrs['href'])) {
if ($texy->linkModule->forceNoFollow && strpos($el->attrs['href'], '//') !== false) {
if ($texy->linkModule->forceNoFollow && str_contains($el->attrs['href'], '//')) {
if (isset($el->attrs['rel'])) {
$el->attrs['rel'] = (array) $el->attrs['rel'];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modules/ImageModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function solve(
private function detectDimensions(Image $image): void
{
// absolute URL & security check for double dot
if (!Helpers::isRelative($image->URL) || strpos($image->URL, '..') !== false) {
if (!Helpers::isRelative($image->URL) || str_contains($image->URL, '..')) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Texy/Modules/LinkModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function factoryLink(string $dest, ?string $mMod, ?string $label): Link
$this->checkLink($link);
}

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

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

// rel="nofollow"
if ($nofollow || ($this->forceNoFollow && strpos($el->attrs['href'], '//') !== false)) {
if ($nofollow || ($this->forceNoFollow && str_contains($el->attrs['href'], '//'))) {
$el->attrs['rel'] = 'nofollow';
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Texy/Modules/ParagraphModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ public function solve(

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

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

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

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

// add <br>
if (strpos($content, "\r") !== false) {
if (str_contains($content, "\r")) {
$key = $texy->protect('<br>', $texy::CONTENT_REPLACED);
$content = str_replace("\r", $key, $content);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Texy/Modules/TableModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private function finishPart(HtmlElement $elPart): void
}

$text = rtrim((string) $elCell->text);
if (strpos($text, "\n") !== false) {
if (str_contains($text, "\n")) {
// multiline parse as block
// HACK: disable tables
$this->disableTables = true;
Expand Down
16 changes: 4 additions & 12 deletions src/Texy/Regexp.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ class Regexp
public const ALL = 1;
public const OFFSET_CAPTURE = 2;

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


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

return $res;
Expand All @@ -58,7 +50,7 @@ public static function match(string $subject, string $pattern, int $flags = 0, i
? preg_match_all($pattern, $subject, $m, $reFlags | PREG_SET_ORDER, $offset)
: preg_match($pattern, $subject, $m, $reFlags, $offset);
if (preg_last_error()) { // run-time error
trigger_error(@self::$messages[preg_last_error()], E_USER_WARNING);
trigger_error(preg_last_error_msg(), E_USER_WARNING);
} elseif ($res) {
return $m;
}
Expand All @@ -79,7 +71,7 @@ public static function replace(
if (is_object($replacement) || is_array($replacement)) {
$res = preg_replace_callback($pattern, $replacement, $subject);
if ($res === null && preg_last_error()) { // run-time error
trigger_error(@self::$messages[preg_last_error()], E_USER_WARNING);
trigger_error(preg_last_error_msg(), E_USER_WARNING);
}

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

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

return $res;
Expand Down
4 changes: 2 additions & 2 deletions src/Texy/Texy.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public function __construct()
if (
extension_loaded('mbstring')
&& mb_get_info('func_overload') & 2
&& substr(mb_get_info('internal_encoding'), 0, 1) === 'U'
&& str_starts_with(mb_get_info('internal_encoding'), 'U')
) {
mb_internal_encoding('pass');
trigger_error("Texy: mb_internal_encoding changed to 'pass'", E_USER_WARNING);
Expand Down Expand Up @@ -344,7 +344,7 @@ public function process(string $text, bool $singleLine = false): string

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

0 comments on commit bf7bae4

Please sign in to comment.