-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
calling this method twice do not make sense #19909
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
Conversation
I agree that the code looks weird and it doesn't make sense ... but if you remove that method, 1 test fails:
|
Looks like a dx issue to me: /**
* @param bool $short Whether to show the short version of the synopsis (with options folded) or not
*/
public function getSynopsis($short = false)
{
$key = $short ? 'short' : 'long';
if (!isset($this->synopsis[$key])) {
$this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short)));
}
return $this->synopsis[$key];
} Could become |
javiereguiluz your are right. Sorry that i did not dig deeper into it. getSynopsis() avoids the single responsibility principle (SRP). Because:
Calling a getter method without using its return value is a:
Instead of calling: we should call: getSynopsis() should be refactored to use the init methods. |
Status: needs work |
@christian-weiss have you digged in why calling this method twice does not make sense exactly? The comment is more or less clear about it
Ie. what are you trying to solve? |
My understanding is @christian-weiss is trying to improve DX, and obviously calling a getter method twice and ignoring its return value isn't particularly straight-forward code to read. In the context of this specific piece of code, I don't have a strong opinion as to whether it is beneficial for this method to remain exactly as is, or for it to be refactored into I simply want to point out that the SRP deficiency @christian-weiss asserted is absolutely valid. Saying (and I quote) "getSynopsis follows SRP perfectly fine" couldn't be farther from the truth: the
Now, to be clear, this isn't meant to be an endorsement of refactoring the code: sometimes the most functional code does not perfectly model every rule, and that's just fine. But then, the conversation should not be whether or not the method follows the single responsibility principle (it doesn't), but instead focus on where to draw the line between effective SRP (and inherit DX clarity that is achieved through the application of this rule) and inefficient SRP (and the excessive verbosity that results). |
Imo the problem is pre-initializing here because otherwise Im not fully aware of inner workings here... but just removing a method call for cosmetic reasons dont seem right. Hence i asked have you digged in why calling this method twice does not make sense exactly?
Yes it's a somewhat pragmatic method (2in1 getter), but valid SRP imo. It's only responsibility is to return a short/ or long synopsis. |
Ok. Initializing is needed because otherwise However it looks inconsistent when using descriptors, At first sight i'd say move the |
Passes tests for the console component. diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php
index 0d5001b..2df1ec1 100644
--- a/src/Symfony/Component/Console/Command/Command.php
+++ b/src/Symfony/Component/Console/Command/Command.php
@@ -209,10 +209,6 @@ class Command
*/
public function run(InputInterface $input, OutputInterface $output)
{
- // force the creation of the synopsis before the merge with the app definition
- $this->getSynopsis(true);
- $this->getSynopsis(false);
-
// add the application arguments and options
$this->mergeApplicationDefinition();
@@ -300,6 +296,10 @@ class Command
return;
}
+ // force the creation of the synopsis before the merge with the app definition
+ $this->getSynopsis(true);
+ $this->getSynopsis(false);
+
$this->definition->addOptions($this->application->getDefinition()->getOptions());
if ($mergeArgs) {
diff --git a/src/Symfony/Component/Console/Descriptor/JsonDescriptor.php b/src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
index 87e38fd..c3852a8 100644
--- a/src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
+++ b/src/Symfony/Component/Console/Descriptor/JsonDescriptor.php
@@ -152,7 +152,6 @@ class JsonDescriptor extends Descriptor
*/
private function getCommandData(Command $command)
{
- $command->getSynopsis();
$command->mergeApplicationDefinition(false);
return array(
diff --git a/src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php b/src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
index 2eb9944..3af4a15 100644
--- a/src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
+++ b/src/Symfony/Component/Console/Descriptor/MarkdownDescriptor.php
@@ -89,7 +89,6 @@ class MarkdownDescriptor extends Descriptor
*/
protected function describeCommand(Command $command, array $options = array())
{
- $command->getSynopsis();
$command->mergeApplicationDefinition(false);
$this->write(
diff --git a/src/Symfony/Component/Console/Descriptor/TextDescriptor.php b/src/Symfony/Component/Console/Descriptor/TextDescriptor.php
index 719f238..cc12aac 100644
--- a/src/Symfony/Component/Console/Descriptor/TextDescriptor.php
+++ b/src/Symfony/Component/Console/Descriptor/TextDescriptor.php
@@ -134,8 +134,6 @@ class TextDescriptor extends Descriptor
*/
protected function describeCommand(Command $command, array $options = array())
{
- $command->getSynopsis(true);
- $command->getSynopsis(false);
$command->mergeApplicationDefinition(false);
$this->writeText('<comment>Usage:</comment>', $options);
diff --git a/src/Symfony/Component/Console/Descriptor/XmlDescriptor.php b/src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
index b5676be..69e5a25 100644
--- a/src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
+++ b/src/Symfony/Component/Console/Descriptor/XmlDescriptor.php
@@ -59,7 +59,6 @@ class XmlDescriptor extends Descriptor
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($commandXML = $dom->createElement('command'));
- $command->getSynopsis();
$command->mergeApplicationDefinition(false);
$commandXML->setAttribute('id', $command->getName()); |
It still looks very weird to call getters and not doing anything with them. Right now it's just getters with side effects it seems |
Agree, but at least it looks weird in 1 place only :) |
If you have trouble with my SRP definition, just leave that argument as it is. Premature optimization is the root of all evil (Donald Knuth). The side effect of a getter is worse and avoids POLS / POSA. To remove the DX issue just move the init stuff forward to a init method or to the constructor. Or give a valid reason to delay the initialization. |
Agree this all is not designed perfectly and perhaps violates one or more patterns in one or more ways. But lets be pragmatic.. the intention of the code is clear by now? Imo. the main problem is edit: im not sure what the effect will be on
|
The goal is not to optimize memory, but to avoid computing the synopsis for all cases which don't need it (it is needed only when displaying the help, not when actually running the command). |
@stof would |
Closing in favor of #20054 |
…finition (ro0NL) This PR was merged into the 5.2-dev branch. Discussion ---------- [Console] Different approach on merging application definition | Q | A | | --- | --- | | Branch? | "master" | | Bug fix? | yes | | New feature? | not really (refactoring) | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | #19181, #17804, #19909, partially #20030 | | License | MIT | | Doc PR | reference to the documentation PR, if any | Before/After: ``` diff $ bin/console list -h Usage: list [options] [--] [<namespace>] Arguments: namespace The namespace name Options: --raw To output raw command list --format=FORMAT The output format (txt, xml, json, or md) [default: "txt"] + -h, --help Display this help message + -q, --quiet Do not output any message + -V, --version Display this application version + --ansi Force ANSI output + --no-ansi Disable ANSI output + -n, --no-interaction Do not ask any interactive question + -e, --env=ENV The environment name [default: "dev"] + --no-debug Switches off debug mode + -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Help: The list command lists all commands: php bin/console list You can also display the commands for a specific namespace: php bin/console list test You can also output the information in other formats by using the --format option: php bin/console list --format=xml It's also possible to get raw list of commands (useful for embedding command runner): php bin/console list --raw ``` This could deprecate `getNativeDefinition` or make it a feature as right now it's internal and unused. edit: resolved the BC break. edit2: question is.. should this target 2.7? Commits ------- 553b173 [Console] Different approach on merging application definition
small refactoring