Skip to content

Commit

Permalink
Add ability to ignore methods using regular expressions. (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo committed Oct 3, 2020
1 parent 9e8b241 commit 272db62
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/AnnotatedCommandFactory.php
Expand Up @@ -43,6 +43,9 @@ class AnnotatedCommandFactory implements AutomaticOptionsProviderInterface
/** var SimpleCacheInterface */
protected $dataStore;

/** var string[] */
protected $ignoredCommandsRegexps = [];

public function __construct()
{
$this->dataStore = new NullCache();
Expand Down Expand Up @@ -113,6 +116,18 @@ public function addListernerCallback(callable $listener)
return $this;
}

/**
* Add a regular expresion used to match methods names
* that will not be part of the final set of commands.
*
* @param string $regex
*/
public function addIgnoredCommandsRegexp(string $regex)
{
$this->ignoredCommandsRegexps[] = $regex;
return $this;
}

/**
* Call all command creation listeners
*
Expand Down Expand Up @@ -286,7 +301,7 @@ public function createCommandsFromClassInfo($commandInfoList, $commandFileInstan
$commandInfoList,
$commandFileInstance,
function ($commandInfo) use ($includeAllPublicMethods) {
return static::isCommandMethod($commandInfo, $includeAllPublicMethods);
return $this->isMethodRecognizedAsCommand($commandInfo, $includeAllPublicMethods);
}
);
}
Expand All @@ -302,6 +317,46 @@ function ($commandInfo) use ($commandFileInstance) {
);
}

protected function isMethodRecognizedAsCommand($commandInfo, $includeAllPublicMethods)
{
// Ignore everything labeled @hook
if ($this->isMethodRecognizedAsHook($commandInfo)) {
return false;
}
// Ignore everything labeled @ignored-command
if ($commandInfo->hasAnnotation('ignored-command')) {
return false;
}
// Include everything labeled @command
if ($commandInfo->hasAnnotation('command')) {
return true;
}
// Skip anything that has a missing or invalid name.
$commandName = $commandInfo->getName();
if (empty($commandName) || preg_match('#[^a-zA-Z0-9:_-]#', $commandName)) {
return false;
}
// Skip anything named like an accessor ('get' or 'set')
if (preg_match('#^(get[A-Z]|set[A-Z])#', $commandInfo->getMethodName())) {
return false;
}

// Skip based on the configured regular expresions
foreach ($this->ignoredCommandsRegexps as $regex) {
if (preg_match($regex, $commandInfo->getMethodName())) {
return false;
}
}

// Default to the setting of 'include all public methods'.
return $includeAllPublicMethods;
}

protected function isMethodRecognizedAsHook($commandInfo)
{
return $commandInfo->hasAnnotation('hook');
}

protected function filterCommandInfoList($commandInfoList, callable $commandSelector)
{
return array_filter($commandInfoList, $commandSelector);
Expand All @@ -312,11 +367,13 @@ public static function isCommandOrHookMethod($commandInfo, $includeAllPublicMeth
return static::isHookMethod($commandInfo) || static::isCommandMethod($commandInfo, $includeAllPublicMethods);
}

// Deprecated: avoid using the isHookMethod in favor of the protected non-static isMethodRecognizedAsHook
public static function isHookMethod($commandInfo)
{
return $commandInfo->hasAnnotation('hook');
}

// Deprecated: avoid using the isCommandMethod in favor of the protected non-static isMethodRecognizedAsCommand
public static function isCommandMethod($commandInfo, $includeAllPublicMethods)
{
// Ignore everything labeled @hook
Expand Down
12 changes: 12 additions & 0 deletions tests/AnnotatedCommandFactoryTest.php
Expand Up @@ -162,6 +162,18 @@ function testIgnoredCommand()
$this->assertArrayNotHasKey('ignoredCommand', $commandList);
}

function testIgnoredCommandByRegex()
{
$this->commandFileInstance = new \Consolidation\TestUtils\ExampleCommandFile;
$this->commandFactory = new AnnotatedCommandFactory();

$commandList = $this->commandFactory->createCommandsFromClass($this->commandFileInstance);
$this->assertArrayHasKey('ignoredCommandByRegex', $commandList);
$this->commandFactory->addIgnoredCommandsRegexp('/ignored.ommand.y.egex/');
$commandList = $this->commandFactory->createCommandsFromClass($this->commandFileInstance);
$this->assertArrayNotHasKey('ignoredCommandByRegex', $commandList);
}

function testOptionWithOptionalValue()
{
$this->commandFileInstance = new \Consolidation\TestUtils\ExampleCommandFile;
Expand Down
4 changes: 4 additions & 0 deletions tests/src/ExampleCommandFile.php
Expand Up @@ -545,4 +545,8 @@ public function globalOptionsOnly($arg, array $options = [])
public function ignoredCommand()
{
}

public function ignoredCommandByRegex()
{
}
}

0 comments on commit 272db62

Please sign in to comment.