Completed
Push — master ( 1a30e2...818da8 )
by Jeroen De
03:02
created

InMemoryFileFetcher::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.2
cc 4
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace FileFetcher;
4
5
use InvalidArgumentException;
6
7
/**
8
 * @since 3.1
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class InMemoryFileFetcher implements FileFetcher {
14
15
	/**
16
	 * @param string[] $files
17
	 * @throws InvalidArgumentException
18
	 */
19
	public function __construct( array $files ) {
20
		foreach ( $files as $url => $fileContents ) {
21
			if ( !is_string( $url ) || !is_string( $fileContents ) ) {
22
				throw new InvalidArgumentException( 'Both file url and file contents need to be of type string' );
23
			}
24
		}
25
26
		$this->files = $files;
0 ignored issues
show
Bug introduced by
The property files does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
	}
28
29
	/**
30
	 * @see FileFetcher::fetchFile
31
	 *
32
	 * @param string $fileUrl
33
	 *
34
	 * @return string
35
	 * @throws FileFetchingException
36
	 */
37
	public function fetchFile( $fileUrl ) {
38
		if ( array_key_exists( $fileUrl, $this->files ) ) {
39
			return $this->files[$fileUrl];
40
		}
41
42
		throw new FileFetchingException( $fileUrl );
43
	}
44
45
}
46