These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* (c) Anton Medvedev <[email protected]> |
||
3 | * |
||
4 | * For the full copyright and license information, please view the LICENSE |
||
5 | * file that was distributed with this source code. |
||
6 | */ |
||
7 | |||
8 | namespace Deployer\Console; |
||
9 | |||
10 | use Deployer\Deployer; |
||
11 | use Deployer\Exception\Exception; |
||
12 | use function Deployer\run; |
||
13 | use Deployer\Task\Context; |
||
14 | use Deployer\Task\Task; |
||
15 | use function Deployer\write; |
||
16 | use function Deployer\writeln; |
||
17 | use Symfony\Component\Console\Command\Command; |
||
18 | use Symfony\Component\Console\Input\InputArgument; |
||
19 | use Symfony\Component\Console\Input\InputInterface as Input; |
||
20 | use Symfony\Component\Console\Input\InputOption as Option; |
||
21 | use Symfony\Component\Console\Output\OutputInterface as Output; |
||
22 | |||
23 | class RunCommand extends Command |
||
24 | { |
||
25 | /** |
||
26 | * @var Deployer |
||
27 | */ |
||
28 | private $deployer; |
||
29 | |||
30 | /** |
||
31 | * @param Deployer $deployer |
||
32 | */ |
||
33 | 17 | public function __construct(Deployer $deployer) |
|
34 | { |
||
35 | 17 | parent::__construct('run'); |
|
36 | 17 | $this->setDescription('Run any arbitrary command on hosts'); |
|
37 | 17 | $this->deployer = $deployer; |
|
38 | 17 | } |
|
39 | |||
40 | /** |
||
41 | * Configures the command |
||
42 | */ |
||
43 | 17 | protected function configure() |
|
44 | { |
||
45 | 17 | $this->addArgument( |
|
46 | 17 | 'command-to-run', |
|
47 | 17 | InputArgument::REQUIRED, |
|
48 | 17 | 'Command to run' |
|
49 | ); |
||
50 | 17 | $this->addOption( |
|
51 | 17 | 'log', |
|
52 | 17 | null, |
|
53 | 17 | Option::VALUE_REQUIRED, |
|
54 | 17 | 'Log to file' |
|
55 | ); |
||
56 | 17 | $this->addOption( |
|
57 | 17 | 'stage', |
|
58 | 17 | null, |
|
59 | 17 | Option::VALUE_REQUIRED, |
|
60 | 17 | 'Stage to deploy' |
|
61 | ); |
||
62 | 17 | $this->addOption( |
|
63 | 17 | 'roles', |
|
64 | 17 | null, |
|
65 | 17 | Option::VALUE_REQUIRED, |
|
66 | 17 | 'Roles to deploy' |
|
67 | ); |
||
68 | 17 | $this->addOption( |
|
69 | 17 | 'hosts', |
|
70 | 17 | null, |
|
71 | 17 | Option::VALUE_REQUIRED, |
|
72 | 17 | 'Host to deploy, comma separated, supports ranges [:]' |
|
73 | ); |
||
74 | 17 | } |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | protected function execute(Input $input, Output $output) |
||
80 | { |
||
81 | $command = $input->getArgument('command-to-run'); |
||
82 | $stage = $input->getOption('stage'); |
||
83 | $roles = $input->getOption('roles'); |
||
84 | $hosts = $input->getOption('hosts'); |
||
85 | |||
86 | if (!empty($input->getOption('log'))) { |
||
87 | $this->deployer->config['log_file'] = $input->getOption('log'); |
||
88 | } |
||
89 | |||
90 | View Code Duplication | if (!empty($hosts)) { |
|
91 | $hosts = $this->deployer->hostSelector->getByHostnames($hosts); |
||
92 | } elseif (!empty($roles)) { |
||
93 | $hosts = $this->deployer->hostSelector->getByRoles($roles); |
||
94 | } else { |
||
95 | $hosts = $this->deployer->hostSelector->getHosts($stage); |
||
96 | } |
||
97 | |||
98 | if (empty($hosts)) { |
||
99 | throw new Exception('No host selected'); |
||
100 | } |
||
101 | |||
102 | $task = new Task($command, function () use ($command, $hosts) { |
||
0 ignored issues
–
show
|
|||
103 | $output = run($command); |
||
0 ignored issues
–
show
It seems like
$command defined by $input->getArgument('command-to-run') on line 81 can also be of type array<integer,string> or null ; however, Deployer\run() does only seem to accept string , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.
Loading history...
|
|||
104 | if (count($hosts) > 1) { |
||
105 | writeln("[{{hostname}}] > $output"); |
||
106 | } else { |
||
107 | write($output); |
||
108 | } |
||
109 | }); |
||
110 | |||
111 | foreach ($hosts as $host) { |
||
112 | $task->run(new Context($host, $input, $output)); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.