Completed
Push — feature/ss4-upgrade ( f41a3f )
by
unknown
10:12
created

PackagistService::getPackageVersions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace SilverStripe\Addons\Services;
4
5
use Composer\Factory;
6
use Composer\IO\NullIO;
7
use Composer\Package\Loader\ArrayLoader;
8
use Composer\Repository\ComposerRepository;
9
use Guzzle\Http\Client;
10
11
/**
12
 * Interacts with Packagist to retrieve package listings and details.
13
 */
14
class PackagistService 
15
{
16
17
	/**
18
	 * @var Composer\Composer
19
	 */
20
	private $composer;
21
22
	/**
23
	 * @var Composer\Repository\RepositoryInterface
24
	 */
25
	private $repository;
26
27
	public function __construct() 
28
	{
29
		$this->composer = Factory::create(new NullIO());
0 ignored issues
show
Documentation Bug introduced by
It seems like \Composer\Factory::creat... \Composer\IO\NullIO()) of type object<Composer\Composer> is incompatible with the declared type object<SilverStripe\Addo...ices\Composer\Composer> of property $composer.

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...
30
		$this->client = new Packagist\Api\Client();
0 ignored issues
show
Bug introduced by
The property client 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...
31
	}
32
33
	/**
34
	 * @return Composer\Composer
35
	 */
36
	public function getComposer() 
37
	{
38
		return $this->composer;
39
	}
40
41
	/**
42
	 * Gets all SilverStripe packages.
43
	 *
44
	 * @return Packagist\Api\Package[]
45
	 */
46
	public function getPackages() 
47
	{
48
		$packages = array();
49
		$loader = new ArrayLoader();
0 ignored issues
show
Unused Code introduced by
$loader is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
51
		$addonTypes = array(
52
			'silverstripe-module',
53
			'silverstripe-theme'
54
		);
55
		foreach ($addonTypes as $type) {
56
			$repositoriesNames = $this->client->all(array('type' => $type));
57
			foreach ($repositoriesNames as $name) {
58
				$packages[] = $this->client->get($name);
59
				echo $name . PHP_EOL; //output to give feedback when running
60
			}
61
		}
62
		return $packages;
63
	}
64
65
	/**
66
	 * Gets all SilverStripe packages, grouped by package name.
67
	 *
68
	 * @return array
69
	 */
70
	public function getGroupedPackages() 
71
	{
72
		$grouped = array();
73
74
		foreach ($this->getPackages() as $package) {
75
			$name = $package->getName();
76
77
			if (array_key_exists($name, $grouped)) {
78
				$grouped[$name][] = $package;
79
			} else {
80
				$grouped[$name] = array($package);
81
			}
82
		}
83
84
		return $grouped;
85
	}
86
87
	/**
88
	 * Gets detailed information for a package.
89
	 *
90
	 * @param string $name
91
	 * @return array
92
	 */
93
	public function getPackageDetails($name) 
94
	{
95
		return $this->client->get($name);
96
	}
97
98
	/**
99
	 * Gets all versions of a package by name.
100
	 *
101
	 * @param $name
102
	 * @return \Composer\Package\PackageInterface[]
103
	 */
104
	public function getPackageVersions($name) 
105
	{
106
		$packages = array();
107
		$loader = new ArrayLoader();
0 ignored issues
show
Unused Code introduced by
$loader is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
109
		$package = $this->client->get($name);
110
111
		foreach ($package->getVersions() as $repo) {
112
			$packages[] = $repo;
113
		}
114
115
		return $packages;
116
	}
117
118
}
119