|
1
|
|
|
<?php |
|
2
|
|
|
namespace Naneau\FileGen\Test\Console; |
|
3
|
|
|
|
|
4
|
|
|
use Naneau\FileGen\Structure; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A simple command that uses the filegenParameters helper to ask for |
|
12
|
|
|
* parameters for a supplied Structure |
|
13
|
|
|
*/ |
|
14
|
|
|
class ParameterCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The structure |
|
18
|
|
|
* |
|
19
|
|
|
* @var Structure |
|
20
|
|
|
**/ |
|
21
|
|
|
private $structure; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* the receive params |
|
25
|
|
|
* |
|
26
|
|
|
* @var array |
|
27
|
|
|
**/ |
|
28
|
|
|
private $received = array(); |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Configure the command |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
**/ |
|
35
|
|
|
protected function configure() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->setName('filegen:test:filegen-parameters'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the structure |
|
42
|
|
|
* |
|
43
|
|
|
* @return Structure |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getStructure() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->structure; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The structure |
|
52
|
|
|
* |
|
53
|
|
|
* @param Structure $structure |
|
54
|
|
|
* @return ParameterCommand |
|
55
|
|
|
*/ |
|
56
|
|
|
public function setStructure(Structure $structure) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->structure = $structure; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the received parameters |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getReceived() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->received; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set the received parameters |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $received |
|
77
|
|
|
* @return ParameterCommand |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setReceived(array $received) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->received = $received; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Execute the command |
|
88
|
|
|
* |
|
89
|
|
|
* @param InputInterface $input |
|
90
|
|
|
* @param OutputInterface $output |
|
91
|
|
|
* @return void |
|
92
|
|
|
**/ |
|
93
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
94
|
|
|
{ |
|
95
|
|
|
$received = $this->getHelper('filegenParameters')->askParameters( |
|
|
|
|
|
|
96
|
|
|
$this->getStructure(), |
|
97
|
|
|
$input, |
|
98
|
|
|
$output |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$this->setReceived($received); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
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 implementation 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 interface: