Passed
Pull Request — master (#26)
by Dmitriy
12:03
created

EnvReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 12
c 1
b 0
f 1
dl 0
loc 28
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A readRaw() 0 9 2
A loadEnvs() 0 8 3
1
<?php
2
3
namespace Yiisoft\Composer\Config\Readers;
4
5
use Dotenv\Dotenv;
0 ignored issues
show
Bug introduced by
The type Dotenv\Dotenv was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Yiisoft\Composer\Config\Exceptions\UnsupportedFileTypeException;
7
8
/**
9
 * EnvReader - reads `.env` files.
10
 */
11
class EnvReader extends AbstractReader
12
{
13
    protected function readRaw(string $path)
14
    {
15
        if (!class_exists(Dotenv::class)) {
16
            throw new UnsupportedFileTypeException('for .env support require `vlucas/phpdotenv` in your composer.json');
17
        }
18
        $info = pathinfo($path);
19
        $this->loadEnvs($info['dirname'], $info['basename']);
20
21
        return $_ENV;
22
    }
23
24
    /**
25
     * Creates and loads Dotenv object.
26
     * Supports all 2, 3 and 4 version of `phpdotenv`
27
     *
28
     * @param mixed $dir
29
     * @param mixed $file
30
     */
31
    private function loadEnvs(string $dir, string $file): void
32
    {
33
        if (method_exists(Dotenv::class, 'createMutable')) {
34
            Dotenv::createMutable($dir, $file)->load();
35
        } elseif (method_exists(Dotenv::class, 'create')) {
36
            Dotenv::create($dir, $file)->overload();
37
        } else {
38
            (new Dotenv($dir, $file))->overload();
39
        }
40
    }
41
}
42