forked from moodlehq/moodle-plugin-ci
-
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.
Implement getRequiredFunctionCalls check and use it in filter plugin …
…validation. This address scenario where file is supposed to contain certain function call, such as `class_alias` in Filter plugin type backward compatibility support per https://moodledev.io/docs/4.5/devupdate#filter-plugins The patch makes possible for deleveloper to specify: * getRequiredFunctionCalls to make sure file contains function call as name suggests. * FileTokens::notFoundHint to give some context for requirement to improve developer experience. This works with FileTokens in any other validation methods.
- Loading branch information
Showing
10 changed files
with
170 additions
and
0 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
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,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Moodle Plugin CI package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Copyright (c) 2024 Moodle Pty Ltd <[email protected]> | ||
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace MoodlePluginCI\PluginValidate\Finder; | ||
|
||
use PhpParser\Node\Name; | ||
|
||
/** | ||
* Finds function call. | ||
*/ | ||
class FunctionCallFinder extends AbstractParserFinder | ||
{ | ||
public function getType(): string | ||
{ | ||
return 'function call'; | ||
} | ||
|
||
public function findTokens($file, FileTokens $fileTokens): void | ||
{ | ||
$statements = $this->parser->parseFile($file); | ||
|
||
foreach ($this->filter->filterFunctionCalls($statements) as $funccall) { | ||
if ($funccall->name instanceof Name) { | ||
$fileTokens->compare((string) $funccall->name); | ||
} | ||
} | ||
} | ||
} |
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
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
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,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Moodle Plugin CI package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* Copyright (c) 2024 Moodle Pty Ltd <[email protected]> | ||
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace MoodlePluginCI\Tests\PluginValidate\Finder; | ||
|
||
use MoodlePluginCI\PluginValidate\Finder\FileTokens; | ||
use MoodlePluginCI\PluginValidate\Finder\FunctionCallFinder; | ||
|
||
class FunctionCallFinderTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function testFindTokens() | ||
{ | ||
$file = __DIR__ . '/../../Fixture/moodle-local_ci/lib.php'; | ||
$fileTokens = FileTokens::create('lib.php')->mustHave('local_ci_subtract'); | ||
|
||
$finder = new FunctionCallFinder(); | ||
$finder->findTokens($file, $fileTokens); | ||
|
||
$this->assertTrue($fileTokens->hasFoundAllTokens()); | ||
$this->assertFalse($fileTokens->hasHint()); | ||
} | ||
|
||
public function testFindTokensNotFound() | ||
{ | ||
$file = __DIR__ . '/../../Fixture/moodle-local_ci/lib.php'; | ||
$fileTokens = FileTokens::create('lib.php')->mustHave('exit')->notFoundHint('Exit not found'); | ||
|
||
$finder = new FunctionCallFinder(); | ||
$finder->findTokens($file, $fileTokens); | ||
|
||
$this->assertFalse($fileTokens->hasFoundAllTokens()); | ||
$this->assertTrue($fileTokens->hasHint()); | ||
$this->assertSame('Exit not found', $fileTokens->hint); | ||
} | ||
} |