Skip to content

Commit

Permalink
bug #20900 [Console] Descriptors should use Helper::strlen (ogizanagi)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.7 branch (closes #20900).

Discussion
----------

[Console] Descriptors should use Helper::strlen

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

Commits
-------

8e9a5f8 [Console] Descriptors should use Helper::strlen
  • Loading branch information
nicolas-grekas committed Dec 28, 2016
2 parents d85febc + 8e9a5f8 commit f1f5bff
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 11 deletions.
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -94,7 +95,7 @@ protected function describeCommand(Command $command, array $options = array())

$this->write(
$command->getName()."\n"
.str_repeat('-', strlen($command->getName()))."\n\n"
.str_repeat('-', Helper::strlen($command->getName()))."\n\n"
.'* Description: '.($command->getDescription() ?: '<none>')."\n"
.'* Usage:'."\n\n"
.array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
Expand All @@ -121,7 +122,7 @@ protected function describeApplication(Application $application, array $options
$describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
$description = new ApplicationDescription($application, $describedNamespace);

$this->write($application->getName()."\n".str_repeat('=', strlen($application->getName())));
$this->write($application->getName()."\n".str_repeat('=', Helper::strlen($application->getName())));

foreach ($description->getNamespaces() as $namespace) {
if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
Expand Down
17 changes: 9 additions & 8 deletions src/Symfony/Component/Console/Descriptor/TextDescriptor.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -37,7 +38,7 @@ protected function describeInputArgument(InputArgument $argument, array $options
$default = '';
}

$totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
$totalWidth = isset($options['total_width']) ? $options['total_width'] : Helper::strlen($argument->getName());
$spacingWidth = $totalWidth - strlen($argument->getName());

$this->writeText(sprintf(' <info>%s</info> %s%s%s',
Expand Down Expand Up @@ -75,7 +76,7 @@ protected function describeInputOption(InputOption $option, array $options = arr
sprintf('--%s%s', $option->getName(), $value)
);

$spacingWidth = $totalWidth - strlen($synopsis);
$spacingWidth = $totalWidth - Helper::strlen($synopsis);

$this->writeText(sprintf(' <info>%s</info> %s%s%s%s',
$synopsis,
Expand All @@ -94,7 +95,7 @@ protected function describeInputDefinition(InputDefinition $definition, array $o
{
$totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
foreach ($definition->getArguments() as $argument) {
$totalWidth = max($totalWidth, strlen($argument->getName()));
$totalWidth = max($totalWidth, Helper::strlen($argument->getName()));
}

if ($definition->getArguments()) {
Expand Down Expand Up @@ -206,7 +207,7 @@ protected function describeApplication(Application $application, array $options

foreach ($namespace['commands'] as $name) {
$this->writeText("\n");
$spacingWidth = $width - strlen($name);
$spacingWidth = $width - Helper::strlen($name);
$this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options);
}
}
Expand Down Expand Up @@ -252,9 +253,9 @@ private function getColumnWidth(array $commands)
$widths = array();

foreach ($commands as $command) {
$widths[] = strlen($command->getName());
$widths[] = Helper::strlen($command->getName());
foreach ($command->getAliases() as $alias) {
$widths[] = strlen($alias);
$widths[] = Helper::strlen($alias);
}
}

Expand All @@ -271,10 +272,10 @@ private function calculateTotalWidthForOptions($options)
$totalWidth = 0;
foreach ($options as $option) {
// "-" + shortcut + ", --" + name
$nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
$nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName());

if ($option->acceptValue()) {
$valueLength = 1 + strlen($option->getName()); // = + value
$valueLength = 1 + Helper::strlen($option->getName()); // = + value
$valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]

$nameLength += $valueLength;
Expand Down
Expand Up @@ -86,7 +86,7 @@ abstract protected function getDescriptor();

abstract protected function getFormat();

private function getDescriptionTestData(array $objects)
protected function getDescriptionTestData(array $objects)
{
$data = array();
foreach ($objects as $name => $object) {
Expand Down
Expand Up @@ -12,9 +12,27 @@
namespace Symfony\Component\Console\Tests\Descriptor;

use Symfony\Component\Console\Descriptor\MarkdownDescriptor;
use Symfony\Component\Console\Tests\Fixtures\DescriptorApplicationMbString;
use Symfony\Component\Console\Tests\Fixtures\DescriptorCommandMbString;

class MarkdownDescriptorTest extends AbstractDescriptorTest
{
public function getDescribeCommandTestData()
{
return $this->getDescriptionTestData(array_merge(
ObjectsProvider::getCommands(),
array('command_mbstring' => new DescriptorCommandMbString())
));
}

public function getDescribeApplicationTestData()
{
return $this->getDescriptionTestData(array_merge(
ObjectsProvider::getApplications(),
array('application_mbstring' => new DescriptorApplicationMbString())
));
}

protected function getDescriptor()
{
return new MarkdownDescriptor();
Expand Down
Expand Up @@ -12,9 +12,27 @@
namespace Symfony\Component\Console\Tests\Descriptor;

use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Tests\Fixtures\DescriptorApplicationMbString;
use Symfony\Component\Console\Tests\Fixtures\DescriptorCommandMbString;

class TextDescriptorTest extends AbstractDescriptorTest
{
public function getDescribeCommandTestData()
{
return $this->getDescriptionTestData(array_merge(
ObjectsProvider::getCommands(),
array('command_mbstring' => new DescriptorCommandMbString())
));
}

public function getDescribeApplicationTestData()
{
return $this->getDescriptionTestData(array_merge(
ObjectsProvider::getApplications(),
array('application_mbstring' => new DescriptorApplicationMbString())
));
}

protected function getDescriptor()
{
return new TextDescriptor();
Expand Down
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Fixtures;

use Symfony\Component\Console\Application;

class DescriptorApplicationMbString extends Application
{
public function __construct()
{
parent::__construct('MbString åpplicätion');

$this->add(new DescriptorCommandMbString());
}
}
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Fixtures;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class DescriptorCommandMbString extends Command
{
protected function configure()
{
$this
->setName('descriptor:åèä')
->setDescription('command åèä description')
->setHelp('command åèä help')
->addUsage('-o|--option_name <argument_name>')
->addUsage('<argument_name>')
->addArgument('argument_åèä', InputArgument::REQUIRED)
->addOption('option_åèä', 'o', InputOption::VALUE_NONE)
;
}
}

0 comments on commit f1f5bff

Please sign in to comment.