Skip to content

Commit 02e5a49

Browse files
committed
refactor: early return and some comments for readability
1 parent 869f4fa commit 02e5a49

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

Extension.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function __construct()
5959
$msg_error .= ' Parsedown ToC Extension requires a later version of Parsedown.' . PHP_EOL;
6060
$msg_error .= ' - Current version : ' . \Parsedown::version . PHP_EOL;
6161
$msg_error .= ' - Required version: ' . self::VERSION_PARSEDOWN_REQUIRED . PHP_EOL;
62+
6263
throw new Exception($msg_error);
6364
}
6465

@@ -84,40 +85,41 @@ protected function blockHeader($Line)
8485
{
8586
// Use parent blockHeader method to process the $Line to $Block
8687
$Block = DynamicParent::blockHeader($Line);
88+
if (empty($Block)) {
89+
return;
90+
}
8791

88-
if (! empty($Block)) {
89-
// Get the text of the heading
90-
if (isset($Block['element']['handler']['argument'])) {
91-
// Compatibility with old Parsedown Version
92-
$text = $Block['element']['handler']['argument'];
93-
}
94-
if (isset($Block['element']['text'])) {
95-
// Current Parsedown
96-
$text = $Block['element']['text'];
97-
}
92+
// Get the text of the heading
93+
if (isset($Block['element']['handler']['argument'])) {
94+
// Compatibility with old Parsedown Version
95+
$text = $Block['element']['handler']['argument'];
96+
}
97+
if (isset($Block['element']['text'])) {
98+
// Current Parsedown
99+
$text = $Block['element']['text'];
100+
}
98101

99-
// Get the heading level. Levels are h1, h2, ..., h6
100-
$level = $Block['element']['name'];
102+
// Get the heading level. Levels are h1, h2, ..., h6
103+
$level = $Block['element']['name'];
101104

102-
// Get the anchor of the heading to link from the ToC list
103-
$id = isset($Block['element']['attributes']['id']) ?
104-
$Block['element']['attributes']['id'] : $this->createAnchorID($text);
105+
// Get the anchor of the heading to link from the ToC list
106+
$id = isset($Block['element']['attributes']['id']) ?
107+
$Block['element']['attributes']['id'] : $this->createAnchorID($text);
105108

106-
// Set attributes to head tags
107-
$Block['element']['attributes'] = array(
108-
'id' => $id,
109-
'name' => $id,
110-
);
109+
// Set attributes to head tags
110+
$Block['element']['attributes'] = array(
111+
'id' => $id,
112+
'name' => $id,
113+
);
111114

112-
// Add/stores the heading element info to the ToC list
113-
$this->setContentsList(array(
114-
'text' => $text,
115-
'id' => $id,
116-
'level' => $level
117-
));
115+
// Add/stores the heading element info to the ToC list
116+
$this->setContentsList(array(
117+
'text' => $text,
118+
'id' => $id,
119+
'level' => $level
120+
));
118121

119-
return $Block;
120-
}
122+
return $Block;
121123
}
122124

123125
/**
@@ -151,19 +153,21 @@ public function contentsList($type_return = 'string')
151153
// Parses the ToC list in markdown to HTML
152154
$result = $this->body($this->contentsListString);
153155
}
156+
154157
return $result;
155158
}
156159

157160
if ('json' === strtolower($type_return)) {
158161
return json_encode($this->contentsListArray);
159162
}
160163

161-
// Forces to return ToC as "string"
164+
// Log the error and forces to return ToC as "string"
162165
error_log(
163166
'Unknown return type given while parsing ToC.'
164167
. ' At: ' . __FUNCTION__ . '() '
165168
. ' in Line:' . __LINE__ . ' (Using default type)'
166169
);
170+
167171
return $this->contentsList('string');
168172
}
169173

@@ -265,6 +269,7 @@ protected function getSalt()
265269
}
266270

267271
$salt = hash('md5', time());
272+
268273
return $salt;
269274
}
270275

0 commit comments

Comments
 (0)