Skip to content

Commit

Permalink
Various coding-style files using moodle-extra standard
Browse files Browse the repository at this point in the history
Also, unrelated, remove a step from readme_moodle.txt
because now it's not needed.
  • Loading branch information
stronk7 committed Sep 30, 2023
1 parent 9470963 commit 7fd838a
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 56 deletions.
1 change: 0 additions & 1 deletion classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
Expand Down
4 changes: 1 addition & 3 deletions classes/report.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report extends \PHP_CodeSniffer\Reports\Xml {

/**
* Generate a partial report for a single processed file.
*
Expand All @@ -58,7 +57,7 @@ public function generateFileReport($report, \PHP_CodeSniffer\Files\File $phpcsFi
}

// Here we are, with a file with 0 errors and warnings.
$out = new \XMLWriter;
$out = new \XMLWriter();
$out->openMemory();
$out->setIndent(true);

Expand All @@ -71,6 +70,5 @@ public function generateFileReport($report, \PHP_CodeSniffer\Files\File $phpcsFi
echo $out->flush();

return true;

}
}
3 changes: 1 addition & 2 deletions classes/runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class runner extends \PHP_CodeSniffer\Runner {

/**
* Create an instance of the runner.
*/
Expand Down Expand Up @@ -144,7 +143,7 @@ public function run() {
echo $e->getMessage();
return $e->getCode();
} catch (\Exception $e) {
$error = 'Problem during processing; checking has been aborted. The error message was: '.$e->getMessage();
$error = 'Problem during processing; checking has been aborted. The error message was: ' . $e->getMessage();
$file->addErrorOnLine($error, 1, 'Internal.Exception');
}
$file->cleanUp();
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

// Look for other problems, not handled by codesniffer. Use same list of ignored (originally in keys, now in values).
local_codechecker_check_other_files(local_codechecker_clean_path($fullpath), $xml, array_keys($ignores));
list($numerrors, $numwarnings) = local_codechecker_count_problems($xml);
[$numerrors, $numwarnings] = local_codechecker_count_problems($xml);

// Output the results report.
echo $output->report($xml, $numerrors, $numwarnings, $showstandard);
Expand Down
29 changes: 19 additions & 10 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ protected function definition() {
$mform = $this->_form;

$a = new stdClass();
$a->link = html_writer::link('https://moodledev.io/general/development/policies/codingstyle',
get_string('moodlecodingguidelines', 'local_codechecker'));
$a->link = html_writer::link(
'https://moodledev.io/general/development/policies/codingstyle',
get_string('moodlecodingguidelines', 'local_codechecker')
);
$a->path = html_writer::tag('tt', 'local/codechecker');
$a->excludeexample = html_writer::tag('tt', 'db, backup/*1, *lib*');
$mform->addElement('static', '', '', get_string('info', 'local_codechecker', $a));
Expand Down Expand Up @@ -84,15 +86,19 @@ function autoload_tools(): bool {
$vendordir = '';
$phpcsdir = '';
// Verify that we have the vendor directory at hand (it contains all the tools).
if (is_dir(__DIR__ . '/vendor') &&
file_exists(__DIR__ . '/vendor/autoload.php')) {
if (
is_dir(__DIR__ . '/vendor') &&
file_exists(__DIR__ . '/vendor/autoload.php')
) {
$vendordir = __DIR__ . '/vendor';
} else {
return false;
}
// Verify that the vendor dir has phpcs installed.
if (is_dir($vendordir . '/squizlabs/php_codesniffer') &&
file_exists($vendordir . '/squizlabs/php_codesniffer/autoload.php')) {
if (
is_dir($vendordir . '/squizlabs/php_codesniffer') &&
file_exists($vendordir . '/squizlabs/php_codesniffer/autoload.php')
) {
$phpcsdir = $vendordir . '/squizlabs/php_codesniffer';
} else {
return false;
Expand Down Expand Up @@ -135,7 +141,7 @@ function local_codesniffer_get_ignores($extraignorelist = '') {
foreach ($plugintypes as $type => $ignored) {
$plugins = core_component::get_plugin_list_with_file($type, 'thirdpartylibs.xml', false);
foreach ($plugins as $plugin => $path) {
$files[$type.'_'.$plugin] = $path;
$files[$type . '_' . $plugin] = $path;
}
}
}
Expand Down Expand Up @@ -170,8 +176,11 @@ function local_codesniffer_get_ignores($extraignorelist = '') {
}

// Manually add our own phpcs stuff to be excluded.
$paths[] = preg_quote(local_codechecker_clean_path(
'/local/codechecker' . DIRECTORY_SEPARATOR . 'phpcs'));
$paths[] = preg_quote(
local_codechecker_clean_path(
'/local/codechecker' . DIRECTORY_SEPARATOR . 'phpcs'
)
);

// Changed in PHP_CodeSniffer 1.4.4 and upwards, so we apply the
// same here: Paths go to keys and mark all them as 'absolute'.
Expand Down Expand Up @@ -324,7 +333,7 @@ function local_codechecker_find_other_files(&$arr, $folder, $ignores, $extension
* @param string $key key within language file ('other_' will be prepended)
* @param bool $warning if true is warning, otherwise error
*/
function local_codechecker_add_problem($fileinxml, $file, $line, $key, $warning=false) {
function local_codechecker_add_problem($fileinxml, $file, $line, $key, $warning = false) {
$type = $warning ? 'warning' : 'error';
$counter = $warning ? 'warnings' : 'errors';

Expand Down
9 changes: 3 additions & 6 deletions readme_moodle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ To update any component:
3. Switch to the lowest PHP version supported by the Moodle version required.
4. Run `composer install` (to install everything).a
5. Update `thirdpartylibs.xml` to annotate the new versions of the tools.
6. Remove `vendor/moodlehq/moodle-cs/moodle/Tests`. Note that this step
will be removed once https://github.com/moodlehq/moodle-cs/pull/69
is released and we update to that version.
7. Commit changes with details about the tools updated.
8. Test, test, test.
9. Optionally, release.
6. Commit changes with details about the tools updated.
7. Test, test, test.
8. Optionally, release.

At some point we may want to make the process above automated, so every time
that a new moodle-cs package is released, everything above (1-8) happens automatically.
Expand Down
81 changes: 59 additions & 22 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,23 @@ public function summary_start($numfiles) {
*/
public function summary_line($fileindex, $prettypath, $numproblems) {
if ($numproblems) {
return html_writer::tag('li', html_writer::link(new moodle_url('#file' . $fileindex),
get_string('filesummary', 'local_codechecker',
['path' => s($prettypath), 'count' => $numproblems])),
['class' => 'fail']);
return html_writer::tag(
'li',
html_writer::link(
new moodle_url(
'#file' . $fileindex
),
get_string(
'filesummary',
'local_codechecker',
[
'path' => s($prettypath),
'count' => $numproblems,
]
),
),
['class' => 'fail']
);
} else {
return html_writer::tag('li', s($prettypath), ['class' => 'good']);
}
Expand All @@ -70,11 +83,17 @@ public function summary_line($fileindex, $prettypath, $numproblems) {
public function summary_end($numfiles, $summary, $type) {
$output = html_writer::end_tag('ul');
if ($summary) {
$output .= html_writer::tag('h2', get_string('summary', 'local_codechecker',
$summary), ['class' => $type]);
$output .= html_writer::tag(
'h2',
get_string('summary', 'local_codechecker', $summary),
['class' => $type]
);
} else {
$output .= html_writer::tag('h2', get_string('success', 'local_codechecker'),
['class' => 'good']);
$output .= html_writer::tag(
'h2',
get_string('success', 'local_codechecker'),
['class' => 'good']
);
}
return $output;
}
Expand All @@ -85,8 +104,9 @@ public function summary_end($numfiles, $summary, $type) {
* @return string HTML to output.
*/
public function invald_path_message($path) {
return $this->output->notification(get_string(
'invalidpath', 'local_codechecker', s($path)));
return $this->output->notification(
get_string('invalidpath', 'local_codechecker', s($path))
);
}

/**
Expand All @@ -104,8 +124,11 @@ public function report(SimpleXMLElement $xml, $numerrors, $numwarnings, $showsta
$grandsummary = '';
$grandtype = '';
if ($numerrors + $numwarnings > 0) {
$grandsummary = get_string('numerrorswarnings', 'local_codechecker',
['errors' => $numerrors, 'warnings' => $numwarnings]);
$grandsummary = get_string(
'numerrorswarnings',
'local_codechecker',
['errors' => $numerrors, 'warnings' => $numwarnings]
);
if ($numerrors) {
$grandtype = 'fail error';
} else {
Expand Down Expand Up @@ -172,11 +195,18 @@ public function report(SimpleXMLElement $xml, $numerrors, $numwarnings, $showsta
* @return string HTML to output.
*/
public function problems($fileindex, $fileinxml, $prettypath) {
$output = html_writer::start_tag('div',
['class' => 'resultfile', 'id' => 'file' . $fileindex]);
$output .= html_writer::tag('h3', html_writer::link(
$output = html_writer::start_tag(
'div',
['class' => 'resultfile', 'id' => 'file' . $fileindex]
);
$output .= html_writer::tag(
'h3',
html_writer::link(
new moodle_url('/local/codechecker/', ['path' => $prettypath]),
s($prettypath), ['title' => get_string('recheckfile', 'local_codechecker')]));
s($prettypath),
['title' => get_string('recheckfile', 'local_codechecker')]
)
);
$output .= html_writer::start_tag('ul');

foreach ($fileinxml->xpath('error|warning') as $problem) {
Expand Down Expand Up @@ -204,12 +234,19 @@ public function problem_message($problem, $prettypath) {
$code = '';
if ($lastfileandline !== $prettypath . '#@#' . $line) {
// We have moved to another line, output it.
$code = html_writer::tag('li', html_writer::tag('div',
html_writer::tag('pre', '#' . $line . ': ' . str_replace(
array_keys($this->replaces),
array_values($this->replaces),
s(local_codechecker_get_line_of_code($line, $prettypath))
))),
$code = html_writer::tag(
'li',
html_writer::tag(
'div',
html_writer::tag(
'pre',
'#' . $line . ': ' . str_replace(
array_keys($this->replaces),
array_values($this->replaces),
s(local_codechecker_get_line_of_code($line, $prettypath))
)
)
),
['class' => 'sourcecode']
);
$lastfileandline = $prettypath . '#@#' . $line;
Expand Down
17 changes: 12 additions & 5 deletions run.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@
}

// Get the command-line options.
list($options, $unrecognized) = cli_get_params(
['help' => false, 'interactive' => false, 'exclude' => ''],
['h' => 'help',
'i' => 'interactive',
'e' => 'exclude', ]);
[$options, $unrecognized] = cli_get_params(
[
'help' => false,
'interactive' => false,
'exclude' => '',
],
[
'h' => 'help',
'i' => 'interactive',
'e' => 'exclude',
]
);

if (count($unrecognized) != 1) {
$options['help'] = true;
Expand Down
9 changes: 7 additions & 2 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
defined('MOODLE_INTERNAL') || die;

if ($hassiteconfig) { // Needs this condition or there is error on login page.
$ADMIN->add('development', new admin_externalpage('local_codechecker',
$ADMIN->add(
'development',
new admin_externalpage(
'local_codechecker',
get_string('pluginname', 'local_codechecker'),
new moodle_url('/local/codechecker/index.php')));
new moodle_url('/local/codechecker/index.php')
)
);
}
10 changes: 7 additions & 3 deletions tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class locallib_test extends \basic_testcase {

/**
* Data provider for test_local_codechecker_find_other_files()
*/
Expand Down Expand Up @@ -110,8 +109,13 @@ public static function local_codechecker_find_other_files_provider(): array {
* @dataProvider local_codechecker_find_other_files_provider
* @covers ::local_codechecker_find_other_files
*/
public function test_local_codechecker_find_other_files(string $path, array $ignores,
array $extensions, array $matches, array $nomatches) {
public function test_local_codechecker_find_other_files(
string $path,
array $ignores,
array $extensions,
array $matches,
array $nomatches,
) {

global $CFG;
require_once(__DIR__ . '/../locallib.php');
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2023092600;;
$plugin->version = 2023092600;
$plugin->release = '5.0.0';
$plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2019111803; // Moodle 3.8.3 release and upwards (first one supporting PHP 7.4).
Expand Down

0 comments on commit 7fd838a

Please sign in to comment.