From 371ce37734f219d2fd4d96ec3c4436c21454274a Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 28 Aug 2024 11:13:53 +0300 Subject: [PATCH] Add strict types --- Parsedown.php | 307 ++++++++++++++++++++++---------------- test/SampleExtensions.php | 4 +- 2 files changed, 177 insertions(+), 134 deletions(-) diff --git a/Parsedown.php b/Parsedown.php index fb5239d1f..a2c2c5b8e 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -1,4 +1,5 @@ textElements($text); @@ -34,7 +35,7 @@ function text($text) return $markup; } - protected function textElements($text) + protected function textElements(string $text) : array { # make sure no definitions are set $this->DefinitionData = array(); @@ -56,52 +57,53 @@ protected function textElements($text) # Setters # - function setBreaksEnabled($breaksEnabled) + function setBreaksEnabled(bool $breaksEnabled) : Parsedown { $this->breaksEnabled = $breaksEnabled; return $this; } - protected $breaksEnabled; + protected bool $breaksEnabled = false; - function setMarkupEscaped($markupEscaped) + function setMarkupEscaped(bool $markupEscaped) : Parsedown { $this->markupEscaped = $markupEscaped; return $this; } - protected $markupEscaped; + protected bool $markupEscaped = false; - function setUrlsLinked($urlsLinked) + function setUrlsLinked(bool $urlsLinked) : Parsedown { $this->urlsLinked = $urlsLinked; return $this; } - protected $urlsLinked = true; + protected bool $urlsLinked = true; - function setSafeMode($safeMode) + function setSafeMode(bool $safeMode) : Parsedown { - $this->safeMode = (bool) $safeMode; + $this->safeMode = $safeMode; return $this; } - protected $safeMode; + protected bool $safeMode = false; - function setStrictMode($strictMode) + function setStrictMode(bool $strictMode) : Parsedown { - $this->strictMode = (bool) $strictMode; + $this->strictMode = $strictMode; return $this; } - protected $strictMode; + protected bool $strictMode = false; - protected $safeLinksWhitelist = array( + /** @var string[] */ + protected array $safeLinksWhitelist = array( 'http://', 'https://', 'ftp://', @@ -123,7 +125,8 @@ function setStrictMode($strictMode) # Lines # - protected $BlockTypes = array( + /** @var string[][] */ + protected array $BlockTypes = array( '#' => array('Header'), '*' => array('Rule', 'List'), '+' => array('List'), @@ -151,7 +154,8 @@ function setStrictMode($strictMode) # ~ - protected $unmarkedBlockTypes = array( + /** @var string[] */ + protected array $unmarkedBlockTypes = array( 'Code', ); @@ -159,19 +163,19 @@ function setStrictMode($strictMode) # Blocks # - protected function lines(array $lines) + protected function lines(array $lines) : string { return $this->elements($this->linesElements($lines)); } - protected function linesElements(array $lines) + protected function linesElements(array $lines) : array { $Elements = array(); $CurrentBlock = null; foreach ($lines as $line) { - if (chop($line) === '') + if (rtrim($line) === '') { if (isset($CurrentBlock)) { @@ -316,7 +320,7 @@ protected function linesElements(array $lines) return $Elements; } - protected function extractElement(array $Component) + protected function extractElement(array $Component) : array { if ( ! isset($Component['element'])) { @@ -333,12 +337,12 @@ protected function extractElement(array $Component) return $Component['element']; } - protected function isBlockContinuable($Type) + protected function isBlockContinuable(string $Type) : bool { return method_exists($this, 'block' . $Type . 'Continue'); } - protected function isBlockCompletable($Type) + protected function isBlockCompletable(string $Type) : bool { return method_exists($this, 'block' . $Type . 'Complete'); } @@ -346,11 +350,11 @@ protected function isBlockCompletable($Type) # # Code - protected function blockCode($Line, $Block = null) + protected function blockCode(array $Line, ?array $Block = null) : ?array { if (isset($Block) and $Block['type'] === 'Paragraph' and ! isset($Block['interrupted'])) { - return; + return null; } if ($Line['indent'] >= 4) @@ -369,9 +373,11 @@ protected function blockCode($Line, $Block = null) return $Block; } + + return null; } - protected function blockCodeContinue($Line, $Block) + protected function blockCodeContinue(array $Line, array $Block) : ?array { if ($Line['indent'] >= 4) { @@ -390,9 +396,11 @@ protected function blockCodeContinue($Line, $Block) return $Block; } + + return null; } - protected function blockCodeComplete($Block) + protected function blockCodeComplete(array $Block) : array { return $Block; } @@ -400,11 +408,11 @@ protected function blockCodeComplete($Block) # # Comment - protected function blockComment($Line) + protected function blockComment(array $Line) : ?array { if ($this->markupEscaped or $this->safeMode) { - return; + return null; } if (strpos($Line['text'], '