Skip to content

Commit

Permalink
Merge pull request #118 from stronk7/prevent_check_when_no_commponent
Browse files Browse the repository at this point in the history
Disable package checks whenever missing a target component
  • Loading branch information
andrewnicols authored Mar 8, 2024
2 parents f71d22b + f06fa34 commit d7be382
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions moodle/Sniffs/Commenting/PackageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ protected function checkDocblock(
$objectType = $this->getObjectType($phpcsFile, $stackPtr);
$expectedPackage = MoodleUtil::getMoodleComponent($phpcsFile, true);

// Nothing to do if we have been unable to determine the package
// (all the following checks rely on this value).
if ($expectedPackage === null) {
return false;
}

$packageTokens = Docblocks::getMatchingDocTags($phpcsFile, $stackPtr, '@package');
if (empty($packageTokens)) {
$fix = $phpcsFile->addFixableError(
Expand Down
21 changes: 21 additions & 0 deletions moodle/Tests/Sniffs/Commenting/PackageSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@
*/
class PackageSniffTest extends MoodleCSBaseTestCase
{
/**
* Test that various checks are not performed when there isn't any component available.
*/
public function testPackageOnMissingComponent(): void {
$this->setStandard('moodle');
$this->setSniff('moodle.Commenting.Package');
$this->setFixture(__DIR__ . '/fixtures/package_tags_nocheck.php');
$this->setComponentMapping([]); // No components available.

$this->setWarnings([]);
$this->setErrors([
// These are still checked because this doesn't depend on the - missing - component mapping.
35 => 'Missing doc comment for class missing_docblock_in_class',
38 => 'Missing doc comment for interface missing_docblock_in_interface',
41 => 'Missing doc comment for trait missing_docblock_in_trait',
44 => 'Missing doc comment for function missing_docblock_in_function',
]);

$this->verifyCsResults();
}

/**
* @dataProvider packageCorrectnessProvider
*/
Expand Down
47 changes: 47 additions & 0 deletions moodle/Tests/Sniffs/Commenting/fixtures/package_tags_nocheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;

defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures.

// Wrong components are not reported because the expected component is needed and we don't know it

/**
* @package wrong_package
*/
class package_wrong_in_class {
}

/**
* @package wrong_package
*/
interface package_wrong_in_interface {
}

/**
* @package wrong_package
*/
interface package_wrong_in_trait {
}

/**
* @package wrong_package
*/
function package_wrong_in_function(): void {
}

// All these (missing) continue being reported because the expected component is not needed.

class missing_docblock_in_class {
}

interface missing_docblock_in_interface {
}

trait missing_docblock_in_trait {
}

function missing_docblock_in_function(): void {
return;
}

0 comments on commit d7be382

Please sign in to comment.