Completed
Pull Request — 1.0 (#853)
by Rob
02:21
created

UpgradeNotice::doWrite()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Utils\Composer;
13
14
use Composer\Script\Event;
15
16
final class UpgradeNotice
17
{
18
    /**
19
     * @param Event $event
20
     */
21
    final static public function doWrite(Event $event)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
22
    {
23
        $package = $event->getComposer()->getPackage();
24
        $version = substr($package->getVersion(), 0, 5);
25
        $console = new ConsoleIO($event->getIO(), $package);
26
27
        if (false === version_compare(PHP_VERSION, '5.4', '>=')
28
         || false === $notice = static::getUpgradeNotice($version)) {
0 ignored issues
show
Comprehensibility introduced by
Since Liip\ImagineBundle\Utils\Composer\UpgradeNotice is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
29
            return;
30
        }
31
32
        $console->write('<error> Update Notice: %s </error>', array($version));
33
34
        foreach ($notice as $line) {
35
            $console->write($line);
36
        }
37
38
        if ($console->isInteractive()) {
39
            $console->ask('Press [ENTER] to acknowledge the above upgrade notice...');
40
        }
41
    }
42
43
    /**
44
     * Read in updates file and parse for the passed package version.
45
     *
46
     * @param string $version
47
     *
48
     * @return string[]|false
49
     */
50
    final static private function getUpgradeNotice($version)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
51
    {
52
        $lines = array_map(function ($line) {
53
            return preg_replace('{^[\s]{2}}', '', rtrim($line));
54
        }, static::readUpgradeFile());
0 ignored issues
show
Comprehensibility introduced by
Since Liip\ImagineBundle\Utils\Composer\UpgradeNotice is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
55
56
        $lines = array_filter($lines, function ($line) use ($version) {
57
            return static::filterLine($line, $version);
0 ignored issues
show
Comprehensibility introduced by
Since Liip\ImagineBundle\Utils\Composer\UpgradeNotice is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
58
        });
59
60
        array_shift($lines);
61
62
        return 0 === count($lines) ? false : $lines;
63
    }
64
65
    /**
66
     * Keep lines if header matches package version, up until the next header is reached.
67
     *
68
     * @param string $line
69
     * @param string $version
70
     *
71
     * @return bool
72
     */
73
    final static private function filterLine($line, $version)
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
74
    {
75
        static $keepLine = false;
76
77
        if (0 === strpos($line, '##')) {
78
            preg_match_all('{(?<version>[0-9]+\.[0-9]+(?:\.[0-9]+)?)}', $line, $matches);
79
80
            $keepLine = count(array_filter($matches['version'], function ($v) use ($version) {
81
                return false !== strpos($version, $v);
82
            })) > 0;
83
        }
84
85
        return $keepLine;
86
    }
87
88
    /**
89
     * Attempt to read the file to an array of lines.
90
     *
91
     * @return string[]
92
     */
93
    final static private function readUpgradeFile()
0 ignored issues
show
Coding Style introduced by
Unnecessary FINAL modifier in FINAL class
Loading history...
94
    {
95
        if (false !== $lines = @file(__DIR__ . '/../../UPGRADE.md')) {
96
            return $lines;
97
        }
98
99
        return array();
100
    }
101
}
102