Skip to content

Commit c725fd8

Browse files
committed
bug #18 Fixing the boolean representation when using 1/0 (Vitor de Souza)
This PR was merged into the master branch. Discussion ---------- Fixing the boolean representation when using 1/0 Until now, if we had something like APP_DEBUG=0 in `.env.dist` file, it was displayed like: ``` > Companienv\Composer\ScriptHandler::run It looks like you are missing some configuration (1 variables). I will help you to sort this out. Let's fix this? (y) APP_DEBUG ? ``` So you have no idea what type of value you should place here (true? 1? something else?). There's no problems with using "true" and "false", however, since we can represent booleans with 1 and 0 as well, we need to display it properly, as in: ``` > Companienv\Composer\ScriptHandler::run It looks like you are missing some configuration (1 variables). I will help you to sort this out. Let's fix this? (y) APP_DEBUG ? (0) ``` So now you know you should place either "0" or "1" here. The problem was actually just checking the if($variable) instead of comparing using "!== ''", so it was considering the string "0" as false and thus not display properly the default value after the question mark. I've also added a test for checking this behaviour and added parenthesis some lines up the "if" I've changed, for clarity of the null-coalesce/ternary precedence. Commits ------- 80497db Fixing the boolean representation when using 1/0
2 parents 069edca + 80497db commit c725fd8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

features/fill-variables.feature

+18
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,21 @@ Feature:
107107
"""
108108
A_BASE64_VALUE=abc123=
109109
"""
110+
111+
Scenario: It displays correctly boolean with 1 and 0
112+
Given the file ".env.dist" contains:
113+
"""
114+
## Something
115+
MY_VARIABLE=0
116+
"""
117+
And the file ".env" contains:
118+
"""
119+
MY_VARIABLE=
120+
"""
121+
When I run the companion with the following answers:
122+
| Let's fix this? (y) | y |
123+
| MY_VARIABLE ? (0) | 0 |
124+
And the file ".env" should contain:
125+
"""
126+
MY_VARIABLE=0
127+
"""

src/Companienv/Interaction/AskVariableValues.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ class AskVariableValues implements Extension
1515
public function getVariableValue(Companion $companion, Block $block, Variable $variable)
1616
{
1717
$definedVariablesHash = $companion->getDefinedVariablesHash();
18-
$defaultValue = $definedVariablesHash[$variable->getName()] ?? $variable->getValue() ?: $variable->getValue();
18+
$defaultValue = ($definedVariablesHash[$variable->getName()] ?? $variable->getValue()) ?: $variable->getValue();
1919
$question = sprintf('<comment>%s</comment> ? ', $variable->getName());
2020

21-
if ($defaultValue) {
21+
if ($defaultValue !== '') {
2222
$question .= '('.$defaultValue.') ';
2323
}
2424

0 commit comments

Comments
 (0)