Issues (52)

src/AnalyzeComments/Config/ConfigLoader.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SavinMikhail\CommentsDensity\AnalyzeComments\Config;
6
7
use SavinMikhail\CommentsDensity\AnalyzeComments\Config\DTO\Config;
0 ignored issues
show
The type SavinMikhail\CommentsDen...ments\Config\DTO\Config 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 SavinMikhail\CommentsDensity\AnalyzeComments\Exception\CommentsDensityException;
9
10
use function defined;
11
use function dirname;
12
use function file_exists;
13
14
use const DIRECTORY_SEPARATOR;
15
16
final class ConfigLoader
17
{
18
    private const CONFIG_FILE = 'comments_density.php';
19
20
    private int $dirLevel = 3;
21
22
    /**
23
     * @throws CommentsDensityException
24
     */
25
    public function __construct()
26
    {
27
        if (!defined('COMMENTS_DENSITY_ENVIRONMENT')) {
28
            throw new CommentsDensityException('COMMENTS_DENSITY_ENVIRONMENT is not set');
29
        }
30
    }
31
32
    /**
33
     * @throws CommentsDensityException
34
     */
35
    public function getConfigDto(): Config
36
    {
37
        $config = require $this->getConfigPath();
38
39
        if (!$config instanceof Config) {
40
            throw new CommentsDensityException('Config file must return an instance of ConfigDTO');
41
        }
42
43
        return $config;
44
    }
45
46
    public function getProjectRoot(): string
47
    {
48
        return dirname(__DIR__, $this->dirLevel);
49
    }
50
51
    /**
52
     * @throws CommentsDensityException
53
     */
54
    private function getConfigPath(): string
55
    {
56
        if (COMMENTS_DENSITY_ENVIRONMENT === 'prod') {
0 ignored issues
show
The constant SavinMikhail\CommentsDen...NTS_DENSITY_ENVIRONMENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57
            $this->dirLevel = 6; // if installed directly via composer
58
        }
59
60
        $configFile = $this->getProjectRoot() . DIRECTORY_SEPARATOR . self::CONFIG_FILE;
61
        if (file_exists($configFile)) {
62
            return $configFile;
63
        }
64
65
        $this->dirLevel = 8; // if installed via barmani/composer-bin-plugin
66
        $newConfigFileLocation = $this->getProjectRoot() . DIRECTORY_SEPARATOR . self::CONFIG_FILE;
67
        if (file_exists($newConfigFileLocation)) {
68
            return $newConfigFileLocation;
69
        }
70
71
        throw new CommentsDensityException(
72
            'Config file does not exists! Looking for ' . $configFile . ' and ' . $newConfigFileLocation,
73
        );
74
    }
75
}
76