Skip to content

Commit b0d397e

Browse files
Allow shorthand usage in configuration files
1 parent 886eaef commit b0d397e

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

src/Runner/Action/PHP.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use CaptainHook\App\Hook\Constrained;
2020
use CaptainHook\App\Hook\EventSubscriber;
2121
use CaptainHook\App\Runner\Action as ActionRunner;
22+
use CaptainHook\App\Runner\Shorthand;
2223
use Error;
2324
use Exception;
2425
use RuntimeException;
@@ -40,13 +41,13 @@ class PHP implements ActionRunner
4041
*
4142
* @var string
4243
*/
43-
private $hook;
44+
private string $hook;
4445

4546
/**
4647
*
4748
* @var \CaptainHook\App\Event\Dispatcher
4849
*/
49-
private $dispatcher;
50+
private Dispatcher $dispatcher;
5051

5152
/**
5253
* PHP constructor.
@@ -71,7 +72,7 @@ public function __construct(string $hook, Dispatcher $dispatcher)
7172
*/
7273
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
7374
{
74-
$class = $action->getAction();
75+
$class = $this->getActionClass($action->getAction());
7576

7677
try {
7778
// if the configured action is a static php method display the captured output and exit
@@ -80,7 +81,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
8081
return;
8182
}
8283

83-
// if not static it has to be an 'Action' so let's instantiate
84+
// if not static, it has to be an 'Action' so let's instantiate
8485
$exe = $this->createAction($class);
8586
// check for any given restrictions
8687
if (!$this->isApplicable($exe)) {
@@ -170,4 +171,15 @@ private function isApplicable(Action $action)
170171
}
171172
return true;
172173
}
174+
175+
/**
176+
* Make sure action shorthands are translated before instantiating
177+
*
178+
* @param string $action
179+
* @return string
180+
*/
181+
private function getActionClass(string $action): string
182+
{
183+
return Shorthand::isShorthand($action) ? Shorthand::getActionClass($action) : $action;
184+
}
173185
}

src/Runner/Condition.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private function createCondition(Config\Condition $config): ConditionInterface
109109
}
110110

111111
/** @var class-string<\CaptainHook\App\Hook\Condition> $class */
112-
$class = $config->getExec();
112+
$class = $this->getConditionClass($config->getExec());
113113
if (!class_exists($class)) {
114114
throw new RuntimeException('could not find condition class: ' . $class);
115115
}
@@ -165,4 +165,15 @@ private function isLogicCondition(Config\Condition $config): bool
165165
{
166166
return in_array(strtolower($config->getExec()), ['and', 'or']);
167167
}
168+
169+
/**
170+
* Returns the condition class
171+
*
172+
* @param string $exec
173+
* @return string
174+
*/
175+
private function getConditionClass(string $exec): string
176+
{
177+
return Shorthand::isShorthand($exec) ? Shorthand::getConditionClass($exec) : $exec;
178+
}
168179
}

src/Runner/Util.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public static function isTypeValid(string $type): bool
4545
*/
4646
public static function getExecType(string $action): string
4747
{
48-
return substr($action, 0, 1) === '\\' ? 'php' : 'cli';
48+
return self::isPHPType($action) ? 'php' : 'cli';
49+
}
50+
51+
/**
52+
* Check if the action type is PHP
53+
*
54+
* @param string $action
55+
* @return bool
56+
*/
57+
private static function isPHPType(string $action): bool
58+
{
59+
return str_starts_with($action, '\\') || Shorthand::isShorthand($action);
4960
}
5061
}

tests/unit/Runner/Action/PHPTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,27 @@ public function testExecuteError(): void
140140
$php->execute($config, $io, $repo, $action);
141141
}
142142

143+
/**
144+
* Check if the action shorthand works
145+
*/
146+
public function testExecuteByShorthand(): void
147+
{
148+
$this->expectException(Exception::class);
149+
$this->expectExceptionMessageMatches('/debugging/i');
150+
151+
$config = $this->createConfigMock();
152+
$io = $this->createIOMock();
153+
$repo = $this->createRepositoryMock();
154+
$action = $this->createActionConfigMock();
155+
$events = new Dispatcher($io, $config, $repo);
156+
$class = 'CaptainHook.Debug.fail';
157+
158+
$action->expects($this->once())->method('getAction')->willReturn($class);
159+
160+
$php = new PHP('pre-commit', $events);
161+
$php->execute($config, $io, $repo, $action);
162+
}
163+
143164
/**
144165
* Tests PHP::execute
145166
*

0 commit comments

Comments
 (0)