UpgradePath::message()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Does what the description says ;)
4
 */
5
class UpgradePath extends BuildTask
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...
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $title = "Upgrade Path checker";
11
12
    /**
13
     * @var string
14
     */
15
    protected $description = 'Checks if which composer dependencies hold your SilverStripe framework upgrade back.';
16
17
    /**
18
     * @param SS_HTTPRequest $request
19
     */
20
    public function run($request)
21
    {
22
        // safe is safe!
23
        if (Permission::check('ADMIN') || $this->isCLI()) {
24
            // get the list of all full releases
25
            $releases = $this->getReleases();
26
            $this->message('Framework versions to consider: ' . join(', ', $releases));
27
            $this->message("--------------------------------------------");
28
29
            // check and print the result
30
            foreach ($releases as $release) {
31
                $this->message($this->getBlockers($release));
32
            }
33
34
            // Done :)
35
            $this->message("\nDone. The information above was not saved anywhere in the DB :)");
36
        } else {
37
            $this->message('Permission issue.');
38
        }
39
    }
40
41
    /**
42
     * for now just return a list of the interesting versions. Later a proper list of all newer versions would be great.
43
     *
44
     * @return array
45
     */
46
    public function getReleases()
47
    {
48
        return array('4.0.0');
49
    }
50
51
    /**
52
     * returns a prepared list of the packages.
53
     *
54
     * parses the following output for convienence reasons:
55
     *
56
     * $ peter@petert-lp /var/www/project (master) $ composer why-not silverstripe/framework:3.3.0
57
     * $ nglasl/silverstripe-misdirection  dev-master  requires  silverstripe/framework (3.1.*)
58
     * $ silverstripe/sqlite3              1.3.x-dev   requires  silverstripe/framework (>=3.0,<3.2)
59
     * $ silverstripe/widgets              1.1.x-dev   requires  silverstripe/framework (3.1.*)
60
     * $ unclecheese/display-logic         1.2.x-dev   requires  silverstripe/framework (3.1.*)
61
     *
62
     * @param string $release
63
     *
64
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
     */
66
    public function getBlockers($release)
67
    {
68
        // get the prohibiting packages for a particular framework version
69
        // @TODO refactor to use actual composer package directly instead of via the cli.
70
        exec(
71
            sprintf(
72
                'php %s %s silverstripe/framework:%s --working-dir=.. 2> /dev/null',
73
                '../vendor/composer/composer/bin/composer',
74
                'why-not',
75
                $release
76
            ),
77
            $packages
78
        );
79
80
        // prepare the output
81
        return
82
            "\n".trim(
83
            "Target SilverStripe Framework Version: {$release}\n".
84
            "--------------------------------------------\n\n".
85
            join("", array_unique(array_map(function ($package) use ($release) {
86
                // some string processing to parse the package string
87
                $elements = preg_split('/\s+/', $package);
88
                $limitation = substr($elements[4], 1, -1);
89
90
                // a simple version parser, which should be enough for this purpose:
91
                $limitationAsNumber = preg_replace('/\D/', '', $limitation);
92
                $limitationAsNumber = $limitationAsNumber . str_repeat('0', 10 - strlen($limitationAsNumber));
93
                $releaseAsNumber = preg_replace('/\D/', '', $release);
94
                $releaseAsNumber = $releaseAsNumber . str_repeat('0', 10 - strlen($releaseAsNumber));
95
96
                // check if this is actual an upgrade. Still could be a downgrade.
97
                if ($releaseAsNumber < $limitationAsNumber) {
98
                    return 'This would be a downgrade.';
99
                }
100
                if ($releaseAsNumber > $limitationAsNumber) {
101
                    // prep a new array.
102
                    return sprintf(
103
                        "Package: %s\n - maximum supported version: %s\n\n",
104
                        $elements[0],
105
                        $limitation
106
                    );
107
                }
108
            return $result; }, $packages))));
0 ignored issues
show
Bug introduced by
The variable $result does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
109
    }
110
111
    /**
112
     * @var boolean
113
     */
114
    protected function isCLI()
115
    {
116
        return (PHP_SAPI === 'cli');
117
    }
118
119
    /**
120
     * prints a message during the run of the task
121
     *
122
     * @param string $text
123
     */
124
    protected function message($text)
125
    {
126
        if (!$this->isCLI()) {
127
            $text = '<pre>' . $text . '</pre>' . PHP_EOL;
128
        }
129
130
        echo $text . PHP_EOL;
131
    }
132
}
133