Passed
Pull Request — master (#26)
by Dmitriy
46:44 queued 36:08
created

EnvReader::readRaw()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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