Skip to content

symfony/config does not support multiline text #60317

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

Closed
root-aza opened this issue May 1, 2025 · 2 comments
Closed

symfony/config does not support multiline text #60317

root-aza opened this issue May 1, 2025 · 2 comments

Comments

@root-aza
Copy link

root-aza commented May 1, 2025

Symfony version(s) affected

5.3 - 7.3

Description

The symfony/config component formats phpdoc comments very poorly, and does not support multiple hyphenated text. Example below

How to reproduce

Repoduce stand:

<?php

declare (strict_types = 1);

use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

require_once __DIR__.'/vendor/autoload.php';

final class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder(): TreeBuilder
    {
        $treeBuilder = new TreeBuilder('temporal');

        $treeBuilder->getRootNode()
            ->children()
                ->scalarNode('foo')
                    ->defaultValue('bar')
                    ->info(
                        <<<STRING
                          Sets the rate limiting on number of activities that can be
                          executed per second per worker. This can be used to limit resources used by the worker.

                          Notice that the number is represented in float, so that you can set it
                          to less than 1 if needed. For example, set the number to 0.1 means you
                          want your activity to be executed once for every 10 seconds. This can be
                          used to protect down stream services from flooding.
                        STRING
                    )
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}


$builder = new ConfigBuilderGenerator(__DIR__ . '/dump');

$builder->build(new Configuration);

Output:

<?php

namespace Symfony\Config;

use Symfony\Component\Config\Loader\ParamConfigurator;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
 * This class is automatically generated to help in creating a config.
 */
class TemporalConfig implements \Symfony\Component\Config\Builder\ConfigBuilderInterface
{
    private $foo;
    private $_usedProperties = [];

    /**
     *   Sets the rate limiting on number of activities that can be
      executed per second per worker. This can be used to limit resources used by the worker.

      Notice that the number is represented in float, so that you can set it
      to less than 1 if needed. For example, set the number to 0.1 means you
      want your activity to be executed once for every 10 seconds. This can be
      used to protect down stream services from flooding.
     * @default 'bar'
     * @param ParamConfigurator|mixed $value
     * @return $this
     */
    public function foo($value): static
    {
        $this->_usedProperties['foo'] = true;
        $this->foo = $value;

        return $this;
    }

    public function getExtensionAlias(): string
    {
        return 'temporal';
    }

    public function __construct(array $value = [])
    {
        if (array_key_exists('foo', $value)) {
            $this->_usedProperties['foo'] = true;
            $this->foo = $value['foo'];
            unset($value['foo']);
        }

        if ([] !== $value) {
            throw new InvalidConfigurationException(sprintf('The following keys are not supported by "%s": ', __CLASS__).implode(', ', array_keys($value)));
        }
    }

    public function toArray(): array
    {
        $output = [];
        if (isset($this->_usedProperties['foo'])) {
            $output['foo'] = $this->foo;
        }

        return $output;
    }

}

Possible Solution

No response

Additional Context

No response

@root-aza
Copy link
Author

root-aza commented May 1, 2025

Example after using the patch:

<?php

namespace Symfony\Config;

use Symfony\Component\Config\Loader\ParamConfigurator;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

/**
 * This class is automatically generated to help in creating a config.
 */
class TemporalConfig implements \Symfony\Component\Config\Builder\ConfigBuilderInterface
{
    private $foo;
    private $_usedProperties = [];

    /**
     * Sets the rate limiting on number of activities that can be
     * executed per second per worker. This can be used to limit resources used by the worker.
     * Notice that the number is represented in float, so that you can set it
     * to less than 1 if needed. For example, set the number to 0.1 means you
     * want your activity to be executed once for every 10 seconds. This can be
     * used to protect down stream services from flooding.
     * @default 'bar'
     * @param ParamConfigurator|mixed $value
     * @return $this
     */
    public function foo($value): static
    {
        $this->_usedProperties['foo'] = true;
        $this->foo = $value;

        return $this;
    }

    public function getExtensionAlias(): string
    {
        return 'temporal';
    }

    public function __construct(array $value = [])
    {
        if (array_key_exists('foo', $value)) {
            $this->_usedProperties['foo'] = true;
            $this->foo = $value['foo'];
            unset($value['foo']);
        }

        if ([] !== $value) {
            throw new InvalidConfigurationException(sprintf('The following keys are not supported by "%s": ', __CLASS__).implode(', ', array_keys($value)));
        }
    }

    public function toArray(): array
    {
        $output = [];
        if (isset($this->_usedProperties['foo'])) {
            $output['foo'] = $this->foo;
        }

        return $output;
    }

}

@GromNaN
Copy link
Member

GromNaN commented May 11, 2025

Thanks for reporting this issue. I opened #60400.
Note that when you use HEREDOC, the indentation of the text should be aligned with the closing tag.

                    ->info(<<<STRING
                        Sets the rate limiting on number of activities that can be
                        executed per second per worker. This can be used to limit resources used by the worker.

                        Notice that the number is represented in float, so that you can set it
                        to less than 1 if needed. For example, set the number to 0.1 means you
                        want your activity to be executed once for every 10 seconds. This can be
                        used to protect down stream services from flooding.
                        STRING)

Status: Reviewed

fabpot added a commit that referenced this issue May 14, 2025
This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[Config] Fix generated comment for multiline "info"

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #60317
| License       | MIT

Instead of fixing the multiline comment formatting only for the "info" part, the `* ` is now prepended to every line of the generated comment block.
`nette/code-generator` uses almost the same transformation: https://github.com/nette/php-generator/blob/42806049a7774a2bd316c958f5dcf01c6b5c56fa/src/PhpGenerator/Helpers.php#L60

Commits
-------

941c051 [Config] Fix generated comment for multiline "info"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants