MDL-80072 core: Coding Style fixes

This commit is contained in:
Andrew Nicols
2024-02-12 11:11:17 +08:00
parent e8eb163ff9
commit ae80cd739b
2 changed files with 73 additions and 50 deletions
+32 -27
View File
@@ -42,16 +42,16 @@ class formatting {
* glossary concepts.
*
* @staticvar bool $strcache
* @param string $string The string to be filtered. Should be plain text, expect
* @param null|string $string The string to be filtered. Should be plain text, expect
* possibly for multilang tags.
* @param boolean $striplinks To strip any link in the result text. Moodle 1.8 default changed from false to true! MDL-8713
* @param array $options options array/object or courseid
* @return string
*/
public function format_string(
$string,
$striplinks = true,
$options = null,
?string $string,
bool $striplinks = true,
?array $options = null,
): string {
global $CFG, $PAGE;
@@ -63,19 +63,19 @@ class formatting {
// We'll use a in-memory cache here to speed up repeated strings.
static $strcache = false;
if (empty($CFG->version) or $CFG->version < 2013051400 or during_initial_install()) {
if (empty($CFG->version) || $CFG->version < 2013051400 || during_initial_install()) {
// Do not filter anything during installation or before upgrade completes.
return $string = strip_tags($string);
}
if ($strcache === false or count($strcache) > 2000) {
if ($strcache === false || count($strcache) > 2000) {
// This number might need some tuning to limit memory usage in cron.
$strcache = array();
$strcache = [];
}
if (is_numeric($options)) {
// Legacy courseid usage.
$options = array('context' => context_course::instance($options));
$options = ['context' => context_course::instance($options)];
} else {
// Detach object, we can not modify it.
$options = (array)$options;
@@ -99,10 +99,10 @@ class formatting {
}
// Calculate md5.
$cachekeys = array(
$cachekeys = [
$string, $striplinks, $options['context']->id,
$options['escape'], current_language(), $options['filter']
);
$options['escape'], current_language(), $options['filter'],
];
$md5 = md5(implode('<+>', $cachekeys));
// Fetch from cache if possible.
@@ -123,7 +123,7 @@ class formatting {
// If the site requires it, strip ALL tags from this string.
if (!empty($this->get_striptags())) {
if ($options['escape']) {
$string = str_replace(array('<', '>'), array('&lt;', '&gt;'), strip_tags($string));
$string = str_replace(['<', '>'], ['&lt;', '&gt;'], strip_tags($string));
} else {
$string = strip_tags($string);
}
@@ -152,7 +152,8 @@ class formatting {
* <pre>
* Options:
* trusted : If true the string won't be cleaned. Default false required noclean=true.
* noclean : If true the string won't be cleaned, unless $CFG->forceclean is set. Default false required trusted=true.
* noclean : If true the string won't be cleaned, unless $CFG->forceclean is set.
* Default false required trusted=true.
* nocache : If true the strign will not be cached and will be formatted every call. Default false.
* filter : If true the string will be run through applicable filters as well. Default true.
* para : If true then the returned string will be wrapped in div tags. Default true.
@@ -166,18 +167,18 @@ class formatting {
* </pre>
*
* @staticvar array $croncache
* @param string $text The text to be formatted. This is raw text originally from user input.
* @param int $format Identifier of the text format to be used
* @param null|string $text The text to be formatted. This is raw text originally from user input.
* @param string $format Identifier of the text format to be used
* [FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_MARKDOWN]
* @param stdClass|array $options text formatting options
* @param int $courseiddonotuse deprecated course id, use context option instead
* @return string
*/
public function format_text(
$text,
$format = FORMAT_MOODLE,
$options = null,
) {
?string $text,
string $format = FORMAT_MOODLE,
?array $options = null,
): string {
global $CFG, $DB, $PAGE;
if ($text === '' || is_null($text)) {
@@ -197,7 +198,7 @@ class formatting {
$options['trusted'] = false;
}
if (!isset($options['noclean'])) {
if ($options['trusted'] and trusttext_active()) {
if ($options['trusted'] && trusttext_active()) {
// No cleaning if text trusted and noclean not specified.
$options['noclean'] = true;
} else {
@@ -226,7 +227,7 @@ class formatting {
$options['blanktarget'] = !empty($options['blanktarget']);
// Calculate best context.
if (empty($CFG->version) or $CFG->version < 2013051400 or during_initial_install()) {
if (empty($CFG->version) || $CFG->version < 2013051400 || during_initial_install()) {
// Do not filter anything during installation or before upgrade completes.
$context = null;
} else if (isset($options['context'])) { // First by explicit passed context option.
@@ -249,13 +250,13 @@ class formatting {
if ($options['filter']) {
$filtermanager = \filter_manager::instance();
$filtermanager->setup_page_for_filters($PAGE, $context); // Setup global stuff filters may have.
$filteroptions = array(
$filteroptions = [
'originalformat' => $format,
'noclean' => $options['noclean'],
);
];
} else {
$filtermanager = new \null_filter_manager();
$filteroptions = array();
$filteroptions = [];
}
switch ($format) {
@@ -331,7 +332,7 @@ class formatting {
}
if (!empty($options['overflowdiv'])) {
$text = \html_writer::tag('div', $text, array('class' => 'no-overflow'));
$text = \html_writer::tag('div', $text, ['class' => 'no-overflow']);
}
if ($options['blanktarget']) {
@@ -353,7 +354,11 @@ class formatting {
// $domdoc->loadHTML($text, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); however it seems like some libxml
// versions don't work properly and end up leaving <html><body>, so I'm forced to use
// this regex to remove those tags as a preventive measure.
$text = trim(preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $domdoc->saveHTML($domdoc->documentElement)));
$text = trim(preg_replace(
'~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i',
'',
$domdoc->saveHTML($domdoc->documentElement),
));
}
return $text;
+41 -23
View File
@@ -364,7 +364,7 @@ class formatting_test extends \advanced_testcase {
filter_set_global_state('emoticon', TEXTFILTER_ON);
$this->assertEquals(
'<p>:-)</p>',
$formatter->format_text('<p>:-)</p>', FORMAT_HTML, array('filter' => false))
$formatter->format_text('<p>:-)</p>', FORMAT_HTML, ['filter' => false])
);
}
@@ -387,7 +387,7 @@ class formatting_test extends \advanced_testcase {
filter_set_global_state('emoticon', TEXTFILTER_ON);
$this->assertEquals(
':-)',
$formatter->format_text(':-)', FORMAT_PLAIN, array('filter' => false))
$formatter->format_text(':-)', FORMAT_PLAIN, ['filter' => false])
);
}
@@ -411,7 +411,7 @@ class formatting_test extends \advanced_testcase {
filter_set_global_state('emoticon', TEXTFILTER_ON);
$this->assertEquals(
"<p><em>:-)</em></p>\n",
$formatter->format_text('*:-)*', FORMAT_MARKDOWN, array('filter' => false))
$formatter->format_text('*:-)*', FORMAT_MARKDOWN, ['filter' => false])
);
}
@@ -435,7 +435,7 @@ class formatting_test extends \advanced_testcase {
filter_set_global_state('emoticon', TEXTFILTER_ON);
$this->assertEquals(
'<div class="text_to_html"><p>:-)</p></div>',
$formatter->format_text('<p>:-)</p>', FORMAT_MOODLE, array('filter' => false))
$formatter->format_text('<p>:-)</p>', FORMAT_MOODLE, ['filter' => false])
);
}
@@ -453,34 +453,48 @@ class formatting_test extends \advanced_testcase {
$context = \context_course::instance($course->id);
$page = $this->getDataGenerator()->create_module(
'page',
['course' => $course->id, 'name' => 'Test 1']
['course' => $course->id, 'name' => 'Test 1'],
);
$cm = get_coursemodule_from_instance('page', $page->id, $page->course, false, MUST_EXIST);
$pageurl = $CFG->wwwroot . '/mod/page/view.php?id=' . $cm->id;
$this->assertSame(
'<p>Read <a class="autolink" title="Test 1" href="' . $pageurl . '">Test 1</a>.</p>',
$formatter->format_text('<p>Read Test 1.</p>', FORMAT_HTML, ['context' => $context])
$formatter->format_text('<p>Read Test 1.</p>', FORMAT_HTML, ['context' => $context]),
);
$this->assertSame(
'<p>Read <a class="autolink" title="Test 1" href="' . $pageurl . '">Test 1</a>.</p>',
$formatter->format_text('<p>Read Test 1.</p>', FORMAT_HTML, ['context' => $context, 'noclean' => true])
$formatter->format_text('<p>Read Test 1.</p>', FORMAT_HTML, ['context' => $context, 'noclean' => true]),
);
$this->assertSame(
'<p>Read Test 1.</p>',
$formatter->format_text('<p><nolink>Read Test 1.</nolink></p>', FORMAT_HTML, ['context' => $context, 'noclean' => false])
$formatter->format_text(
'<p><nolink>Read Test 1.</nolink></p>',
FORMAT_HTML,
[
'context' => $context,
'noclean' => false,
],
),
);
$this->assertSame(
'<p>Read Test 1.</p>',
$formatter->format_text('<p><nolink>Read Test 1.</nolink></p>', FORMAT_HTML, ['context' => $context, 'noclean' => true])
$formatter->format_text(
'<p><nolink>Read Test 1.</nolink></p>',
FORMAT_HTML,
[
'context' => $context,
'noclean' => true,
],
),
);
$this->assertSame(
'<p><span class="nolink">Read Test 1.</span></p>',
$formatter->format_text('<p><span class="nolink">Read Test 1.</span></p>', FORMAT_HTML, ['context' => $context])
$formatter->format_text('<p><span class="nolink">Read Test 1.</span></p>', FORMAT_HTML, ['context' => $context]),
);
}
@@ -489,7 +503,7 @@ class formatting_test extends \advanced_testcase {
$this->assertEquals(
'<div class="no-overflow"><p>Hello world</p></div>',
$formatter->format_text('<p>Hello world</p>', FORMAT_HTML, array('overflowdiv' => true))
$formatter->format_text('<p>Hello world</p>', FORMAT_HTML, ['overflowdiv' => true]),
);
}
@@ -502,7 +516,7 @@ class formatting_test extends \advanced_testcase {
*/
public function test_format_text_blanktarget($link, $expected): void {
$formatter = new formatting();
$actual = $formatter->format_text($link, FORMAT_MOODLE, array('blanktarget' => true, 'filter' => false, 'noclean' => true));
$actual = $formatter->format_text($link, FORMAT_MOODLE, ['blanktarget' => true, 'filter' => false, 'noclean' => true]);
$this->assertEquals($expected, $actual);
}
@@ -511,49 +525,53 @@ class formatting_test extends \advanced_testcase {
*
* @return array of testcases
*/
public function format_text_blanktarget_testcases() {
public static function format_text_blanktarget_testcases(): array {
return [
'Simple link' => [
'<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4">Hey, that\'s pretty good!</a>',
'<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank"' .
' rel="noreferrer">Hey, that\'s pretty good!</a></div>'
' rel="noreferrer">Hey, that\'s pretty good!</a></div>',
],
'Link with rel' => [
'<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="nofollow">Hey, that\'s pretty good!</a>',
'<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="nofollow noreferrer"' .
' target="_blank">Hey, that\'s pretty good!</a></div>'
' target="_blank">Hey, that\'s pretty good!</a></div>',
],
'Link with rel noreferrer' => [
'<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="noreferrer">Hey, that\'s pretty good!</a>',
'<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" rel="noreferrer"' .
' target="_blank">Hey, that\'s pretty good!</a></div>'
' target="_blank">Hey, that\'s pretty good!</a></div>',
],
'Link with target' => [
'<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_self">Hey, that\'s pretty good!</a>',
'<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_self">' .
'Hey, that\'s pretty good!</a></div>'
'Hey, that\'s pretty good!</a></div>',
],
'Link with target blank' => [
'<a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank">Hey, that\'s pretty good!</a>',
'<div class="text_to_html"><a href="https://www.youtube.com/watch?v=JeimE8Wz6e4" target="_blank"' .
' rel="noreferrer">Hey, that\'s pretty good!</a></div>'
' rel="noreferrer">Hey, that\'s pretty good!</a></div>',
],
'Link with Frank\'s casket inscription' => [
// phpcs:ignore moodle.Files.LineLength
'<a href="https://en.wikipedia.org/wiki/Franks_Casket">ᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻ' .
'ᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ</a>',
'<div class="text_to_html"><a href="https://en.wikipedia.org/wiki/Franks_Casket" target="_blank" ' .
'<div class="text_to_html"><a href="https://en.wikipedia.org/wiki/Franks_Casket" target="_blank" ' .
// phpcs:ignore moodle.Files.LineLength
'rel="noreferrer">ᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁᚠᛁᛋᚳ᛫ᚠᛚᚩᛞᚢ᛫ᚪᚻᚩᚠᚩᚾᚠᛖᚱᚷ ᛖᚾ' .
'ᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ</a></div>'
'ᛒᛖᚱᛁᚷ ᚹᚪᚱᚦᚷᚪ᛬ᛋᚱᛁᚳᚷᚱᚩᚱᚾᚦᚫᚱᚻᛖᚩᚾᚷᚱᛖᚢᛏᚷᛁᛋᚹᚩᛗ ᚻᚱᚩᚾᚫᛋᛒᚪᚾ ᛗᚫᚷᛁ</a></div>',
],
'No link' => [
'Some very boring text written with the Latin script',
'<div class="text_to_html">Some very boring text written with the Latin script</div>'
'<div class="text_to_html">Some very boring text written with the Latin script</div>',
],
'No link with Thror\'s map runes' => [
// phpcs:ignore moodle.Files.LineLength
'ᛋᛏᚫᚾᛞ ᛒᚣ ᚦᛖ ᚷᚱᛖᚣ ᛋᛏᚩᚾᛖ ᚻᚹᛁᛚᛖ ᚦᛖ ᚦᚱᚢᛋᚻ ᚾᚩᚳᛋ ᚫᚾᛞ ᚦᛖ ᛋᛖᛏᛏᛁᚾᚷ ᛋᚢᚾ ᚹᛁᚦ ᚦᛖ ᛚᚫᛋᛏ ᛚᛁᚷᚻᛏ ᚩᚠ ᛞᚢᚱᛁᚾᛋ ᛞᚫᚣ ᚹᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ',
// phpcs:ignore moodle.Files.LineLength
'<div class="text_to_html">ᛋᛏᚫᚾᛞ ᛒᚣ ᚦᛖ ᚷᚱᛖᚣ ᛋᛏᚩᚾᛖ ᚻᚹᛁᛚᛖ ᚦᛖ ᚦᚱᚢᛋᚻ ᚾᚩᚳᛋ ᚫᚾᛞ ᚦᛖ ᛋᛖᛏᛏᛁᚾᚷ ᛋᚢᚾ ᚹᛁᚦ ᚦᛖ ᛚᚫᛋᛏ ᛚᛁᚷᚻᛏ ᚩᚠ ᛞᚢᚱᛁᚾᛋ ᛞᚫᚣ ᚹ' .
'ᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ</div>'
]
'ᛁᛚᛚ ᛋᚻᛁᚾᛖ ᚢᛈᚩᚾ ᚦᛖ ᚳᛖᚣᚻᚩᛚᛖ</div>',
],
];
}