Skip to content

Commit

Permalink
Adjust filter plugin requirements to comply with 4.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
kabalin committed Oct 8, 2024
1 parent f722b93 commit 4ad6950
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 12 deletions.
22 changes: 22 additions & 0 deletions src/Parser/StatementFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -116,6 +118,26 @@ public function filterAssignments(array $statements): array
return $assigns;
}

/**
* Extract all the function call expressions from the statements.
*
* @param Stmt[] $statements
*
* @return FuncCall[]
*/
public function filterFunctionCalls(array $statements): array
{
$calls = [];
foreach ($statements as $statement) {
// Only expressions that are function calls.
if ($statement instanceof Expression && $statement->expr instanceof FuncCall) {
$calls[] = $statement->expr;
}
}

return $calls;
}

/**
* Find first variable assignment with a given name.
*
Expand Down
37 changes: 37 additions & 0 deletions src/PluginValidate/Finder/FunctionCallFinder.php
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) 2018 Blackboard Inc. (http://www.blackboard.com)
* 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);
}
}
}
}
2 changes: 2 additions & 0 deletions src/PluginValidate/PluginValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use MoodlePluginCI\PluginValidate\Finder\ClassFinder;
use MoodlePluginCI\PluginValidate\Finder\FileTokens;
use MoodlePluginCI\PluginValidate\Finder\FinderInterface;
use MoodlePluginCI\PluginValidate\Finder\FunctionCallFinder;
use MoodlePluginCI\PluginValidate\Finder\FunctionFinder;
use MoodlePluginCI\PluginValidate\Finder\LangFinder;
use MoodlePluginCI\PluginValidate\Finder\TableFinder;
Expand Down Expand Up @@ -115,6 +116,7 @@ public function verifyRequirements(): void
$this->findRequiredTokens(new TableFinder(), [$this->requirements->getRequiredTables()]);
$this->findRequiredTokens(new TablePrefixFinder(), [$this->requirements->getRequiredTablePrefix()]);
$this->findRequiredTokens(new BehatTagFinder(), $this->requirements->getRequiredBehatTags());
$this->findRequiredTokens(new FunctionCallFinder(), $this->requirements->getRequiredFunctionCalls());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/PluginValidate/Requirements/AbstractRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ protected function behatTagsFactory(array $tags): array
return $fileTokens;
}

/**
* Required function calls.
*
* @return FileTokens[]
*/
abstract public function getRequiredFunctionCalls(): array;

/**
* An array of required files, paths are relative to the plugin directory.
*
Expand Down
28 changes: 24 additions & 4 deletions src/PluginValidate/Requirements/FilterRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,40 @@ class FilterRequirements extends GenericRequirements
{
public function getRequiredFiles(): array
{
return array_merge(parent::getRequiredFiles(), [
'filter.php',
]);
$files = ['filter.php'];
if ($this->moodleVersion >= 405) {
$files[] = 'classes/text_filter.php';
}

return array_merge(parent::getRequiredFiles(), $files);
}

public function getRequiredClasses(): array
{
if ($this->moodleVersion <= 404) {
return [
FileTokens::create('filter.php')->mustHave('filter_' . $this->plugin->name),
];
}

return [
FileTokens::create('filter.php')->mustHave('filter_' . $this->plugin->name),
FileTokens::create('classes/text_filter.php')->mustHave("filter_{$this->plugin->name}\\text_filter"),
];
}

public function getRequiredStrings(): FileTokens
{
return FileTokens::create($this->getLangFile())->mustHave('filtername');
}

public function getRequiredFunctionCalls(): array
{
if ($this->moodleVersion <= 404) {
return [];
}

return [
FileTokens::create('filter.php')->mustHave('class_alia'),
];
}
}
5 changes: 5 additions & 0 deletions src/PluginValidate/Requirements/GenericRequirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function getRequiredClasses(): array
return [];
}

public function getRequiredFunctionCalls(): array
{
return [];
}

public function getRequiredStrings(): FileTokens
{
return FileTokens::create($this->getLangFile())->mustHave('pluginname');
Expand Down
39 changes: 31 additions & 8 deletions tests/PluginValidate/Requirements/FilterRequirementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class FilterRequirementsTest extends \PHPUnit\Framework\TestCase

protected function setUp(): void
{
$this->requirements = new FilterRequirements(new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 29);
$this->requirements404 = new FilterRequirements(new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 404);
$this->requirements = new FilterRequirements(new Plugin('filter_activitynames', 'filter', 'activitynames', ''), 405);
}

protected function tearDown(): void
{
$this->requirements = null;
$this->requirements404 = null;
$this->requirements = null;
}

public function testResolveRequirements()
Expand All @@ -39,29 +41,50 @@ public function testResolveRequirements()

$this->assertInstanceOf(
'MoodlePluginCI\PluginValidate\Requirements\FilterRequirements',
$resolver->resolveRequirements(new Plugin('', 'filter', '', ''), 29)
$resolver->resolveRequirements(new Plugin('', 'filter', '', ''), 404)
);
}

public function testGetRequiredFiles404()
{
$files = $this->requirements404->getRequiredFiles();

$this->assertTrue(in_array('filter.php', $files, true));
$this->assertFalse(in_array('classes/text_filter.php', $files, true));
foreach ($files as $file) {
$this->assertIsString($file);
}
}

public function testGetRequiredFiles()
{
$files = $this->requirements->getRequiredFiles();

$this->assertNotEmpty($files);
$this->assertTrue(in_array('filter.php', $files, true));
$this->assertTrue(in_array('classes/text_filter.php', $files, true));
foreach ($files as $file) {
$this->assertIsString($file);
}
}

public function testGetRequiredClasses404()
{
$classes = $this->requirements404->getRequiredClasses();

$this->assertCount(1, $classes);
$class = reset($classes);
$this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class);
$this->assertSame('filter.php', $class->file);
}

public function testGetRequiredClasses()
{
$classes = $this->requirements->getRequiredClasses();

$this->assertNotEmpty($classes);
foreach ($classes as $class) {
$this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class);
}
$this->assertCount(1, $classes);
$class = reset($classes);
$this->assertInstanceOf('MoodlePluginCI\PluginValidate\Finder\FileTokens', $class);
$this->assertSame('classes/text_filter.php', $class->file);
}

public function testGetRequiredStrings()
Expand Down

0 comments on commit 4ad6950

Please sign in to comment.