Passed
Push — master ( 247a52...f18ab4 )
by Melech
07:21 queued 03:19
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Application;
15
16
use Valkyrja\Application\Constant\ComponentClass;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Constant\ComponentClass 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...
17
use Valkyrja\Application\Constant\ConfigName;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Constant\ConfigName 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...
18
use Valkyrja\Application\Constant\EnvName;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Constant\EnvName 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...
19
use Valkyrja\Application\Contract\Application;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Application\Contract\Application 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...
20
use Valkyrja\Application\Support\Component;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Valkyrja\Application\Component. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use Valkyrja\Config\Config as ParentConfig;
22
use Valkyrja\Support\Directory;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Support\Directory 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...
23
24
/**
25
 * Class Config.
26
 *
27
 * @author Melech Mizrachi
28
 */
29
class Config extends ParentConfig
30
{
31
    /**
32
     * @inheritDoc
33
     *
34
     * @var array<string, string>
35
     */
36
    protected static array $envNames = [
37
        ConfigName::ENV             => EnvName::ENV,
38
        ConfigName::DEBUG_MODE      => EnvName::DEBUG_MODE,
39
        ConfigName::URL             => EnvName::URL,
40
        ConfigName::TIMEZONE        => EnvName::TIMEZONE,
41
        ConfigName::VERSION         => EnvName::VERSION,
42
        ConfigName::KEY             => EnvName::KEY,
43
        ConfigName::COMPONENTS      => EnvName::COMPONENTS,
44
        ConfigName::CACHE_FILE_PATH => EnvName::CACHE_FILE_PATH,
45
    ];
46
47
    /**
48
     * @param non-empty-string          $env        The environment
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
49
     * @param non-empty-string          $url        The url
50
     * @param non-empty-string          $timezone   The timezone
51
     * @param non-empty-string          $version    The version
52
     * @param non-empty-string          $key        The secret key
53
     * @param class-string<Component>[] $components The components
54
     */
55
    public function __construct(
56
        public string $env = 'production',
57
        public bool $debugMode = false,
58
        public string $url = 'localhost',
59
        public string $timezone = 'UTC',
60
        public string $version = Application::VERSION,
61
        public string $key = 'some_secret_app_key',
62
        public array $components = [],
63
        public string $cacheFilePath = ''
64
    ) {
65
    }
66
67
    public function setPropertiesFromEnv(string $env): void
68
    {
69
        if ($this->cacheFilePath === '') {
70
            $this->cacheFilePath = Directory::cachePath('config.php');
71
        }
72
73
        $this->components = [
74
            ComponentClass::API,
75
            ComponentClass::ASSET,
76
            ComponentClass::AUTH,
77
            ComponentClass::BROADCAST,
78
            ComponentClass::CACHE,
79
            ComponentClass::CRYPT,
80
            ComponentClass::FILESYSTEM,
81
            ComponentClass::HTTP_CLIENT,
82
            ComponentClass::JWT,
83
            ComponentClass::LOG,
84
            ComponentClass::MAIL,
85
            ComponentClass::NOTIFICATION,
86
            ComponentClass::ORM,
87
            ComponentClass::SESSION,
88
            ComponentClass::SMS,
89
            ComponentClass::VIEW,
90
        ];
91
92
        parent::setPropertiesFromEnv($env);
93
    }
94
}
95