PackagistService::getPackageVersions()   A
last analyzed

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
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
use Composer\Factory;
4
use Composer\IO\NullIO;
5
use Composer\Package\Loader\ArrayLoader;
6
use Composer\Repository\ComposerRepository;
7
8
/**
9
 * Interacts with Packagist to retrieve package listings and details.
10
 */
11
class PackagistService
12
{
13
14
    /**
15
     * @var Composer\Composer
16
     */
17
    private $composer;
18
19
    /**
20
     * @var Composer\Repository\RepositoryInterface
21
     */
22
    private $repository;
23
24
    public function __construct()
25
    {
26
        $this->composer = Factory::create(new NullIO());
27
        $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...
28
    }
29
30
    /**
31
     * @return Composer\Composer
32
     */
33
    public function getComposer()
34
    {
35
        return $this->composer;
36
    }
37
38
    /**
39
     * Gets all SilverStripe packages.
40
     *
41
     * @return Packagist\Api\Package[]
42
     */
43
    public function getPackages()
44
    {
45
        $packages = array();
46
        $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...
47
48
        $addonTypes = array(
49
            'silverstripe-module',
50
            'silverstripe-vendormodule',
51
            'silverstripe-theme'
52
        );
53
        foreach ($addonTypes as $type) {
54
            $repositoriesNames = $this->client->all(array('type' => $type));
55
            foreach ($repositoriesNames as $name) {
56
                $packages[] = $this->client->get($name);
57
                echo $name . PHP_EOL; //output to give feedback when running
58
            }
59
        }
60
        return $packages;
61
    }
62
63
    /**
64
     * Gets all SilverStripe packages, grouped by package name.
65
     *
66
     * @return array
67
     */
68
    public function getGroupedPackages()
69
    {
70
        $grouped = array();
71
72
        foreach ($this->getPackages() as $package) {
73
            $name = $package->getName();
74
75
            if (array_key_exists($name, $grouped)) {
76
                $grouped[$name][] = $package;
77
            } else {
78
                $grouped[$name] = array($package);
79
            }
80
        }
81
82
        return $grouped;
83
    }
84
85
    /**
86
     * Gets detailed information for a package.
87
     *
88
     * @param string $name
89
     * @return array
90
     */
91
    public function getPackageDetails($name)
92
    {
93
        return $this->client->get($name);
94
    }
95
96
    /**
97
     * Gets all versions of a package by name.
98
     *
99
     * @param $name
100
     * @return \Composer\Package\PackageInterface[]
101
     */
102
    public function getPackageVersions($name)
103
    {
104
        $packages = array();
105
        $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...
106
107
        $package = $this->client->get($name);
108
109
        foreach ($package->getVersions() as $repo) {
110
            $packages[] = $repo;
111
        }
112
113
        return $packages;
114
    }
115
}
116