Utilise constant arrays over static vars

This commit is contained in:
Aidan Woods 2019-07-25 00:45:53 +02:00
parent ea55a9ffb0
commit 37f306c3a8
No known key found for this signature in database
GPG Key ID: 9A6A8EFAA512BBB9
6 changed files with 16 additions and 23 deletions

View File

@ -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;

View File

@ -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
); );
} }

View File

@ -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);
} }
/** /**

View File

@ -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";
} }

View File

@ -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) {

View File

@ -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,