Passed
Pull Request — master (#211)
by Michele
02:45
created

Version   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 12
Bugs 1 Features 0
Metric Value
wmc 15
eloc 35
c 12
b 1
f 0
dl 0
loc 97
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersionFromHeaderFile() 0 8 2
A updateJSON() 0 16 3
A getVersionFromHeaderFileContents() 0 7 2
A getVersionFromHeader() 0 10 3
A fileHasVersionMacro() 0 3 1
A rglob() 0 7 2
A __toString() 0 3 1
A __construct() 0 6 1
1
<?php
2
3
/*
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Package\Util\Header;
38
39
use Composer\Package\Version\VersionParser;
40
use Exception;
41
use Pickle\Base\Interfaces;
42
use Pickle\Package\Util;
43
44
class Version
45
{
46
    protected $package;
47
48
    protected $header;
49
50
    protected $version;
51
52
    protected $macroNameRegex;
53
54
    public function __construct(Interfaces\Package $package)
55
    {
56
        $this->package = $package;
57
        $this->macroNameRegex = '(?:PHP_)?' . strtoupper($this->package->getSimpleName()) . '_(?:EXT_)?VERSION';
0 ignored issues
show
Bug introduced by
The method getSimpleName() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $this->macroNameRegex = '(?:PHP_)?' . strtoupper($this->package->/** @scrutinizer ignore-call */ getSimpleName()) . '_(?:EXT_)?VERSION';
Loading history...
58
59
        $this->version = $this->getVersionFromHeader();
60
    }
61
62
    public function __toString()
63
    {
64
        return $this->version;
65
    }
66
67
    public function fileHasVersionMacro($fname)
68
    {
69
        return $this->getVersionFromHeaderFile($fname) !== null;
70
    }
71
72
    public function getVersionFromHeader()
73
    {
74
        $headers = self::rglob($this->package->getSourceDir() . DIRECTORY_SEPARATOR . '*.h');
0 ignored issues
show
Bug Best Practice introduced by
The method Pickle\Package\Util\Header\Version::rglob() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
        /** @scrutinizer ignore-call */ 
75
        $headers = self::rglob($this->package->getSourceDir() . DIRECTORY_SEPARATOR . '*.h');
Loading history...
75
        foreach ($headers as $header) {
76
            $macroValue = $this->getVersionFromHeaderFile($header);
77
            if ($macroValue !== null) {
78
                return $macroValue;
79
            }
80
        }
81
        throw new Exception("Couldn't parse or find the version defined in the {$this->macroNameRegex} macro");
82
    }
83
84
    public function updateJSON()
85
    {
86
        if ($this->package->getPrettyVersion() == $this->version) {
87
            /* Don't touch, it's the same. */
88
            return;
89
        }
90
91
        $dumper = new Util\Dumper();
92
        $composer_json = $this->package->getRootDir() . DIRECTORY_SEPARATOR . 'composer.json';
93
94
        $this->package->replaceVersion((new VersionParser())->normalize($this->version), $this->version);
0 ignored issues
show
Bug introduced by
The method replaceVersion() does not exist on Pickle\Base\Interfaces\Package. Since it exists in all sub-types, consider adding an abstract or default implementation to Pickle\Base\Interfaces\Package. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
        $this->package->/** @scrutinizer ignore-call */ 
95
                        replaceVersion((new VersionParser())->normalize($this->version), $this->version);
Loading history...
95
96
        $len = file_put_contents($composer_json, json_encode($dumper->dump($this->package), JSON_PRETTY_PRINT));
97
98
        if (!$len) {
99
            throw new Exception("Failed to update '{$package_json}'");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $package_json seems to be never defined.
Loading history...
100
        }
101
    }
102
103
    /**
104
     * Extract the macro value from a header file.
105
     *
106
     * @throws Exception if we couldn't read the file
107
     *
108
     * @return string|null return NULL if the macro couldn't be found, the macro value otherwise
109
     */
110
    protected function getVersionFromHeaderFile(string $headerFilePath): ?string
111
    {
112
        $headerFileContents = file_get_contents($headerFilePath);
113
        if ($headerFileContents === false) {
114
            throw new Exception("Could not read header file {$headerFilePath}");
115
        }
116
117
        return $this->getVersionFromHeaderFileContents($headerFileContents);
118
    }
119
120
    /**
121
     * Extract the macro value from the contents of a header file.
122
     *
123
     * @return string|null return NULL if the macro couldn't be found, the macro value otherwise
124
     */
125
    protected function getVersionFromHeaderFileContents(string $headerFileContents): ?string
126
    {
127
        // Match versions surrounded by quotes and versions without quotes
128
        $versionMatcher = '(".*"|.*\b)';
129
        $pat = ',define\s+' . $this->macroNameRegex . '\s+' . $versionMatcher . ',i';
130
131
        return preg_match($pat, $headerFileContents, $result) ? trim($result[1], '"') : null;
132
    }
133
134
    private function rglob($pattern, $flags = 0)
135
    {
136
        $files = glob($pattern, $flags);
137
        foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
138
            $files = array_merge($files, self::rglob($dir . '/' . basename($pattern), $flags));
0 ignored issues
show
Bug Best Practice introduced by
The method Pickle\Package\Util\Header\Version::rglob() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
            $files = array_merge($files, self::/** @scrutinizer ignore-call */ rglob($dir . '/' . basename($pattern), $flags));
Loading history...
139
        }
140
        return $files;
141
    }
142
}
143
144
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
145