ComposerPackageVersion::getPackageVersions()   C
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 2
nop 1
1
<?php
2
/**
3
 * Describes a installed composer package version.
4
 *
5
 * This is a DataObject to allow saving the information in the db to use the information in various ways.
6
 */
7
class ComposerPackageVersion extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    /**
10
     * @var array
11
     */
12
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
        'Scope' => "Enum('Global,Project','Project')",
14
        'Package' => 'Varchar(255)',
15
        'Version' => 'Varchar(255)',
16
    );
17
18
    /**
19
     * @var array
20
     */
21
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
        'Scope',
23
        'Package',
24
        'Version',
25
    );
26
27
    /**
28
     * requireDefaultRecords() gets abused to update the information on dev/build.
29
     */
30
    public function requireDefaultRecords()
31
    {
32
        parent::requireDefaultRecords();
33
34
        // delete all records of this object
35
        foreach (self::get() as $package) {
36
            $package->delete();
37
        }
38
39
        // generate the records new
40
        $globalPackages = $this->getPackageVersions(true);
41
        $packages = $this->getPackageVersions();
42
43
        // write all the package information
44
        foreach (array_merge($globalPackages, $packages) as $package) {
45
            $instance = self::create($package);
46
            $instance->write();
47
        }
48
    }
49
50
    /**
51
     * returns a prepared list of the packages.
52
     *
53
     * @param bool $global (default: false)
54
     *
55
     * @return array
56
     */
57
    public function getPackageVersions($global = false)
58
    {
59
        // different commands for project specific and global packages
60
        if ($global) {
61
            exec('php ../vendor/composer/composer/bin/composer global show --installed 2> /dev/null', $packages);
62
        } else {
63
            exec('php ../vendor/composer/composer/bin/composer show --installed --working-dir=.. 2> /dev/null', $packages);
64
        }
65
66
        // prepare the array
67
        return array_map(function ($package) use (&$global) {
68
            // some regex to parse the package string
69
            $elements = preg_split('/\s+/', $package);
70
71
            // prep a new array.
72
            $match = array(
73
                'Scope' => ($global) ? 'Global' : 'Project',
74
                'Package' => $elements[0],
75
            );
76
77
            // ensure to keep the hash if it has been defined
78
            if (true
79
                && array_key_exists(2, $elements)
80
                && strlen($elements[2]) == 7
81
                && strtolower($elements[2]) == $elements[2]
82
            ) {
83
                $match['Version'] = $elements[1].' '.$elements[2];
84
            } else {
85
                $match['Version'] = $elements[1];
86
            }
87
88
            // return
89
            return $match;
90
        }, $packages);
91
    }
92
}
93