mirror of
https://github.com/erusev/parsedown.git
synced 2024-09-20 03:51:29 +02:00
Utilise constant arrays over static vars
This commit is contained in:
parent
ea55a9ffb0
commit
37f306c3a8
@ -21,14 +21,12 @@ final class Emphasis implements Inline
|
|||||||
/** @var 'em'|'strong' */
|
/** @var 'em'|'strong' */
|
||||||
private $type;
|
private $type;
|
||||||
|
|
||||||
/** @var array{*: string, _: string} */
|
private const STRONG_REGEX = [
|
||||||
private static $STRONG_REGEX = [
|
|
||||||
'*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s',
|
'*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*+[*])+?)[*]{2}(?![*])/s',
|
||||||
'_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us',
|
'_' => '/^__((?:\\\\_|[^_]|_[^_]*+_)+?)__(?!_)/us',
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var array{*: string, _: string} */
|
private const EM_REGEX = [
|
||||||
private static $EM_REGEX = [
|
|
||||||
'*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
|
'*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s',
|
||||||
'_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
|
'_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us',
|
||||||
];
|
];
|
||||||
@ -58,9 +56,9 @@ final class Emphasis implements Inline
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\preg_match(self::$STRONG_REGEX[$marker], $Excerpt->text(), $matches)) {
|
if (\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;
|
||||||
|
@ -17,8 +17,7 @@ use Erusev\Parsedown\Configurable;
|
|||||||
|
|
||||||
final class BlockTypes implements Configurable
|
final class BlockTypes implements Configurable
|
||||||
{
|
{
|
||||||
/** @var array<array-key, array<int, class-string<Block>>> */
|
private const DEFAULT_BLOCK_TYPES = [
|
||||||
private static $defaultBlockTypes = [
|
|
||||||
'#' => [Header::class],
|
'#' => [Header::class],
|
||||||
'*' => [Rule::class, TList::class],
|
'*' => [Rule::class, TList::class],
|
||||||
'+' => [TList::class],
|
'+' => [TList::class],
|
||||||
@ -44,8 +43,7 @@ final class BlockTypes implements Configurable
|
|||||||
'~' => [FencedCode::class],
|
'~' => [FencedCode::class],
|
||||||
];
|
];
|
||||||
|
|
||||||
/** @var array<int, class-string<Block>> */
|
private const DEFAULT_UNMARKED_BLOCK_TYPES = [
|
||||||
private static $defaultUnmarkedBlockTypes = [
|
|
||||||
IndentedCode::class,
|
IndentedCode::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -69,8 +67,8 @@ final class BlockTypes implements Configurable
|
|||||||
public static function initial()
|
public static function initial()
|
||||||
{
|
{
|
||||||
return new self(
|
return new self(
|
||||||
self::$defaultBlockTypes,
|
self::DEFAULT_BLOCK_TYPES,
|
||||||
self::$defaultUnmarkedBlockTypes
|
self::DEFAULT_UNMARKED_BLOCK_TYPES
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,8 +20,7 @@ use Erusev\Parsedown\Configurable;
|
|||||||
|
|
||||||
final class InlineTypes implements Configurable
|
final class InlineTypes implements Configurable
|
||||||
{
|
{
|
||||||
/** @var array<array-key, array<int, class-string<Inline>>> */
|
private const DEFAULT_INLINE_TYPES = [
|
||||||
private static $defaultInlineTypes = [
|
|
||||||
'!' => [Image::class],
|
'!' => [Image::class],
|
||||||
'*' => [Emphasis::class],
|
'*' => [Emphasis::class],
|
||||||
'_' => [Emphasis::class],
|
'_' => [Emphasis::class],
|
||||||
@ -53,7 +52,7 @@ final class InlineTypes implements Configurable
|
|||||||
/** @return self */
|
/** @return self */
|
||||||
public static function initial()
|
public static function initial()
|
||||||
{
|
{
|
||||||
return new self(self::$defaultInlineTypes);
|
return new self(self::DEFAULT_INLINE_TYPES);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,8 +10,7 @@ final class Element implements Renderable
|
|||||||
{
|
{
|
||||||
use CanonicalStateRenderable;
|
use CanonicalStateRenderable;
|
||||||
|
|
||||||
/** @var array<string, true> */
|
const TEXT_LEVEL_ELEMENTS = [
|
||||||
public static $TEXT_LEVEL_ELEMENTS = [
|
|
||||||
'a' => true,
|
'a' => true,
|
||||||
'b' => true,
|
'b' => true,
|
||||||
'i' => true,
|
'i' => true,
|
||||||
@ -167,7 +166,7 @@ final class Element implements Renderable
|
|||||||
foreach ($this->Contents as $C) {
|
foreach ($this->Contents as $C) {
|
||||||
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";
|
||||||
}
|
}
|
||||||
@ -179,7 +178,7 @@ final class Element implements Renderable
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
$Last instanceof Element
|
$Last instanceof Element
|
||||||
&& ! \array_key_exists(\strtolower($Last->name()), self::$TEXT_LEVEL_ELEMENTS)
|
&& ! \array_key_exists(\strtolower($Last->name()), self::TEXT_LEVEL_ELEMENTS)
|
||||||
) {
|
) {
|
||||||
$html .= "\n";
|
$html .= "\n";
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,7 @@ namespace Erusev\Parsedown\Html\Sanitisation;
|
|||||||
|
|
||||||
final class UrlSanitiser
|
final class UrlSanitiser
|
||||||
{
|
{
|
||||||
/** @var string[] */
|
private const COMMON_SCHEMES = [
|
||||||
private static $COMMON_SCHEMES = [
|
|
||||||
'http://',
|
'http://',
|
||||||
'https://',
|
'https://',
|
||||||
'ftp://',
|
'ftp://',
|
||||||
@ -33,7 +32,7 @@ final class UrlSanitiser
|
|||||||
public static function filter($url, $permittedSchemes = null)
|
public static function filter($url, $permittedSchemes = null)
|
||||||
{
|
{
|
||||||
if (! isset($permittedSchemes)) {
|
if (! isset($permittedSchemes)) {
|
||||||
$permittedSchemes = self::$COMMON_SCHEMES;
|
$permittedSchemes = self::COMMON_SCHEMES;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($permittedSchemes as $scheme) {
|
foreach ($permittedSchemes as $scheme) {
|
||||||
|
@ -28,7 +28,7 @@ class CommonMarkTestWeak extends CommonMarkTestStrict
|
|||||||
*/
|
*/
|
||||||
public function __construct($name = null, array $data = [], $dataName = '')
|
public function __construct($name = null, array $data = [], $dataName = '')
|
||||||
{
|
{
|
||||||
$textLevelElements = \array_keys(Element::$TEXT_LEVEL_ELEMENTS);
|
$textLevelElements = \array_keys(Element::TEXT_LEVEL_ELEMENTS);
|
||||||
|
|
||||||
\array_walk(
|
\array_walk(
|
||||||
$textLevelElements,
|
$textLevelElements,
|
||||||
|
Loading…
Reference in New Issue
Block a user