Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Fix bug with utf-8 bug, change writePrompt to use one function prepareChoices #33911

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
wants to merge 6 commits into from

Conversation

proggga
Copy link
Contributor

@proggga proggga commented Oct 8, 2019

Q A
Branch? 3.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets #33928
License MIT
Doc PR

Refactor QuestionHelper and SymfonyQuestionHelper to use one function that prepare choices question. Remove duplicate code.

@@ -30,6 +30,7 @@
*/
class QuestionHelper extends Helper
{
const DEFAULT_PROMPT = ' > ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should it be a public const ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, moved to Question class

@proggga proggga force-pushed the progga/refactor_questions_helper branch from b36d10b to cf9efe9 Compare October 8, 2019 14:45
@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Oct 8, 2019
@proggga
Copy link
Contributor Author

proggga commented Oct 8, 2019

Please help, why 'TranslationFilesTest.php:42' is failing?
I didn't touch that code

@proggga
Copy link
Contributor Author

proggga commented Oct 15, 2019

Anyone?

@ostrolucky
Copy link
Contributor

It doesn't seem related, don't worry about it. But perhaps you could get rid of it by rebasing on top of latest 3.4?

@proggga proggga force-pushed the progga/refactor_questions_helper branch from 0e1edd9 to 913f8c3 Compare October 15, 2019 17:50
@proggga
Copy link
Contributor Author

proggga commented Oct 15, 2019

Rebased on top 3.4

@proggga proggga force-pushed the progga/refactor_questions_helper branch from 913f8c3 to b5e1596 Compare October 24, 2019 11:32
@proggga
Copy link
Contributor Author

proggga commented Oct 24, 2019

Rebase on top 3.4,
anybody?

@proggga
Copy link
Contributor Author

proggga commented Oct 29, 2019

Are you serious?

@javiereguiluz
Copy link
Member

@proggga because of how Symfony releases new versions, October-November of each year is a very stressful moment for us. Moreover, every two years, the stress is even more intense because we release a new major version. This year is one of those years. So, we're ultra busy with millions of things and we may respond to you a bit late. Thanks for understanding! (and thanks for your contribution too!)

@proggga
Copy link
Contributor Author

proggga commented Oct 29, 2019

Sorry for maybe being aggressive, It was strange, very fast people came and gave alot of changes, but after it disappeared

@nicolas-grekas nicolas-grekas changed the title Refactor writePrompt to use one function prepareChoices [Console ]Refactor writePrompt to use one function prepareChoices Dec 2, 2019
@nicolas-grekas nicolas-grekas changed the title [Console ]Refactor writePrompt to use one function prepareChoices [Console] Refactor writePrompt to use one function prepareChoices Dec 2, 2019
@nicolas-grekas
Copy link
Member

There is a mismatch here: the PR title tells about a refacto, but the linked issue is about UTF-8 length computation. refacto go on master - fixes on 3.4. Could you please update the PR according to what it is? I didn't look better before asking :)

@proggga proggga changed the title [Console] Refactor writePrompt to use one function prepareChoices [Console] Fix bug with utf-8 writePrompt to use one function prepareChoices Dec 2, 2019
@proggga proggga changed the title [Console] Fix bug with utf-8 writePrompt to use one function prepareChoices [Console] Fix bug with utf-8 bug, change writePrompt to use one function prepareChoices Dec 2, 2019
@proggga
Copy link
Contributor Author

proggga commented Dec 3, 2019

Fixed

{
$choices = $question->getChoices();

$maxWidth = max(array_map([$this, 'strlen'], array_keys($choices)));
Copy link
Contributor

@fancyweb fancyweb Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self::strlen?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[static::class, 'strlen'] (but honestly I've no issue with using $this)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently use self:: everywhere, we don't expect those methods to be overidden.

$messages = [];

foreach ($choices as $key => $value) {
$width = $maxWidth - $this->strlen($key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self::strlen

foreach ($choices as $key => $value) {
$width = $maxWidth - $this->strlen($key);
// We using strlen + str_repeat to fix sprintf whitespace padding problem with UTF-8)
$messages[] = sprintf(' [<%s>%s%s</%s>] %s', $tag, $key, str_repeat(' ', $width), $tag, $value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas sprintf left justify does not work with multibytes. Shouldn't we create a sprintf method in the abstract Helper class like we did for strlen, substr, etc.? Or using str_repeat for all strings is ok?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, you want to implement sprintf in pure PHP.
Yes, sure... in theory :)
in practice, I don't know :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean add a sprintf helper method to handle the left justify. If the string is not utf-8 we use the normal sprintf. If it is, we use the str_repeat strategy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sure!

Copy link
Contributor

@fancyweb fancyweb Dec 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@proggga Would you have time to take care of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for reviewing this,
Yea, Maybe I can, but I'm not true symphonist, so I don't know rules for creating new helper/naming/best place for it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@proggga Would you have time to take care of this?

May be you can give instructions, and after may be I can do it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your proposed changes, you replaces the sprintf left justify feat by strlen + str_repeat everytime. Firstly, this alternative should only be used for multibytes strings. Secondly, it should be done in a new helper method in the abstract Helper.php class (like we did for strlen and sbustr).

}
$output->writeln($messages);
// ChoiceQuestion can have any prompt
$output->write($question->getPrompt() ?: Question::DEFAULT_PROMPT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPrompt() will always return a string. The constant is also useless. Just write the prompt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's another bug fixed by this PR btw. Unrelated to the linked issue.

// ChoiceQuestion can have any prompt
$output->write($question->getPrompt() ?: Question::DEFAULT_PROMPT);
} else {
$output->write(Question::DEFAULT_PROMPT);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here keep the old behavior?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should actually move the prompt from the ChoiceQuestion to the Question class? 🤔

@nicolas-grekas
Copy link
Member

Continued in #35000, thanks @proggga

nicolas-grekas added a commit that referenced this pull request Dec 17, 2019
…n choices keys and custom prompt (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[Console][SymfonyQuestionHelper] Handle multibytes question choices keys and custom prompt

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #33928
| License       | MIT
| Doc PR        | -

Replaces / finishes #33911

Commits
-------

f175032 [Console][SymfonyQuestionHelper] Handle multibytes question choices keys and custom prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants