These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | $dotenv = new Dotenv\Dotenv(__DIR__); |
||
3 | $dotenv->overload(); |
||
4 | |||
5 | /** |
||
6 | * Get Environment variable |
||
7 | * |
||
8 | * @param string $key |
||
9 | * @param null $default |
||
10 | * @return string/null |
||
0 ignored issues
–
show
The doc-type
string/null could not be parsed: Unknown type name "string/null" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. ![]() |
|||
11 | */ |
||
12 | function env($key = '', $default = null) |
||
0 ignored issues
–
show
env uses the super-global variable $_ENV which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
![]() |
|||
13 | { |
||
14 | return $_ENV[$key] != '' ? $_ENV[$key] : $default; |
||
15 | } |
||
16 | |||
17 | if (env('APP_DEBUG') === 'true') { |
||
18 | error_reporting(E_ALL); |
||
19 | ini_set('display_errors', 1); |
||
20 | } |
||
21 | |||
22 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.