Skip to content

Commit

Permalink
bug #18 Fixing the boolean representation when using 1/0 (Vitor de So…
Browse files Browse the repository at this point in the history
…uza)

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
  • Loading branch information
sroze committed Aug 27, 2018
2 parents 069edca + 80497db commit c725fd8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions features/fill-variables.feature
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,21 @@ Feature:
"""
A_BASE64_VALUE=abc123=
"""

Scenario: It displays correctly boolean with 1 and 0
Given the file ".env.dist" contains:
"""
## Something
MY_VARIABLE=0
"""
And the file ".env" contains:
"""
MY_VARIABLE=
"""
When I run the companion with the following answers:
| Let's fix this? (y) | y |
| MY_VARIABLE ? (0) | 0 |
And the file ".env" should contain:
"""
MY_VARIABLE=0
"""
4 changes: 2 additions & 2 deletions src/Companienv/Interaction/AskVariableValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class AskVariableValues implements Extension
public function getVariableValue(Companion $companion, Block $block, Variable $variable)
{
$definedVariablesHash = $companion->getDefinedVariablesHash();
$defaultValue = $definedVariablesHash[$variable->getName()] ?? $variable->getValue() ?: $variable->getValue();
$defaultValue = ($definedVariablesHash[$variable->getName()] ?? $variable->getValue()) ?: $variable->getValue();
$question = sprintf('<comment>%s</comment> ? ', $variable->getName());

if ($defaultValue) {
if ($defaultValue !== '') {
$question .= '('.$defaultValue.') ';
}

Expand Down

0 comments on commit c725fd8

Please sign in to comment.