Skip to content

Commit 91e9f5f

Browse files
committed
refactor snippets library class: return types and phpdocs
1 parent 21c26b3 commit 91e9f5f

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

classes/snippets.php

+33-27
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,16 @@
2424

2525
namespace theme_boost_union;
2626

27+
use stdClass;
28+
2729
/**
28-
* Class snippets
30+
* Library class containing solely static functions for dealing with SCSS snippets.
2931
*
3032
* @package theme_boost_union
3133
* @copyright 2024 André Menrath, University of Graz <[email protected]>
3234
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3335
*/
3436
class snippets {
35-
/**
36-
* Constant for how many a Kilobyte are in bytes.
37-
*
38-
* @var int
39-
*/
40-
const KB_IN_BYTES = 1024;
41-
4237
/**
4338
* List of all available Snippet Meta File headers.
4439
*
@@ -65,7 +60,7 @@ class snippets {
6560
*
6661
* @return string|null
6762
*/
68-
public static function get_snippet_file($path, $source) {
63+
public static function get_snippet_file($path, $source): string|null {
6964
global $CFG;
7065
// Get the snippet file based on the different sources.
7166
if ('theme_boost_union' === $source) {
@@ -81,21 +76,26 @@ public static function get_snippet_file($path, $source) {
8176
/**
8277
* Loads the Snippets SCSS content.
8378
*
79+
* Returns an empty string as a fallback if the snippet is not found.
80+
*
8481
* @param mixed $path
8582
* @param mixed $source
8683
*
87-
* @return boolean|string
84+
* @return string
8885
*/
89-
public static function get_snippet_scss($path, $source) {
86+
public static function get_snippet_scss($path, $source): string {
9087
// Get the snippets file, based on the source.
9188
$file = self::get_snippet_file($path, $source);
9289

90+
// Return an empty string if the file is not found/readable.
9391
if (is_null($file)) {
9492
return '';
9593
}
9694

9795
$scss = file_get_contents( $file, false, null, 0);
98-
return $scss;
96+
97+
// Return the SCSS or an empty string if reading the file has failed.
98+
return $scss ?: '';
9999
}
100100

101101
/**
@@ -104,9 +104,9 @@ public static function get_snippet_scss($path, $source) {
104104
* @param string $path
105105
* @param string $source
106106
*
107-
* @return mixed
107+
* @return stdClass|null
108108
*/
109-
public static function get_snippet_meta($path, $source) {
109+
public static function get_snippet_meta($path, $source): stdClass|null {
110110
// Get the snippets file, based on the source.
111111
$file = self::get_snippet_file($path, $source);
112112

@@ -124,7 +124,7 @@ public static function get_snippet_meta($path, $source) {
124124
}
125125

126126
// Create an object containing the information.
127-
$snippet = new \stdClass();
127+
$snippet = new stdClass();
128128
$snippet->title = $headers['Snippet Title'];
129129
$snippet->description = $headers['Description'];
130130
$snippet->scope = $headers['Scope'];
@@ -141,9 +141,9 @@ public static function get_snippet_meta($path, $source) {
141141
*
142142
* @param \moodle_recordset $snippetrecordset
143143
*
144-
* @return array
144+
* @return array An array of snippet objects.
145145
*/
146-
public static function compose_snippets_data($snippetrecordset) {
146+
public static function compose_snippets_data($snippetrecordset): array {
147147
$snippets = [];
148148

149149
foreach ($snippetrecordset as $snippetrecord) {
@@ -164,7 +164,7 @@ public static function compose_snippets_data($snippetrecordset) {
164164
*
165165
* @return string
166166
*/
167-
public static function get_enabled_snippet_scss() {
167+
public static function get_enabled_snippet_scss(): string {
168168
global $DB;
169169

170170
// Compose SQL base query.
@@ -190,6 +190,8 @@ public static function get_enabled_snippet_scss() {
190190
/**
191191
* Strips close comment and close php tags from file headers.
192192
*
193+
* @copyright WordPress https://developer.wordpress.org/reference/functions/_cleanup_header_comment/
194+
*
193195
* @param string $str Header comment to clean up.
194196
*
195197
* @return string
@@ -208,14 +210,15 @@ private static function cleanup_header_comment($str) {
208210
* If the file data is not within that first 8 KB, then the author should correct
209211
* the snippet.
210212
*
213+
* @copyright forked from https://developer.wordpress.org/reference/functions/get_file_data/
214+
*
211215
* @param string $file Absolute path to the file.
212216
*
213-
* @copyright forked from https://developer.wordpress.org/reference/functions/get_file_data/
214217
* @return string[] Array of file header values keyed by header name.
215218
*/
216-
public static function get_snippet_meta_from_file($file) {
219+
public static function get_snippet_meta_from_file($file): array {
217220
// Pull only the first 8 KB of the file in.
218-
$filedata = file_get_contents( $file, false, null, 0, 8 * self::KB_IN_BYTES );
221+
$filedata = file_get_contents( $file, false, null, 0, 8192);
219222

220223
if ( false === $filedata ) {
221224
$filedata = '';
@@ -226,6 +229,7 @@ public static function get_snippet_meta_from_file($file) {
226229

227230
$headers = [];
228231

232+
// Scan for each snippet header meta information in the files top scss comment.
229233
foreach (self::SNIPPET_HEADERS as $regex) {
230234
if (preg_match('/^(?:[ \t]*)?[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $filedata, $match)
231235
&& $match[1]) {
@@ -243,23 +247,21 @@ public static function get_snippet_meta_from_file($file) {
243247
*
244248
* @return string[]
245249
*/
246-
private static function get_builtin_snippet_paths() {
250+
private static function get_builtin_snippet_paths(): array {
247251
global $CFG;
248252
// Get an array of all .scss files in the directory.
249253
$files = glob($CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . '*.scss');
250254

251-
// Get the basenames.
252-
$basenames = array_map(fn($file) => basename($file), $files);
253-
254-
return $basenames;
255+
// Return an array of the basenames of the files.
256+
return is_array($files) ? array_map(fn($file) => basename($file), $files) : [];
255257
}
256258

257259
/**
258260
* Make sure builtin snippets are in the database.
259261
*
260262
* @return void
261263
*/
262-
public static function add_builtin_snippets() {
264+
public static function add_builtin_snippets(): void {
263265
global $DB;
264266

265267
// Get builtin snippets that are present on disk.
@@ -281,6 +283,8 @@ public static function add_builtin_snippets() {
281283
return $snippet->path;
282284
}, $snippets);
283285

286+
$transaction = $DB->start_delegated_transaction();
287+
284288
foreach ($paths as $path) {
285289
if (!in_array($path, $presentpaths)) {
286290
$DB->insert_record(
@@ -295,5 +299,7 @@ public static function add_builtin_snippets() {
295299
$sortorder += 1;
296300
}
297301
}
302+
303+
$transaction->allow_commit();
298304
}
299305
}

0 commit comments

Comments
 (0)