Skip to content

Commit dd210a5

Browse files
committed
Merge pull request #617 from pantheon-systems/lint_files_4
Linted the forgotten rest of files in php/Terminus/*
2 parents 41f1bdc + 5457f8b commit dd210a5

17 files changed

+1191
-667
lines changed

php/Terminus/Completions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function __construct($line) {
3232

3333
list($command, $args, $assoc_args) = $r;
3434

35-
$spec = SynopsisParser::parse($command->get_synopsis());
35+
$spec = SynopsisParser::parse($command->getSynopsis());
3636

3737
foreach ($spec as $arg) {
3838
if ($arg['type'] == 'positional' && $arg['name'] == 'file') {
@@ -41,8 +41,8 @@ function __construct($line) {
4141
}
4242
}
4343

44-
if ($command->can_have_subcommands()) {
45-
foreach ($command->get_subcommands() as $name => $_) {
44+
if ($command->canHaveSubcommands()) {
45+
foreach ($command->getSubcommands() as $name => $_) {
4646
$this->add("$name ");
4747
}
4848
} else {
@@ -102,9 +102,9 @@ private function getCommand($words) {
102102
}
103103
}
104104

105-
$r = Terminus::getRunner()->find_command_to_run($positional_args);
105+
$r = Terminus::getRunner()->findCommandToRun($positional_args);
106106
if (!is_array($r) && array_pop($positional_args) == $this->cur_word) {
107-
$r = Terminus::getRunner()->find_command_to_run($positional_args);
107+
$r = Terminus::getRunner()->findCommandToRun($positional_args);
108108
}
109109

110110
if (!is_array($r)) {

php/Terminus/Dispatcher/CommandFactory.php

Lines changed: 90 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,108 @@
1010
*/
1111
class CommandFactory {
1212

13-
public static function create( $name, $class, $parent ) {
14-
$reflection = new \ReflectionClass( $class );
15-
16-
if ( $reflection->hasMethod( '__invoke' ) ) {
17-
$command = self::create_subcommand( $parent, $name, $reflection->name,
18-
$reflection->getMethod( '__invoke' ) );
13+
/**
14+
* Creates a new composite command or subcommand
15+
*
16+
* @param [string] $name Name of command to create
17+
* @param [string] $class Name of class command belongs to
18+
* @param [RootCommand] $parent Parent command
19+
* @return [mixed] $command Either CompositeCommand or Subcommand
20+
*/
21+
public static function create($name, $class, $parent) {
22+
$reflection = new \ReflectionClass($class);
23+
24+
if ($reflection->hasMethod('__invoke')) {
25+
$command = self::createSubcommand(
26+
$parent,
27+
$name,
28+
$reflection->name,
29+
$reflection->getMethod('__invoke')
30+
);
1931
} else {
20-
$command = self::create_composite_command( $parent, $name, $reflection );
32+
$command = self::createCompositeCommand($parent, $name, $reflection);
2133
}
2234

2335
return $command;
2436
}
2537

26-
private static function create_subcommand( $parent, $name, $class_name, $method ) {
27-
$docparser = new DocParser( $method->getDocComment() );
28-
29-
if ( !$name )
30-
$name = $docparser->getTag( 'subcommand' );
31-
32-
if ( !$name )
33-
$name = $method->name;
34-
35-
$method_name = $method->name;
36-
37-
$when_invoked = function ( $args, $assoc_args ) use ( $class_name, $method_name ) {
38-
call_user_func( array( new $class_name, $method_name ), $args, $assoc_args );
39-
};
40-
41-
return new Subcommand( $parent, $name, $docparser, $when_invoked );
42-
}
43-
44-
private static function create_composite_command( $parent, $name, $reflection ) {
45-
$docparser = new DocParser( $reflection->getDocComment() );
46-
47-
$container = new CompositeCommand( $parent, $name, $docparser );
48-
49-
foreach ( $reflection->getMethods() as $method ) {
50-
if ( !self::is_good_method( $method ) )
38+
/**
39+
* Creates a new composite command
40+
*
41+
* @param [RootCommand] $parent Parent command
42+
* @param [string] $name Name of command to create
43+
* @param [\ReflectionClass] $reflection Object with name of class to call
44+
* @return [CompositeCommand] $container
45+
*/
46+
private static function createCompositeCommand($parent, $name, $reflection) {
47+
$docparser = new DocParser($reflection->getDocComment());
48+
$container = new CompositeCommand($parent, $name, $docparser);
49+
50+
foreach ($reflection->getMethods() as $method) {
51+
if (!self::isGoodMethod($method)) {
5152
continue;
52-
53-
$subcommand = self::create_subcommand( $container, false, $reflection->name, $method );
54-
55-
$subcommand_name = $subcommand->get_name();
56-
57-
$container->add_subcommand( $subcommand_name, $subcommand );
53+
}
54+
$subcommand = self::createSubcommand(
55+
$container,
56+
false,
57+
$reflection->name,
58+
$method
59+
);
60+
$subcommand_name = $subcommand->getName();
61+
$container->addSubcommand($subcommand_name, $subcommand);
5862
}
5963

6064
return $container;
6165
}
6266

63-
private static function is_good_method( $method ) {
64-
return $method->isPublic() && !$method->isConstructor() && !$method->isStatic();
67+
/**
68+
* Creates a new subcommand
69+
*
70+
* @param [RootCommand] $parent Parent command
71+
* @param [string] $name Name of command to create
72+
* @param [string] $class_name Name of class command belongs to
73+
* @param [string] $method Name of function to invoke in class
74+
* @return [Subcommand] $subcommand
75+
*/
76+
private static function createSubcommand(
77+
$parent,
78+
$name,
79+
$class_name,
80+
$method
81+
) {
82+
$docparser = new DocParser($method->getDocComment());
83+
if (!$name) {
84+
$name = $docparser->getTag('subcommand');
85+
if (!$name) {
86+
$name = $method->name;
87+
}
88+
}
89+
$method_name = $method->name;
90+
$when_invoked =
91+
function ($args, $assoc_args) use ($class_name, $method_name) {
92+
call_user_func(
93+
array(new $class_name, $method_name),
94+
$args,
95+
$assoc_args
96+
);
97+
};
98+
99+
$subcommand = new Subcommand($parent, $name, $docparser, $when_invoked);
100+
return $subcommand;
101+
}
102+
103+
/**
104+
* Decides if the given method is a good one
105+
*
106+
* @param [string] $method Name of method to be called
107+
* @return [boolean] $is_good_method
108+
*/
109+
private static function isGoodMethod($method) {
110+
$is_good_method = $method->isPublic()
111+
&& !$method->isConstructor()
112+
&& !$method->isStatic();
113+
return $is_good_method;
65114
}
115+
66116
}
67117

0 commit comments

Comments
 (0)