EnvReader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
eloc 12
c 0
b 0
f 0
dl 0
loc 29
ccs 0
cts 12
cp 0
rs 10

2 Methods

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