PackagistService::getPackages()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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