|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RMiller\ExemplifyExtension\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class ExemplifyCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
protected function configure() |
|
14
|
|
|
{ |
|
15
|
|
|
$this |
|
16
|
|
|
->setName('exemplify') |
|
17
|
|
|
->setDefinition(array( |
|
18
|
|
|
new InputArgument('class', InputArgument::REQUIRED, 'Class method belongs to'), |
|
19
|
|
|
new InputArgument('method', InputArgument::REQUIRED, 'Method to describe'), |
|
20
|
|
|
)) |
|
21
|
|
|
->setDescription('Adds an example for a method') |
|
22
|
|
|
->addOption('confirm', null, InputOption::VALUE_NONE, 'Ask for confirmation before creating example') |
|
23
|
|
|
->setHelp(<<<EOF |
|
24
|
|
|
The <info>%command.name%</info> command creates an example for a method: |
|
25
|
|
|
|
|
26
|
|
|
<info>php %command.full_name% ClassName MethodName</info> |
|
27
|
|
|
|
|
28
|
|
|
Will generate an example in the ClassNameSpec. |
|
29
|
|
|
|
|
30
|
|
|
EOF |
|
31
|
|
|
) |
|
32
|
|
|
; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param InputInterface $input |
|
37
|
|
|
* @param OutputInterface $output |
|
38
|
|
|
* |
|
39
|
|
|
* @return int|null|void |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$container = $this->getApplication()->getContainer(); |
|
|
|
|
|
|
44
|
|
|
$container->configure(); |
|
45
|
|
|
|
|
46
|
|
|
$classname = $input->getArgument('class'); |
|
47
|
|
|
$method = $input->getArgument('method'); |
|
48
|
|
|
|
|
49
|
|
|
if (!$this->confirm($input, $classname, $method)) { |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$resource = $container->get('locator.resource_manager')->createResource($classname); |
|
54
|
|
|
|
|
55
|
|
|
$container->get('code_generator')->generate($resource, 'specification_method', [ |
|
56
|
|
|
'method' => $method, |
|
57
|
|
|
'type' => $this->confirmMethodType($output), |
|
58
|
|
|
]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param InputInterface $input |
|
63
|
|
|
* @param $classname |
|
64
|
|
|
* @param $method |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
private function confirm(InputInterface $input, $classname, $method) |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$input->getOption('confirm')) { |
|
70
|
|
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$question = sprintf('Do you want to generate an example for %s::%s? (Y/n)', $classname, $method); |
|
74
|
|
|
$io = $this->getApplication()->getContainer()->get('console.io'); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return $io->askConfirmation($question, true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param OutputInterface $output |
|
81
|
|
|
*/ |
|
82
|
|
|
private function confirmMethodType(OutputInterface $output) |
|
83
|
|
|
{ |
|
84
|
|
|
$formattedMethodTypes = ['instance method','named constructor', 'static method']; |
|
85
|
|
|
$methodTypes = ['instance-method', 'named-constructor', 'static-method']; |
|
86
|
|
|
|
|
87
|
|
|
return $methodTypes[$this->getHelper('dialog')->select( |
|
|
|
|
|
|
88
|
|
|
$output, |
|
89
|
|
|
'Please select the method type (defaults to instance method)', |
|
90
|
|
|
$formattedMethodTypes, |
|
91
|
|
|
0 |
|
92
|
|
|
)]; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: