Passed
Push — 0.7.0 ( 418a4b...b5eec6 )
by Alexander
02:57
created

RepositoryCreator::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
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 ReflectionClass;
26
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...
27
use Syscodes\Contracts\Dotenv\Adapter;
28
use Syscodes\Dotenv\Repository\Adapters\Readers;
29
use Syscodes\Dotenv\Repository\Adapters\Writers;
30
use Syscodes\Dotenv\Repository\Adapters\EnvAdapter;
31
use Syscodes\Dotenv\Repository\Adapters\ServerAdapter;
32
33
/**
34
 * Allows you to bring all the adapters.
35
 * 
36
 * @author Alexander Campo <[email protected]>
37
 */
38
final class RepositoryCreator
39
{
40
    protected static $adapterDefault = [
41
        EnvAdapter::class,
42
        ServerAdapter::class,
43
    ];
44
45
    /**
46
     * Gets adapters allow list to use.
47
     * 
48
     * @var string[] $allowlist
49
     */
50
    protected $allowlist;
51
52
    /**
53
     * The set of readers to use.
54
     * 
55
     * @var \Syscodes\Dotenv\Repository\Adapters\Readers $readers
56
     */
57
    protected $readers;
58
59
    /**
60
     * The set of writers to use.
61
     * 
62
     * @var \Syscodes\Dotenv\Repository\Adapters\Writers $writers
63
     */
64
    protected $writers;
65
66
    /**
67
     * Constructor. Create a new Repository creator instance.
68
     * 
69
     * @param  \Syscodes\Dotenv\Repository\Adapters\Readers  $readers
70
     * @param  \Syscodes\Dotenv\Repository\Adapters\Writers  $writers
71
     * @param  string[]|null  $allowList
72
     * 
73
     * @return void
74
     */
75
    public function __construct(array $readers = [], array $writers = [], array $allowList = null)
76
    {
77
        $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...
78
        $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...
79
        $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...
80
    }
81
82
    /**
83
     * Create a new repository creator instance with the default adapters added.
84
     * 
85
     * @return \Syscodes\Dotenv\Repository\RepositoryCreator
86
     */
87
    public static function createDefaultAdapters()
88
    {
89
        $adapters = iterator_to_array(self::defaultAdapters());
90
        
91
        return new static($adapters, $adapters);
92
    }
93
    
94
    /**
95
     * Return the array of default adapters.
96
     * 
97
     * @return \Syscodes\Contracts\Dotenv\Adapter
98
     */
99
    protected static function defaultAdapters()
100
    {
101
        foreach (static::$adapterDefault as $adapter) {
102
            yield new $adapter;
0 ignored issues
show
Bug Best Practice introduced by
The expression yield new $adapter() returns the type Generator which is incompatible with the documented return type Syscodes\Contracts\Dotenv\Adapter.
Loading history...
103
        }
104
    }
105
106
    /**
107
     * Determine if the given name if of an adapter class.
108
     * 
109
     * @param  string  $name
110
     * 
111
     * @return bool
112
     */
113
    protected static function inAdapterClass(string $name)
114
    {
115
        if ( ! class_exists($name))
116
        {
117
            return false;
118
        }
119
120
        return (new ReflectionClass($name))->implementsInterface(Adapter::class);
121
    }
122
123
    /**
124
     * Creates a repository builder with the given reader added.
125
     * 
126
     * @param  string  $adapter
127
     * 
128
     * @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...
129
     * 
130
     * @return \InvalidArgumentException
131
     */
132
    public function addAdapter(string $adapter)
133
    {
134
        if ( ! is_string($adapter) && ! ($adapter instanceof Adapter)) {
0 ignored issues
show
introduced by
The condition is_string($adapter) is always true.
Loading history...
135
            throw new InvalidaArgumentException("Expected either an instance of [{$this->allowList}]");
136
        }
137
138
        $adapter = $this->getReflectionClass($adapter);
139
140
        $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

140
        $readers = array_merge(/** @scrutinizer ignore-type */ $this->readers, [$adapter]);
Loading history...
141
        $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

141
        $writers = array_merge(/** @scrutinizer ignore-type */ $this->writers, [$adapter]);
Loading history...
142
143
        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...
144
    }
145
146
    /**
147
     * Gets class.
148
     * 
149
     * @param  string  $class
150
     * 
151
     * @return object
152
     */
153
    protected function getReflectionClass($class)
154
    {
155
        $object = new ReflectionClass($class);
156
157
        return $object->newInstanceWithoutConstructor();
158
    }
159
160
    /**
161
     * Creates a new repository instance.
162
     * 
163
     * @return \Syscodes\Dotenv\Repository\AdapterRepository
164
     */
165
    public function make()
166
    {
167
        $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

167
        $readers = new Readers(/** @scrutinizer ignore-type */ $this->readers);
Loading history...
168
        $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

168
        $writers = new Writers(/** @scrutinizer ignore-type */ $this->writers);
Loading history...
169
170
        return new AdapterRepository($readers, $writers);
171
    }
172
}