forked from moodlehq/moodle-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create FileHas Sniff to check for copyright + license
- Loading branch information
1 parent
f8c2af7
commit 10c69f1
Showing
16 changed files
with
479 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
|
||
// This file is part of Moodle - https://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Commenting; | ||
|
||
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; | ||
|
||
/** | ||
* Test the VariableCommentSniff sniff. | ||
* | ||
* @copyright 2024 onwards Andrew Lyons <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\Commenting\FileHasSniff | ||
*/ | ||
class FileHasSniffTest extends MoodleCSBaseTestCase | ||
{ | ||
/** | ||
* @dataProvider fixtureProvider | ||
*/ | ||
public function testSniffWithFixtures( | ||
string $fixture, | ||
array $errors, | ||
array $warnings | ||
): void { | ||
$this->setStandard('moodle'); | ||
$this->setSniff('moodle.Commenting.FileHas'); | ||
$this->setFixture(sprintf("%s/fixtures/FileHas/%s.php", __DIR__, $fixture)); | ||
$this->setErrors($errors); | ||
$this->setWarnings($warnings); | ||
|
||
$this->verifyCsResults(); | ||
} | ||
|
||
public static function fixtureProvider(): array { | ||
$cases = [ | ||
'Single artifact, Single docblock' => [ | ||
'fixture' => 'single_artifact_single_docblock', | ||
'errors' => [], | ||
'warnings' => [], | ||
], | ||
'Single artifact, Single docblock (missing)' => [ | ||
'fixture' => 'single_artifact_single_docblock_missing', | ||
'errors' => [ | ||
3 => [ | ||
'Missing @copyright tag', | ||
'Missing @license tag', | ||
], | ||
], | ||
'warnings' => [], | ||
], | ||
'Single artifact, Single docblock (missing tags)' => [ | ||
'fixture' => 'single_artifact_single_docblock_missing_tags', | ||
'errors' => [ | ||
6 => [ | ||
'Missing @copyright tag', | ||
'Missing @license tag', | ||
], | ||
], | ||
'warnings' => [], | ||
], | ||
'Single artifact, multiple docblocks' => [ | ||
'fixture' => 'single_artifact_multiple_docblock', | ||
'errors' => [ | ||
], | ||
'warnings' => [], | ||
], | ||
'Single artifact, multiple docblocks (missing)' => [ | ||
'fixture' => 'single_artifact_multiple_docblock_missing', | ||
'errors' => [ | ||
// Note: Covered by MissingDocblockSniff. | ||
], | ||
'warnings' => [], | ||
], | ||
'Single artifact, multiple docblocks (wrong)' => [ | ||
'fixture' => 'single_artifact_multiple_docblock_wrong', | ||
'errors' => [ | ||
], | ||
'warnings' => [ | ||
7 => 'Invalid @license tag. ' | ||
. 'Expected "https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later", ' | ||
. 'found "http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later"', | ||
], | ||
], | ||
'Multiple artifacts, File docblock' => [ | ||
'fixture' => 'multiple_artifact_has_file_docblock', | ||
'errors' => [], | ||
'warnings' => [], | ||
], | ||
'Multiple artifacts, File docblock (missing)' => [ | ||
'fixture' => 'multiple_artifact_has_file_docblock_missing', | ||
'errors' => [], | ||
'warnings' => [], | ||
], | ||
'Multiple artifacts, File docblock (wrong data)' => [ | ||
'fixture' => 'multiple_artifact_has_file_docblock_wrong', | ||
'errors' => [ | ||
3 => 'Missing @copyright tag', | ||
], | ||
'warnings' => [ | ||
6 => 'Invalid @license tag. Expected "https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later", found "Me!"', | ||
], | ||
], | ||
'Multiple artifacts, File docblock (missing tags)' => [ | ||
'fixture' => 'multiple_artifact_has_file_docblock_missing_tags', | ||
'errors' => [ | ||
3 => [ | ||
'Missing @copyright tag', | ||
'Missing @license tag', | ||
], | ||
], | ||
'warnings' => [], | ||
], | ||
]; | ||
|
||
// if (version_compare(PHP_VERSION, '8.0.0') >= 0) { | ||
// // Since PHP 8.0. | ||
// $cases['Constructor Property Promotions'] = [ | ||
// 'fixture' => 'constructor_property_promotion', | ||
// 'errors' => [ | ||
// 7 => 'Missing member variable doc comment', | ||
// 13 => 'Expected "bool" but found "BOOLEAN" for @var tag in member variable comment', | ||
// 20 => 'The @var tag must be the first tag in a member variable commen', | ||
// ], | ||
// 'warnings' => [ | ||
// 19 => '@deprecated tag is not allowed in member variable comment', | ||
// ], | ||
// ]; | ||
// } | ||
|
||
// if (version_compare(PHP_VERSION, '8.1.0') >= 0) { | ||
// // Since PHP 8.1. | ||
// $cases['Constructor Property Promotions with Readonly params'] = [ | ||
// 'fixture' => 'constructor_property_promotion_readonly', | ||
// 'errors' => [ | ||
// 7 => 'Missing member variable doc comment', | ||
// 13 => 'Expected "bool" but found "BOOLEAN" for @var tag in member variable comment', | ||
// 20 => 'The @var tag must be the first tag in a member variable commen', | ||
// ], | ||
// 'warnings' => [ | ||
// 19 => '@deprecated tag is not allowed in member variable comment', | ||
// ], | ||
// ]; | ||
// } | ||
|
||
return $cases; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
moodle/Tests/Sniffs/Commenting/fixtures/FileHas/multiple_artifact_has_file_docblock.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/** | ||
* File docblock | ||
* | ||
* @copyright 2024 Andrew Lyons <[email protected]> | ||
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
/** | ||
* Class docblock. | ||
* | ||
*/ | ||
class multiple_artifact_has_file_docblock | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
} | ||
|
||
trait example_trait {} | ||
interface example_interface {} |
16 changes: 16 additions & 0 deletions
16
.../Tests/Sniffs/Commenting/fixtures/FileHas/multiple_artifact_has_file_docblock_missing.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/** | ||
* Class docblock. | ||
* | ||
*/ | ||
class multiple_artifact_has_file_docblock_missing | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
} | ||
|
||
trait example_trait {} | ||
interface example_interface {} |
20 changes: 20 additions & 0 deletions
20
...s/Sniffs/Commenting/fixtures/FileHas/multiple_artifact_has_file_docblock_missing_tags.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* File docblock | ||
*/ | ||
|
||
/** | ||
* Class docblock. | ||
* | ||
*/ | ||
class multiple_artifact_has_file_docblock_missing_tags | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
} | ||
|
||
trait example_trait {} | ||
interface example_interface {} |
22 changes: 22 additions & 0 deletions
22
...le/Tests/Sniffs/Commenting/fixtures/FileHas/multiple_artifact_has_file_docblock_wrong.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* File docblock | ||
* | ||
* @license Me! | ||
*/ | ||
|
||
/** | ||
* Class docblock. | ||
* | ||
*/ | ||
class multiple_artifact_has_file_docblock_wrong | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
} | ||
|
||
trait example_trait {} | ||
interface example_interface {} |
Oops, something went wrong.