From eeab8e50368a91557c3b4a2ef23d3f8766c3b100 Mon Sep 17 00:00:00 2001 From: Evgeniy Prokhorov Date: Sat, 12 Dec 2020 23:38:17 +0400 Subject: [PATCH] Small fix code style in constraints and added support constant PHP_EOL --- .gitignore | 1 + Parsedown.php | 150 +++++++++++++++++++++++++------------------------- 2 files changed, 75 insertions(+), 76 deletions(-) diff --git a/.gitignore b/.gitignore index d8a7996..6b772a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ composer.lock vendor/ +.idea diff --git a/Parsedown.php b/Parsedown.php index ae0cbde..dada844 100644 --- a/Parsedown.php +++ b/Parsedown.php @@ -29,7 +29,7 @@ class Parsedown $markup = $this->elements($Elements); # trim line breaks - $markup = trim($markup, "\n"); + $markup = trim($markup, PHP_EOL); return $markup; } @@ -40,13 +40,13 @@ class Parsedown $this->DefinitionData = array(); # standardize line breaks - $text = str_replace(array("\r\n", "\r"), "\n", $text); + $text = str_replace(array("\r\n", "\r"), PHP_EOL, $text); # remove surrounding line breaks - $text = trim($text, "\n"); + $text = trim($text, PHP_EOL); # split text into lines - $lines = explode("\n", $text); + $lines = explode(PHP_EOL, $text); # iterate through lines to identify blocks return $this->linesElements($lines); @@ -251,7 +251,7 @@ class Parsedown { $Block['type'] = $blockType; - if ( ! isset($Block['identified'])) + if ( !isset($Block['identified'])) { if (isset($CurrentBlock)) { @@ -274,7 +274,7 @@ class Parsedown # ~ - if (isset($CurrentBlock) and $CurrentBlock['type'] === 'Paragraph') + if (isset($CurrentBlock) && $CurrentBlock['type'] === 'Paragraph') { $Block = $this->paragraphContinue($Line, $CurrentBlock); } @@ -298,7 +298,7 @@ class Parsedown # ~ - if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type'])) + if (isset($CurrentBlock['continuable']) && $this->isBlockCompletable($CurrentBlock['type'])) { $methodName = 'block' . $CurrentBlock['type'] . 'Complete'; $CurrentBlock = $this->$methodName($CurrentBlock); @@ -318,7 +318,7 @@ class Parsedown protected function extractElement(array $Component) { - if ( ! isset($Component['element'])) + if ( !isset($Component['element'])) { if (isset($Component['markup'])) { @@ -348,7 +348,7 @@ class Parsedown protected function blockCode($Line, $Block = null) { - if (isset($Block) and $Block['type'] === 'Paragraph' and ! isset($Block['interrupted'])) + if (isset($Block) && $Block['type'] === 'Paragraph' && !isset($Block['interrupted'])) { return; } @@ -377,16 +377,14 @@ class Parsedown { if (isset($Block['interrupted'])) { - $Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']); + $Block['element']['element']['text'] .= str_repeat(PHP_EOL, $Block['interrupted']); unset($Block['interrupted']); } - $Block['element']['element']['text'] .= "\n"; - $text = substr($Line['body'], 4); - $Block['element']['element']['text'] .= $text; + $Block['element']['element']['text'] .= PHP_EOL . $text; return $Block; } @@ -402,7 +400,7 @@ class Parsedown protected function blockComment($Line) { - if ($this->markupEscaped or $this->safeMode) + if ($this->markupEscaped || $this->safeMode) { return; } @@ -432,7 +430,7 @@ class Parsedown return; } - $Block['element']['rawHtml'] .= "\n" . $Line['body']; + $Block['element']['rawHtml'] .= PHP_EOL . $Line['body']; if (strpos($Line['text'], '-->') !== false) { @@ -508,13 +506,13 @@ class Parsedown if (isset($Block['interrupted'])) { - $Block['element']['element']['text'] .= str_repeat("\n", $Block['interrupted']); + $Block['element']['element']['text'] .= str_repeat(PHP_EOL, $Block['interrupted']); unset($Block['interrupted']); } if (($len = strspn($Line['text'], $Block['char'])) >= $Block['openerLength'] - and chop(substr($Line['text'], $len), ' ') === '' + && chop(substr($Line['text'], $len), ' ') === '' ) { $Block['element']['element']['text'] = substr($Block['element']['element']['text'], 1); @@ -523,7 +521,7 @@ class Parsedown return $Block; } - $Block['element']['element']['text'] .= "\n" . $Line['body']; + $Block['element']['element']['text'] .= PHP_EOL . $Line['body']; return $Block; } @@ -547,7 +545,7 @@ class Parsedown $text = trim($Line['text'], '#'); - if ($this->strictMode and isset($text[0]) and $text[0] !== ' ') + if ($this->strictMode && isset($text[0]) && $text[0] !== ' ') { return; } @@ -615,8 +613,8 @@ class Parsedown { if ( isset($CurrentBlock) - and $CurrentBlock['type'] === 'Paragraph' - and ! isset($CurrentBlock['interrupted']) + && $CurrentBlock['type'] === 'Paragraph' + && !isset($CurrentBlock['interrupted']) ) { return; } @@ -642,7 +640,7 @@ class Parsedown protected function blockListContinue($Line, array $Block) { - if (isset($Block['interrupted']) and empty($Block['li']['handler']['argument'])) + if (isset($Block['interrupted']) && empty($Block['li']['handler']['argument'])) { return null; } @@ -650,13 +648,13 @@ class Parsedown $requiredIndent = ($Block['indent'] + strlen($Block['data']['marker'])); if ($Line['indent'] < $requiredIndent - and ( + && ( ( $Block['data']['type'] === 'ol' - and preg_match('/^[0-9]++'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) - ) or ( + && preg_match('/^[0-9]++'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) + ) || ( $Block['data']['type'] === 'ul' - and preg_match('/^'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) + && preg_match('/^'.$Block['data']['markerTypeRegex'].'(?:[ ]++(.*)|$)/', $Line['text'], $matches) ) ) ) { @@ -688,12 +686,12 @@ class Parsedown return $Block; } - elseif ($Line['indent'] < $requiredIndent and $this->blockList($Line)) + elseif ($Line['indent'] < $requiredIndent && $this->blockList($Line)) { return null; } - if ($Line['text'][0] === '[' and $this->blockReference($Line)) + if ($Line['text'][0] === '[' && $this->blockReference($Line)) { return $Block; } @@ -716,7 +714,7 @@ class Parsedown return $Block; } - if ( ! isset($Block['interrupted'])) + if ( !isset($Block['interrupted'])) { $text = preg_replace('/^[ ]{0,'.$requiredIndent.'}+/', '', $Line['body']); @@ -771,14 +769,14 @@ class Parsedown return; } - if ($Line['text'][0] === '>' and preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) + if ($Line['text'][0] === '>' && preg_match('/^>[ ]?+(.*+)/', $Line['text'], $matches)) { $Block['element']['handler']['argument'] []= $matches[1]; return $Block; } - if ( ! isset($Block['interrupted'])) + if ( !isset($Block['interrupted'])) { $Block['element']['handler']['argument'] []= $Line['text']; @@ -793,7 +791,7 @@ class Parsedown { $marker = $Line['text'][0]; - if (substr_count($Line['text'], $marker) >= 3 and chop($Line['text'], " $marker") === '') + if (substr_count($Line['text'], $marker) >= 3 && chop($Line['text'], " $marker") === '') { $Block = array( 'element' => array( @@ -810,12 +808,12 @@ class Parsedown protected function blockSetextHeader($Line, array $Block = null) { - if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) + if ( !isset($Block) || $Block['type'] !== 'Paragraph' || isset($Block['interrupted'])) { return; } - if ($Line['indent'] < 4 and chop(chop($Line['text'], ' '), $Line['text'][0]) === '') + if ($Line['indent'] < 4 && chop(chop($Line['text'], ' '), $Line['text'][0]) === '') { $Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2'; @@ -828,7 +826,7 @@ class Parsedown protected function blockMarkup($Line) { - if ($this->markupEscaped or $this->safeMode) + if ($this->markupEscaped || $this->safeMode) { return; } @@ -856,12 +854,12 @@ class Parsedown protected function blockMarkupContinue($Line, array $Block) { - if (isset($Block['closed']) or isset($Block['interrupted'])) + if (isset($Block['closed']) || isset($Block['interrupted'])) { return; } - $Block['element']['rawHtml'] .= "\n" . $Line['body']; + $Block['element']['rawHtml'] .= PHP_EOL . $Line['body']; return $Block; } @@ -872,7 +870,7 @@ class Parsedown protected function blockReference($Line) { if (strpos($Line['text'], ']') !== false - and preg_match('/^\[(.+?)\]:[ ]*+?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/', $Line['text'], $matches) + && preg_match('/^\[(.+?)\]:[ ]*+?(?:[ ]+["\'(](.+)["\')])?[ ]*+$/', $Line['text'], $matches) ) { $id = strtolower($matches[1]); @@ -896,16 +894,16 @@ class Parsedown protected function blockTable($Line, array $Block = null) { - if ( ! isset($Block) or $Block['type'] !== 'Paragraph' or isset($Block['interrupted'])) + if ( !isset($Block) || $Block['type'] !== 'Paragraph' || isset($Block['interrupted'])) { return; } if ( strpos($Block['element']['handler']['argument'], '|') === false - and strpos($Line['text'], '|') === false - and strpos($Line['text'], ':') === false - or strpos($Block['element']['handler']['argument'], "\n") !== false + && strpos($Line['text'], '|') === false + && strpos($Line['text'], ':') === false + || strpos($Block['element']['handler']['argument'], PHP_EOL) !== false ) { return; } @@ -1024,7 +1022,7 @@ class Parsedown return; } - if (count($Block['alignments']) === 1 or $Line['text'][0] === '|' or strpos($Line['text'], '|')) + if (count($Block['alignments']) === 1 || $Line['text'][0] === '|' || strpos($Line['text'], '|')) { $Elements = array(); @@ -1097,7 +1095,7 @@ class Parsedown return; } - $Block['element']['handler']['argument'] .= "\n".$Line['text']; + $Block['element']['handler']['argument'] .= PHP_EOL.$Line['text']; return $Block; } @@ -1135,7 +1133,7 @@ class Parsedown protected function lineElements($text, $nonNestables = array()) { # standardize line breaks - $text = str_replace(array("\r\n", "\r"), "\n", $text); + $text = str_replace(array("\r\n", "\r"), PHP_EOL, $text); $Elements = array(); @@ -1165,21 +1163,21 @@ class Parsedown $Inline = $this->{"inline$inlineType"}($Excerpt); - if ( ! isset($Inline)) + if ( !isset($Inline)) { continue; } # makes sure that the inline belongs to "our" marker - if (isset($Inline['position']) and $Inline['position'] > $markerPosition) + if (isset($Inline['position']) && $Inline['position'] > $markerPosition) { continue; } # sets a default inline position - if ( ! isset($Inline['position'])) + if ( !isset($Inline['position'])) { $Inline['position'] = $markerPosition; } @@ -1223,7 +1221,7 @@ class Parsedown foreach ($Elements as &$Element) { - if ( ! isset($Element['autobreak'])) + if ( !isset($Element['autobreak'])) { $Element['autobreak'] = false; } @@ -1247,7 +1245,7 @@ class Parsedown $this->breaksEnabled ? '/[ ]*+\n/' : '/(?:[ ]*+\\\\|[ ]{2,}+)\n/', array( array('name' => 'br'), - array('text' => "\n"), + array('text' => PHP_EOL), ), $text ); @@ -1282,11 +1280,11 @@ class Parsedown . $hostnameLabel . '(?:\.' . $hostnameLabel . ')*'; if (strpos($Excerpt['text'], '>') !== false - and preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches) + && preg_match("/^<((mailto:)?$commonMarkEmail)>/i", $Excerpt['text'], $matches) ){ $url = $matches[1]; - if ( ! isset($matches[2])) + if ( !isset($matches[2])) { $url = "mailto:$url"; } @@ -1306,14 +1304,14 @@ class Parsedown protected function inlineEmphasis($Excerpt) { - if ( ! isset($Excerpt['text'][1])) + if ( !isset($Excerpt['text'][1])) { return; } $marker = $Excerpt['text'][0]; - if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) + if ($Excerpt['text'][1] === $marker && preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) { $emphasis = 'strong'; } @@ -1341,7 +1339,7 @@ class Parsedown protected function inlineEscapeSequence($Excerpt) { - if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters)) + if (isset($Excerpt['text'][1]) && in_array($Excerpt['text'][1], $this->specialCharacters)) { return array( 'element' => array('rawHtml' => $Excerpt['text'][1]), @@ -1352,7 +1350,7 @@ class Parsedown protected function inlineImage($Excerpt) { - if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') + if ( !isset($Excerpt['text'][1]) || $Excerpt['text'][1] !== '[') { return; } @@ -1443,7 +1441,7 @@ class Parsedown $definition = strtolower($Element['handler']['argument']); } - if ( ! isset($this->DefinitionData['Reference'][$definition])) + if ( !isset($this->DefinitionData['Reference'][$definition])) { return; } @@ -1462,12 +1460,12 @@ class Parsedown protected function inlineMarkup($Excerpt) { - if ($this->markupEscaped or $this->safeMode or strpos($Excerpt['text'], '>') === false) + if ($this->markupEscaped || $this->safeMode || strpos($Excerpt['text'], '>') === false) { return; } - if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt['text'], $matches)) + if ($Excerpt['text'][1] === '/' && preg_match('/^<\/\w[\w-]*+[ ]*+>/s', $Excerpt['text'], $matches)) { return array( 'element' => array('rawHtml' => $matches[0]), @@ -1475,7 +1473,7 @@ class Parsedown ); } - if ($Excerpt['text'][1] === '!' and preg_match('/^/s', $Excerpt['text'], $matches)) + if ($Excerpt['text'][1] === '!' && preg_match('/^/s', $Excerpt['text'], $matches)) { return array( 'element' => array('rawHtml' => $matches[0]), @@ -1483,7 +1481,7 @@ class Parsedown ); } - if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w[\w-]*+(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+\/?>/s', $Excerpt['text'], $matches)) + if ($Excerpt['text'][1] !== ' ' && preg_match('/^<\w[\w-]*+(?:[ ]*+'.$this->regexHtmlAttribute.')*+[ ]*+\/?>/s', $Excerpt['text'], $matches)) { return array( 'element' => array('rawHtml' => $matches[0]), @@ -1494,8 +1492,8 @@ class Parsedown protected function inlineSpecialCharacter($Excerpt) { - if (substr($Excerpt['text'], 1, 1) !== ' ' and strpos($Excerpt['text'], ';') !== false - and preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches) + if (substr($Excerpt['text'], 1, 1) !== ' ' && strpos($Excerpt['text'], ';') !== false + && preg_match('/^&(#?+[0-9a-zA-Z]++);/', $Excerpt['text'], $matches) ) { return array( 'element' => array('rawHtml' => '&' . $matches[1] . ';'), @@ -1508,12 +1506,12 @@ class Parsedown protected function inlineStrikethrough($Excerpt) { - if ( ! isset($Excerpt['text'][1])) + if ( !isset($Excerpt['text'][1])) { return; } - if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) + if ($Excerpt['text'][1] === '~' && preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) { return array( 'extent' => strlen($matches[0]), @@ -1531,13 +1529,13 @@ class Parsedown protected function inlineUrl($Excerpt) { - if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') + if ($this->urlsLinked !== true || !isset($Excerpt['text'][2]) || $Excerpt['text'][2] !== '/') { return; } if (strpos($Excerpt['context'], 'http') !== false - and preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE) + && preg_match('/\bhttps?+:[\/]{2}[^\s<]+\b\/*+/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE) ) { $url = $matches[0][0]; @@ -1559,7 +1557,7 @@ class Parsedown protected function inlineUrlTag($Excerpt) { - if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) + if (strpos($Excerpt['text'], '>') !== false && preg_match('/^<(\w++:\/{2}[^ >]++)>/i', $Excerpt['text'], $matches)) { $url = $matches[1]; @@ -1789,11 +1787,11 @@ class Parsedown // (autobreak === false) covers both sides of an element $autoBreak = !$autoBreak ? $autoBreak : $autoBreakNext; - $markup .= ($autoBreak ? "\n" : '') . $this->element($Element); + $markup .= ($autoBreak ? PHP_EOL : '') . $this->element($Element); $autoBreak = $autoBreakNext; } - $markup .= $autoBreak ? "\n" : ''; + $markup .= $autoBreak ? PHP_EOL : ''; return $markup; } @@ -1804,9 +1802,9 @@ class Parsedown { $Elements = $this->linesElements($lines); - if ( ! in_array('', $lines) - and isset($Elements[0]) and isset($Elements[0]['name']) - and $Elements[0]['name'] === 'p' + if ( !in_array('', $lines, true) + && isset($Elements[0], $Elements[0]['name']) + && $Elements[0]['name'] === 'p' ) { unset($Elements[0]['name']); } @@ -1866,7 +1864,7 @@ class Parsedown 'img' => 'src', ); - if ( ! isset($Element['name'])) + if ( !isset($Element['name'])) { unset($Element['attributes']); return $Element; @@ -1877,12 +1875,12 @@ class Parsedown $Element = $this->filterUnsafeUrlInAttribute($Element, $safeUrlNameToAtt[$Element['name']]); } - if ( ! empty($Element['attributes'])) + if ( !empty($Element['attributes'])) { foreach ($Element['attributes'] as $att => $val) { # filter out badly parsed attribute - if ( ! preg_match($goodAttribute, $att)) + if ( !preg_match($goodAttribute, $att)) { unset($Element['attributes'][$att]); }