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

View File

@ -17,8 +17,7 @@ use Erusev\Parsedown\Configurable;
final class BlockTypes implements Configurable
{
/** @var array<array-key, array<int, class-string<Block>>> */
private static $defaultBlockTypes = [
private const DEFAULT_BLOCK_TYPES = [
'#' => [Header::class],
'*' => [Rule::class, TList::class],
'+' => [TList::class],
@ -44,8 +43,7 @@ final class BlockTypes implements Configurable
'~' => [FencedCode::class],
];
/** @var array<int, class-string<Block>> */
private static $defaultUnmarkedBlockTypes = [
private const DEFAULT_UNMARKED_BLOCK_TYPES = [
IndentedCode::class,
];
@ -69,8 +67,8 @@ final class BlockTypes implements Configurable
public static function initial()
{
return new self(
self::$defaultBlockTypes,
self::$defaultUnmarkedBlockTypes
self::DEFAULT_BLOCK_TYPES,
self::DEFAULT_UNMARKED_BLOCK_TYPES
);
}

View File

@ -20,8 +20,7 @@ use Erusev\Parsedown\Configurable;
final class InlineTypes implements Configurable
{
/** @var array<array-key, array<int, class-string<Inline>>> */
private static $defaultInlineTypes = [
private const DEFAULT_INLINE_TYPES = [
'!' => [Image::class],
'*' => [Emphasis::class],
'_' => [Emphasis::class],
@ -53,7 +52,7 @@ final class InlineTypes implements Configurable
/** @return self */
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;
/** @var array<string, true> */
public static $TEXT_LEVEL_ELEMENTS = [
const TEXT_LEVEL_ELEMENTS = [
'a' => true,
'b' => true,
'i' => true,
@ -167,7 +166,7 @@ final class Element implements Renderable
foreach ($this->Contents as $C) {
if (
$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";
}
@ -179,7 +178,7 @@ final class Element implements Renderable
if (
$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";
}

View File

@ -4,8 +4,7 @@ namespace Erusev\Parsedown\Html\Sanitisation;
final class UrlSanitiser
{
/** @var string[] */
private static $COMMON_SCHEMES = [
private const COMMON_SCHEMES = [
'http://',
'https://',
'ftp://',
@ -33,7 +32,7 @@ final class UrlSanitiser
public static function filter($url, $permittedSchemes = null)
{
if (! isset($permittedSchemes)) {
$permittedSchemes = self::$COMMON_SCHEMES;
$permittedSchemes = self::COMMON_SCHEMES;
}
foreach ($permittedSchemes as $scheme) {

View File

@ -28,7 +28,7 @@ class CommonMarkTestWeak extends CommonMarkTestStrict
*/
public function __construct($name = null, array $data = [], $dataName = '')
{
$textLevelElements = \array_keys(Element::$TEXT_LEVEL_ELEMENTS);
$textLevelElements = \array_keys(Element::TEXT_LEVEL_ELEMENTS);
\array_walk(
$textLevelElements,