1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers; |
4
|
|
|
|
5
|
|
|
use Consolidation\AnnotatedCommand\CommandData; |
6
|
|
|
use Consolidation\AnnotatedCommand\Hooks\HookManager; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
9
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Call hooks. |
15
|
|
|
*/ |
16
|
|
|
class ReplaceCommandHookDispatcher extends HookDispatcher |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return int |
21
|
|
|
*/ |
22
|
|
|
public function hasReplaceCommandHook() |
23
|
|
|
{ |
24
|
|
|
return count($this->getReplaceCommandHooks()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return \callable[] |
29
|
|
|
*/ |
30
|
|
|
public function getReplaceCommandHooks() |
31
|
|
|
{ |
32
|
|
|
$hooks = [ |
33
|
|
|
HookManager::REPLACE_COMMAND_HOOK, |
34
|
|
|
]; |
35
|
|
|
$replaceCommandHooks = $this->getHooks($hooks); |
36
|
|
|
|
37
|
|
|
return $replaceCommandHooks; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param \Consolidation\AnnotatedCommand\CommandData $commandData |
42
|
|
|
* |
43
|
|
|
* @return callable |
44
|
|
|
*/ |
45
|
|
|
public function getReplacementCommand(CommandData $commandData, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$replaceCommandHooks = $this->getReplaceCommandHooks($commandData); |
|
|
|
|
48
|
|
|
|
49
|
|
|
// We only take the first hook implementation of "replace-command" as the replacement. Commands shouldn't have |
50
|
|
|
// more than one replacement. |
51
|
|
|
$replacementCommand = reset($replaceCommandHooks); |
52
|
|
|
|
53
|
|
|
if (count($replaceCommandHooks) > 1) { |
54
|
|
|
$command_name = $commandData->annotationData()->get('command', 'unknown'); |
55
|
|
|
$output->writeln("<comment>Warning: multiple implementations of the \"replace-command\" hook exist for the \"$command_name\" command:</comment>"); |
56
|
|
|
foreach($replaceCommandHooks as $replaceCommandHook) { |
57
|
|
|
$class = get_class($replaceCommandHook[0]); |
58
|
|
|
$method = $replaceCommandHook[1]; |
59
|
|
|
$hook_name = "$class->$method"; |
60
|
|
|
$output->writeln("<comment> - $hook_name</comment>"); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $replacementCommand; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.