Skip to content

Commit d7be382

Browse files
authored
Merge pull request #118 from stronk7/prevent_check_when_no_commponent
Disable package checks whenever missing a target component
2 parents f71d22b + f06fa34 commit d7be382

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

moodle/Sniffs/Commenting/PackageSniff.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ protected function checkDocblock(
144144
$objectType = $this->getObjectType($phpcsFile, $stackPtr);
145145
$expectedPackage = MoodleUtil::getMoodleComponent($phpcsFile, true);
146146

147+
// Nothing to do if we have been unable to determine the package
148+
// (all the following checks rely on this value).
149+
if ($expectedPackage === null) {
150+
return false;
151+
}
152+
147153
$packageTokens = Docblocks::getMatchingDocTags($phpcsFile, $stackPtr, '@package');
148154
if (empty($packageTokens)) {
149155
$fix = $phpcsFile->addFixableError(

moodle/Tests/Sniffs/Commenting/PackageSniffTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@
2929
*/
3030
class PackageSniffTest extends MoodleCSBaseTestCase
3131
{
32+
/**
33+
* Test that various checks are not performed when there isn't any component available.
34+
*/
35+
public function testPackageOnMissingComponent(): void {
36+
$this->setStandard('moodle');
37+
$this->setSniff('moodle.Commenting.Package');
38+
$this->setFixture(__DIR__ . '/fixtures/package_tags_nocheck.php');
39+
$this->setComponentMapping([]); // No components available.
40+
41+
$this->setWarnings([]);
42+
$this->setErrors([
43+
// These are still checked because this doesn't depend on the - missing - component mapping.
44+
35 => 'Missing doc comment for class missing_docblock_in_class',
45+
38 => 'Missing doc comment for interface missing_docblock_in_interface',
46+
41 => 'Missing doc comment for trait missing_docblock_in_trait',
47+
44 => 'Missing doc comment for function missing_docblock_in_function',
48+
]);
49+
50+
$this->verifyCsResults();
51+
}
52+
3253
/**
3354
* @dataProvider packageCorrectnessProvider
3455
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\PHPUnit;
4+
5+
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures.
6+
7+
// Wrong components are not reported because the expected component is needed and we don't know it
8+
9+
/**
10+
* @package wrong_package
11+
*/
12+
class package_wrong_in_class {
13+
}
14+
15+
/**
16+
* @package wrong_package
17+
*/
18+
interface package_wrong_in_interface {
19+
}
20+
21+
/**
22+
* @package wrong_package
23+
*/
24+
interface package_wrong_in_trait {
25+
}
26+
27+
/**
28+
* @package wrong_package
29+
*/
30+
function package_wrong_in_function(): void {
31+
}
32+
33+
// All these (missing) continue being reported because the expected component is not needed.
34+
35+
class missing_docblock_in_class {
36+
}
37+
38+
interface missing_docblock_in_interface {
39+
}
40+
41+
trait missing_docblock_in_trait {
42+
}
43+
44+
function missing_docblock_in_function(): void {
45+
return;
46+
}
47+

0 commit comments

Comments
 (0)