1
0
mirror of https://github.com/erusev/parsedown.git synced 2023-08-10 21:13:06 +03:00

Constant arrays to static vars for PHP 5.5

This commit is contained in:
Aidan Woods
2019-01-20 19:06:46 +00:00
parent 37895448ba
commit 2757274854
4 changed files with 20 additions and 12 deletions

View File

@ -45,7 +45,7 @@ final class Markup implements ContinuableBlock
if (\preg_match('/^<[\/]?+(\w*)(?:[ ]*+'.self::REGEX_HTML_ATTRIBUTE.')*+[ ]*+(\/)?>/', $Context->line()->text(), $matches)) { if (\preg_match('/^<[\/]?+(\w*)(?:[ ]*+'.self::REGEX_HTML_ATTRIBUTE.')*+[ ]*+(\/)?>/', $Context->line()->text(), $matches)) {
$element = \strtolower($matches[1]); $element = \strtolower($matches[1]);
if (\array_key_exists($element, Element::TEXT_LEVEL_ELEMENTS)) { if (\array_key_exists($element, Element::$TEXT_LEVEL_ELEMENTS)) {
return null; return null;
} }

View File

@ -21,12 +21,14 @@ final class Emphasis implements Inline
/** @var 'em'|'strong' */ /** @var 'em'|'strong' */
private $type; private $type;
const STRONG_REGEX = [ /** @var array{*: string, _: string} */
private static $STRONG_REGEX = [
'*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s', '*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s',
'_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us', '_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us',
]; ];
const EM_REGEX = [ /** @var array{*: string, _: string} */
private static $EM_REGEX = [
'*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', '*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
'_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', '_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
]; ];
@ -56,9 +58,9 @@ final class Emphasis implements Inline
$marker = $Excerpt->text()[0] === '*' ? '*' : '_'; $marker = $Excerpt->text()[0] === '*' ? '*' : '_';
if ($Excerpt->text()[1] === $marker and \preg_match(self::STRONG_REGEX[$marker], $Excerpt->text(), $matches)) { if ($Excerpt->text()[1] === $marker and \preg_match(self::$STRONG_REGEX[$marker], $Excerpt->text(), $matches)) {
$emphasis = 'strong'; $emphasis = 'strong';
} elseif (\preg_match(self::EM_REGEX[$marker], $Excerpt->text(), $matches)) { } elseif (\preg_match(self::$EM_REGEX[$marker], $Excerpt->text(), $matches)) {
$emphasis = 'em'; $emphasis = 'em';
} else { } else {
return null; return null;

View File

@ -10,7 +10,8 @@ final class Element implements Renderable
{ {
use CanonicalStateRenderable; use CanonicalStateRenderable;
const TEXT_LEVEL_ELEMENTS = [ /** @var array<string, true> */
public static $TEXT_LEVEL_ELEMENTS = [
'a' => true, 'a' => true,
'b' => true, 'b' => true,
'i' => true, 'i' => true,
@ -61,7 +62,8 @@ final class Element implements Renderable
'basefont' => true, 'basefont' => true,
]; ];
const COMMON_SCHEMES = [ /** @var string[] */
public static $COMMON_SCHEMES = [
'http://', 'http://',
'https://', 'https://',
'ftp://', 'ftp://',
@ -197,7 +199,7 @@ final class Element implements Renderable
if ( if (
$First instanceof Element $First instanceof Element
&& ! \array_key_exists(\strtolower($First->name()), self::TEXT_LEVEL_ELEMENTS) && ! \array_key_exists(\strtolower($First->name()), self::$TEXT_LEVEL_ELEMENTS)
) { ) {
$html .= "\n"; $html .= "\n";
} }
@ -208,7 +210,7 @@ final class Element implements Renderable
if ( if (
$C instanceof Element $C instanceof Element
&& ! \array_key_exists(\strtolower($C->name()), self::TEXT_LEVEL_ELEMENTS) && ! \array_key_exists(\strtolower($C->name()), self::$TEXT_LEVEL_ELEMENTS)
) { ) {
$html .= "\n"; $html .= "\n";
} }
@ -225,11 +227,15 @@ final class Element implements Renderable
/** /**
* @param string $url * @param string $url
* @param string[] $permittedSchemes * @param string[]|null $permittedSchemes
* @return string * @return string
*/ */
public static function filterUnsafeUrl($url, $permittedSchemes = self::COMMON_SCHEMES) public static function filterUnsafeUrl($url, $permittedSchemes = null)
{ {
if (! isset($permittedSchemes)) {
$permittedSchemes = self::$COMMON_SCHEMES;
}
foreach ($permittedSchemes as $scheme) { foreach ($permittedSchemes as $scheme) {
if (self::striAtStart($url, $scheme)) { if (self::striAtStart($url, $scheme)) {
return $url; return $url;

View File

@ -24,7 +24,7 @@ class CommonMarkTestWeak extends CommonMarkTestStrict
{ {
parent::setUp(); parent::setUp();
$textLevelElements = \array_keys(Element::TEXT_LEVEL_ELEMENTS); $textLevelElements = \array_keys(Element::$TEXT_LEVEL_ELEMENTS);
\array_walk($textLevelElements, function (&$element) { \array_walk($textLevelElements, function (&$element) {
$element = \preg_quote($element, '/'); $element = \preg_quote($element, '/');