Skip to content

Commit

Permalink
Remove filephpdocpresent and classesdocumented
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 13, 2024
1 parent 90c24e5 commit d06cebd
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 165 deletions.
5 changes: 0 additions & 5 deletions lang/en/local_moodlecheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@

$string['error_emptynophpfile'] = 'The file is empty or doesn\'t contain PHP code. Skipped.';

$string['rule_filephpdocpresent'] = 'File-level phpdocs block is present';
$string['error_filephpdocpresent'] = 'File-level phpdocs block is not found';

$string['rule_classesdocumented'] = 'All classes are documented';
$string['error_classesdocumented'] = 'Class <b>{$a->class}</b> is not documented';
$string['rule_functionsdocumented'] = 'All functions are documented';
$string['error_functionsdocumented'] = 'Function <b>{$a->function}</b> is not documented';
$string['rule_variablesdocumented'] = 'All variables are documented';
Expand Down
43 changes: 0 additions & 43 deletions rules/phpdocs_basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

defined('MOODLE_INTERNAL') || die;

local_moodlecheck_registry::add_rule('filephpdocpresent')->set_callback('local_moodlecheck_filephpdocpresent');
local_moodlecheck_registry::add_rule('classesdocumented')->set_callback('local_moodlecheck_classesdocumented');
local_moodlecheck_registry::add_rule('functionsdocumented')->set_callback('local_moodlecheck_functionsdocumented');
local_moodlecheck_registry::add_rule('variablesdocumented')->set_callback('local_moodlecheck_variablesdocumented');
local_moodlecheck_registry::add_rule('constsdocumented')->set_callback('local_moodlecheck_constsdocumented');
Expand All @@ -47,47 +45,6 @@
local_moodlecheck_registry::add_rule('phpdocsuncurlyinlinetag')->set_callback('local_moodlecheck_phpdocsuncurlyinlinetag');
local_moodlecheck_registry::add_rule('phpdoccontentsinlinetag')->set_callback('local_moodlecheck_phpdoccontentsinlinetag');

/**
* Checks if file-level phpdocs block is present
*
* @param local_moodlecheck_file $file
* @return array of found errors
*/
function local_moodlecheck_filephpdocpresent(local_moodlecheck_file $file) {
// This rule doesn't apply if the file is 1-artifact file (see #66).
$artifacts = $file->get_artifacts();
if (count($artifacts[T_CLASS]) + count($artifacts[T_INTERFACE]) + count($artifacts[T_TRAIT]) === 1) {
return [];
}
if ($file->find_file_phpdocs() === false) {
$tokens = &$file->get_tokens();
for ($i = 0; $i < 90; $i++) {
if (isset($tokens[$i]) && !in_array($tokens[$i][0], [T_OPEN_TAG, T_WHITESPACE, T_COMMENT])) {
return [['line' => $file->get_line_number($i)]];
}
}
// For some reason we cound not find the line number.
return [['line' => '']];
}
return [];
}

/**
* Checks if all classes have phpdocs blocks
*
* @param local_moodlecheck_file $file
* @return array of found errors
*/
function local_moodlecheck_classesdocumented(local_moodlecheck_file $file) {
$errors = [];
foreach ($file->get_classes() as $class) {
if ($class->phpdocs === false) {
$errors[] = ['class' => $class->name, 'line' => $file->get_line_number($class->boundaries[0])];
}
}
return $errors;
}

