Passed
Push — 0.7.0 ( 7cf8ff...8cbd21 )
by Alexander
04:15 queued 11s
created

RepositoryCreator::addAdapter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Dotenv\Repository;
24
25
use InvalidaArgumentException;
0 ignored issues
show
Bug introduced by
The type InvalidaArgumentException 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...
26
use Syscodes\Contracts\Dotenv\Adapter;
27
use Syscodes\Dotenv\Repository\Adapters\Readers;
28
use Syscodes\Dotenv\Repository\Adapters\Writers;
29
use Syscodes\Dotenv\Repository\Adapters\EnvAdapter;
30
use Syscodes\Dotenv\Repository\Adapters\ServerAdapter;
31
32
/**
33
 * Allows you to bring all the adapters.
34
 * 
35
 * @author Alexander Campo <[email protected]>
36
 */
37
final class RepositoryCreator
38
{
39
    protected const ADAPTERS_DEFAULT = [
40
        EnvAdapter::class,
41
        ServerAdapter::class,
42
    ];
43
44
    /**
45
     * Gets adapters allow list to use.
46
     * 
47
     * @var string[] $allowlist
48
     */
49
    protected $allowlist;
50
51
    /**
52
     * The set of readers to use.
53
     * 
54
     * @var \Syscodes\Dotenv\Repository\Adapters\Readers $readers
55
     */
56
    protected $readers;
57
58
    /**
59
     * The set of writers to use.
60
     * 
61
     * @var \Syscodes\Dotenv\Repository\Adapters\Writers $writers
62
     */
63
    protected $writers;
64
65
    /**
66
     * Constructor. Create a new Repository creator instance.
67
     * 
68
     * @param  \Syscodes\Dotenv\Repository\Adapters\Readers  $readers
69
     * @param  \Syscodes\Dotenv\Repository\Adapters\Writers  $writers
70
     * @param  string[]|null  $allowList
71
     * 
72
     * @return void
73
     */
74
    public function __construct(array $readers = [], array $writers = [], array $allowList = null)
75
    {
76
        $this->readers   = $readers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $readers of type array is incompatible with the declared type Syscodes\Dotenv\Repository\Adapters\Readers of property $readers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->writers   = $writers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $writers of type array is incompatible with the declared type Syscodes\Dotenv\Repository\Adapters\Writers of property $writers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
        $this->allowList = $allowList;
0 ignored issues
show
Bug Best Practice introduced by
The property allowList does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
    }
80
81
    /**
82
     * Create a new repository creator instance with the default adapters added.
83
     * 
84
     * @return \Syscodes\Dotenv\Repository\RepositoryCreator
85
     */
86
    public static function createDefaultAdapters()
87
    {
88
        $adapters = iterator_to_array(self::defaultAdapters());
0 ignored issues
show
Bug introduced by
self::defaultAdapters() of type Syscodes\Contracts\Dotenv\Adapter is incompatible with the type Traversable expected by parameter $iterator of iterator_to_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        $adapters = iterator_to_array(/** @scrutinizer ignore-type */ self::defaultAdapters());
Loading history...
89
90
        return new static($adapters, $adapters);
91
    }
92
    
93
    /**
94
     * Return the array of default adapters.
95
     * 
96
     * @return \Syscodes\Contracts\Dotenv\Adapter
97
     */
98
    protected static function defaultAdapters()
99
    {
100
        foreach (self::ADAPTERS_DEFAULT as $adapter) {
101
            return new $adapter;
102
        }
103
    }
104
105
    /**
106
     * Determine if the given name if of an adapter class.
107
     * 
108
     * @param  string  $name
109
     * 
110
     * @return bool
111
     */
112
    protected static function inAdapterClass(string $name)
113
    {
114
        if ( ! class_exists($name))
115
        {
116
            return false;
117
        }
118
119
        return (new ReflectionClass($name))->implementsInterface(Adapter::class);
0 ignored issues
show
Bug introduced by
The type Syscodes\Dotenv\Repository\ReflectionClass was not found. Did you mean ReflectionClass? If so, make sure to prefix the type with \.
Loading history...
120
    }
121
122
    /**
123
     * Creates a repository builder with the given reader added.
124
     * 
125
     * @param  string  $adapter
126
     * 
127
     * @return new static
0 ignored issues
show
Bug introduced by
The type Syscodes\Dotenv\Repository\new 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...
128
     * 
129
     * @return \InvalidArgumentException
130
     */
131
    public function addAdapter(string $adapter)
132
    {
133
        if ( ! is_string($adapter) && ! ($adapter instanceof Adapter)) {
0 ignored issues
show
introduced by
The condition is_string($adapter) is always true.
Loading history...
134
            throw new InvalidaArgumentException("Expected either an instance of [{$this->allowList}]");
135
        }
136
137
        $readers = array_merge($this->readers, [$adapter]);
0 ignored issues
show
Bug introduced by
$this->readers of type Syscodes\Dotenv\Repository\Adapters\Readers is incompatible with the type array expected by parameter $arrays of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $readers = array_merge(/** @scrutinizer ignore-type */ $this->readers, [$adapter]);
Loading history...
138
        $writers = array_merge($this->writers, [$adapter]);
0 ignored issues
show
Bug introduced by
$this->writers of type Syscodes\Dotenv\Repository\Adapters\Writers is incompatible with the type array expected by parameter $arrays of array_merge(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

138
        $writers = array_merge(/** @scrutinizer ignore-type */ $this->writers, [$adapter]);
Loading history...
139
140
        return new static($readers, $writers, $this->allowList);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new static($reade...ters, $this->allowList) returns the type Syscodes\Dotenv\Repository\RepositoryCreator which is incompatible with the documented return type Syscodes\Dotenv\Repository\new.
Loading history...
141
    }
142
143
    /**
144
     * Creates a new repository instance.
145
     * 
146
     * @return \Syscodes\Dotenv\Repository\AdapterRepository
147
     */
148
    public function make()
149
    {
150
        $readers = new Readers($this->readers);
0 ignored issues
show
Bug introduced by
$this->readers of type Syscodes\Dotenv\Repository\Adapters\Readers is incompatible with the type array expected by parameter $readers of Syscodes\Dotenv\Reposito...\Readers::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
        $readers = new Readers(/** @scrutinizer ignore-type */ $this->readers);
Loading history...
151
        $writers = new Writers($this->writers);
0 ignored issues
show
Bug introduced by
$this->writers of type Syscodes\Dotenv\Repository\Adapters\Writers is incompatible with the type array expected by parameter $writers of Syscodes\Dotenv\Reposito...\Writers::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
        $writers = new Writers(/** @scrutinizer ignore-type */ $this->writers);
Loading history...
152
153
        return new AdapterRepository($readers, $writers);
154
    }
155
}