Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4.x] Fix console handler with named arguments and add more tests #300

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Handler/ConsoleHandler.php
Expand Up @@ -264,7 +264,7 @@ private static function normalizeParams(array $params, array $args): array

$key = array_search($name, $params);
Assert::integer($key);
$params = array_slice($params, $key + 1);
unset($params[$key]);
} else {
$name = array_shift($params);
}
Expand Down
Expand Up @@ -8,6 +8,7 @@ Feature: ConsoleArgument named arguments with PHP8
<?php

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
"""
Expand Down Expand Up @@ -36,3 +37,28 @@ Feature: ConsoleArgument named arguments with PHP8
| Type | Message |
| Trace | $argument: string |
And I see no other errors

Scenario: Assert adding console argument with only named arguments works as expected
Given I have the following code
"""
class MyCommand extends Command
{
protected function configure(): void
{
$this->addArgument(name: 'test', description: 'foo', mode: InputArgument::OPTIONAL, default: 'test');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @psalm-trace $argument */
$argument = $input->getArgument('test');

return 0;
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| Trace | $argument: string |
And I see no other errors
31 changes: 31 additions & 0 deletions tests/acceptance/acceptance/console/ConsoleOptionNamedArgs.feature
Expand Up @@ -37,3 +37,34 @@ Feature: ConsoleOption named arguments with PHP8
| Type | Message |
| Trace | $string: string |
And I see no other errors

Scenario: Assert adding options with only named arguments works as expected
Given I have the following code
"""
class MyCommand extends Command
{
public function configure(): void
{
$this->addOption(
name: 'test',
mode: InputOption::VALUE_REQUIRED,
description: 'foo',
shortcut: 't',
default: 'test'
);
}

public function execute(InputInterface $input, OutputInterface $output): int
{
/** @psalm-trace $string */
$string = $input->getOption('test');

return 0;
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| Trace | $string: string |
And I see no other errors