Completed
Push — master ( 013ef7...4604a2 )
by Radu
07:01
created

Settings::load()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 21
rs 9.8666
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Environment;
6
7
use WebServCo\Framework\Exceptions\ConfigurationException;
8
9
final class Settings
10
{
11
    protected const FILENAME = '.env.ini';
12
13
    public static function load(string $projectPath): bool
14
    {
15
        $filePath = \sprintf('%sconfig/%s', $projectPath, self::FILENAME);
16
        if (!\is_readable($filePath)) {
17
            throw new ConfigurationException('Environment configuration file is not readable.');
18
        }
19
        $data = \parse_ini_file(
20
            $filePath, // filename
21
            // true => "multidimensional array, with the section names and settings included"
22
            false, // process_sections
23
            // \INI_SCANNER_TYPED - tries to convert booleans and numeric types
24
            // INI_SCANNER_NORMAL - everything is a string
25
            \INI_SCANNER_TYPED, // scanner_mode
26
        );
27
        if (!$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
28
            throw new ConfigurationException('Error loading environment configuration file');
29
        }
30
        foreach ($data as $key => $value) {
31
            Setting::set($key, $value);
32
        }
33
        return true;
34
    }
35
}
36