Completed
Branch master (537795)
by
unknown
33:10
created

ComposerJson::getRequiredDependencies()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 2
nop 0
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Reads a composer.json file and provides accessors to get
5
 * its hash and the required dependencies
6
 *
7
 * @since 1.25
8
 */
9
class ComposerJson {
10
11
	/**
12
	 * @param string $location
13
	 */
14
	public function __construct( $location ) {
15
		$this->contents = json_decode( file_get_contents( $location ), true );
0 ignored issues
show
Bug introduced by
The property contents 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...
16
	}
17
18
	/**
19
	 * Dependencies as specified by composer.json
20
	 *
21
	 * @return array
22
	 */
23
	public function getRequiredDependencies() {
24
		$deps = [];
25
		if ( isset( $this->contents['require'] ) ) {
26
			foreach ( $this->contents['require'] as $package => $version ) {
27
				if ( $package !== "php" && strpos( $package, 'ext-' ) !== 0 ) {
28
					$deps[$package] = self::normalizeVersion( $version );
29
				}
30
			}
31
		}
32
33
		return $deps;
34
	}
35
36
	/**
37
	 * Strip a leading "v" from the version name
38
	 *
39
	 * @param string $version
40
	 * @return string
41
	 */
42
	public static function normalizeVersion( $version ) {
43
		if ( strpos( $version, 'v' ) === 0 ) {
44
			// Composer auto-strips the "v" in front of the tag name
45
			$version = ltrim( $version, 'v' );
46
		}
47
48
		return $version;
49
	}
50
51
}
52