Skip to content

Conversation

sanpii
Copy link
Contributor

@sanpii sanpii commented Apr 10, 2017

Q A
Branch? master
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

This is a quick fix to prevent anoying when you follow a tutorial using another dotenv component. For example, this code uses vlucas/phpdotenv:

$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
$dotenv->load();

Silently failed with symfony/dotenv.

@@ -47,7 +47,13 @@
public function load(/*...$paths*/)
Copy link
Member

Choose a reason for hiding this comment

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

if we want to enforce one arg, what about doing it here load($path/*, ...$paths*/)?

Copy link
Member

Choose a reason for hiding this comment

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

@nicolas-grekas suggestion looks better indeed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we want to enforce one arg, what about doing it here load($path/*, ...$paths*/) ?

For me, this introduces two bad things:

  • add an array_unshift call;
  • pollute the method signature.

Copy link
Member

Choose a reason for hiding this comment

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

Well, it does not really pollute the method signature if we really want to require at least 1 arg. In PHP 5.6+, public function load(...$paths) means 0+ arguments, not 1+.

Copy link
Member

Choose a reason for hiding this comment

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

and in the current code, this does not introduce any array_unshift. $paths = func_get_args(); will still give us all arguments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof ok, good points.

@@ -39,15 +39,18 @@
/**
* Loads one or several .env files.
*
* @param ...string A list of files to load
* @param $path A file to load
* @param ...string A list of additionnal files to load
Copy link
Member

Choose a reason for hiding this comment

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

Should be string[] instead of ...string

Copy link
Member

Choose a reason for hiding this comment

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

not true. string[] means the outside is passing an array. Here, the signature is not an array but a variadic arg

@@ -39,15 +39,18 @@
/**
* Loads one or several .env files.
*
* @param ...string A list of files to load
* @param $path A file to load
Copy link
Member

Choose a reason for hiding this comment

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

Missing string type

{
// func_get_args() to be replaced by a variadic argument for Symfony 4.0
foreach (func_get_args() as $path) {
$paths = func_get_args();
Copy link
Member

Choose a reason for hiding this comment

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

Why making this change? Using func_get_args() below is still fine to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, it’s a relic from my first patch.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

👍

@@ -39,12 +39,13 @@
/**
* Loads one or several .env files.
*
* @param ...string A list of files to load
* @param string $path A file to load
* @param string[] $paths A list of additionnal files to load
Copy link
Contributor

Choose a reason for hiding this comment

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

@param string ...$paths?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas says the opposite #22364 (review)

Copy link
Contributor

Choose a reason for hiding this comment

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

I see... we dont allow for arrays right? So im not sure about []

Copy link
Member

Choose a reason for hiding this comment

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

looking at eg http://stackoverflow.com/questions/14513356/phpdoc-documenting-a-function-with-a-variable-number-of-arguments
...string should be way to go, sorry for the bad comment

@fabpot
Copy link
Member

fabpot commented Apr 11, 2017

Thank you @sanpii.

@fabpot fabpot merged commit 911bc68 into symfony:master Apr 11, 2017
fabpot added a commit that referenced this pull request Apr 11, 2017
This PR was merged into the 3.3-dev branch.

Discussion
----------

[Dotenv] Throwing an error when loading nothing

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

This is a quick fix to prevent anoying when you follow a tutorial using another dotenv component. For example, this code uses [vlucas/phpdotenv](https://packagist.org/packages/vlucas/phpdotenv):

```php
$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
$dotenv->load();
```

Silently failed with symfony/dotenv.

Commits
-------

911bc68 [Dotenv] Throwing an error when loading nothing
@sanpii sanpii deleted the dotenv-empty-load branch April 11, 2017 14:32
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