/**
* Checks if all functions have phpdocs blocks.
*
Expand Down
117 changes: 0 additions & 117 deletions tests/moodlecheck_rules_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,51 +71,6 @@ public function test_constantclass(): void {

// TODO: Change to DOMNodeList::count() when php71 support is gone.
$this->assertSame(0, $found->length);

// Also verify that contents do not include any problem with line 42 / classesdocumented. Use simple string matching here.
$this->assertStringNotContainsString('line="42"', $result);
$this->assertStringNotContainsString('classesdocumented', $result);
}

/**
* Assert that the file block is required for old files, and not for 1-artifact ones.
*
* @covers ::local_moodlecheck_filephpdocpresent
*/
public function test_file_block_required(): void {
global $PAGE;

$output = $PAGE->get_renderer('local_moodlecheck');

// A file with multiple classes, require the file phpdoc block.
$path = new local_moodlecheck_path('local/moodlecheck/tests/fixtures/phpdoc_file_required_yes1.php', null);
$result = $output->display_path($path, 'xml');
$this->assertStringContainsString('File-level phpdocs block is not found', $result);

// A file without any class (library-like), require the file phpdoc block.
$path = new local_moodlecheck_path('local/moodlecheck/tests/fixtures/phpdoc_file_required_yes2.php', null);
$result = $output->display_path($path, 'xml');
$this->assertStringContainsString('File-level phpdocs block is not found', $result);

// A file with one interface and one trait, require the file phpdoc block.
$path = new local_moodlecheck_path('local/moodlecheck/tests/fixtures/phpdoc_file_required_yes3.php', null);
$result = $output->display_path($path, 'xml');
$this->assertStringContainsString('File-level phpdocs block is not found', $result);

// A file with only one class, do not require the file phpdoc block.
$path = new local_moodlecheck_path('local/moodlecheck/tests/fixtures/phpdoc_file_required_no1.php', null);
$result = $output->display_path($path, 'xml');
$this->assertStringNotContainsString('File-level phpdocs block is not found', $result);

// A file with only one interface, do not require the file phpdoc block.
$path = new local_moodlecheck_path('local/moodlecheck/tests/fixtures/phpdoc_file_required_no2.php', null);
$result = $output->display_path($path, 'xml');
$this->assertStringNotContainsString('File-level phpdocs block is not found', $result);

// A file with only one trait, do not require the file phpdoc block.
$path = new local_moodlecheck_path('local/moodlecheck/tests/fixtures/phpdoc_file_required_no3.php', null);
$result = $output->display_path($path, 'xml');
$this->assertStringNotContainsString('File-level phpdocs block is not found', $result);
}

/**
Expand Down Expand Up @@ -361,76 +316,6 @@ public function test_phpdoc_tags_inline(): void {
$this->assertStringNotContainsString('ba8by}', $result);
}

/**
* Verify that anonymous classes do not require phpdoc class blocks.
*
* @dataProvider anonymous_class_provider
* @param string $path
* @param bool $expectclassesdocumentedfail Whether the
*
* @covers \local_moodlecheck_file::get_artifacts
*/
public function test_phpdoc_anonymous_class_docblock(string $path, bool $expectclassesdocumentedfail): void {
global $PAGE;

$output = $PAGE->get_renderer('local_moodlecheck');
$checkpath = new local_moodlecheck_path($path, null);
$result = $output->display_path($checkpath, 'xml');

if ($expectclassesdocumentedfail) {
$this->assertStringContainsString('classesdocumented', $result);
} else {
$this->assertStringNotContainsString('classesdocumented', $result);
}
}

/**
* Data provider for anonymous classes tests.
*
* @return array
*/
public static function anonymous_class_provider(): array {
$rootpath = 'local/moodlecheck/tests/fixtures/anonymous';
return [
'return new class {' => [
"{$rootpath}/anonymous.php",
false,
],
'return new class extends parentclass {' => [
"{$rootpath}/extends.php",
false,
],
'return new class implements someinterface {' => [
"{$rootpath}/implements.php",
false,
],
'return new class extends parentclass implements someinterface {' => [
"{$rootpath}/extendsandimplements.php",
false,
],
'return new class (with params) {' => [
"{$rootpath}/anonymous_with_params.php",
false,
],
'return new class (with params) extends parentclass {' => [
"{$rootpath}/extends_with_params.php",
false,
],
'return new class (with params) implements someinterface {' => [
"{$rootpath}/implements_with_params.php",
false,
],
'$value = new class {' => [
"{$rootpath}/assigned.php",
false,
],
'class someclass extends parentclass {' => [
"{$rootpath}/named.php",
true,
],
];
}

/**
* Verify that empty files and files without PHP aren't processed
*
Expand Down Expand Up @@ -613,7 +498,6 @@ public function test_text_format_errors_and_warnings(): void {
$result = $output->display_path($path, 'text');

$this->assertStringContainsString('tests/fixtures/error_and_warning.php', $result);
$this->assertStringContainsString('11: Class someclass is not documented (error)', $result);
$this->assertStringContainsString('12: Function someclass::somefunc is not documented (warning)', $result);
}

Expand All @@ -631,7 +515,6 @@ public function test_html_format_errors_and_warnings(): void {
$result = $output->display_path($path, 'html');

$this->assertStringContainsString('tests/fixtures/error_and_warning.php</span>', $result);
$this->assertStringContainsString('<b>11</b>: Class <b>someclass</b> is not documented (error)', $result);
$this->assertStringContainsString('<b>12</b>: Function <b>someclass::somefunc</b> is not documented (warning)', $result);
}
}

0 comments on commit d06cebd

Please sign in to comment.