Completed
Push — master ( 7325fd...d28e66 )
by Daniel
02:04
created

decisionPackageOrPackageDev()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\composer_packages_listing;
30
31
/**
32
 * usefull functions to get quick results
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait ComposerPackagesListing
37
{
38
39
    /**
40
     * Decision between Main or Development packages
41
     *
42
     * @param boolean $devInstead
43
     * @return string
44
     */
45
    private function decisionPackageOrPackageDev($devInstead)
46
    {
47
        $sReturn = 'packages';
48
        if ($devInstead) {
49
            $sReturn = 'packages-dev';
50
        }
51
        return $sReturn;
52
    }
53
54
    /**
55
     * Exposes few Environment details
56
     *
57
     * @return array
58
     */
59
    protected function exposeEnvironmentDetails()
60
    {
61
        $knownValues = [
62
            'AMD64' => 'x64 (64 bit)',
63
            'i386'  => 'x86 (32 bit)',
64
            'i586'  => 'x86 (32 bit)',
65
        ];
66
        return [
67
            'Host Name'                     => php_uname('n'),
68
            'Machine Type'                  => php_uname('m'),
69
            'Operating System Architecture' => $knownValues[php_uname('m')],
70
            'Operating System Name'         => php_uname('s'),
71
            'Operating System Version'      => php_uname('r') . ' ' . php_uname('v'),
72
        ];
73
    }
74
75
    /**
76
     *
77
     * @return array
78
     */
79
    protected function exposePhpDetails()
80
    {
81
        $packageReleaseTimestamp = $this->getFileModifiedTimestampOfFile(PHP_BINARY);
0 ignored issues
show
Documentation introduced by
PHP_BINARY is of type string, but the function expects a object<danielgp\composer_packages_listing\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
        return [
83
            'Aging'            => $this->getPkgAging($packageReleaseTimestamp),
84
            'Description'      => 'PHP is a popular general-purpose scripting language'
85
            . ' that is especially suited to web development',
86
            'Homepage'         => 'https://secure.php.net/',
87
            'License'          => 'PHP License v3.01',
88
            'Notification URL' => '---',
89
            'PHP required'     => '---',
90
            'Package Name'     => 'ZendEngine/PHP',
91
            'Product'          => 'PHP',
92
            'Time'             => date('l, d F Y H:i:s', strtotime($packageReleaseTimestamp)),
93
            'Time as PHP no.'  => strtotime($packageReleaseTimestamp),
94
            'Type'             => 'scripting language',
95
            'Url'              => 'https://github.com/php/php-src',
96
            'Vendor'           => 'The PHP Group',
97
            'Version'          => PHP_VERSION,
98
            'Version no.'      => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
99
        ];
100
    }
101
102
    /**
103
     * Returns Modified date and time of a given file
104
     *
105
     * @param type $fileName
106
     * @return string
107
     */
108
    protected function getFileModifiedTimestampOfFile($fileName, $resultInUtcTimeZone = false)
109
    {
110
        if (!file_exists($fileName)) {
111
            return ['error' => $fileToRead . ' was not found'];
0 ignored issues
show
Bug introduced by
The variable $fileToRead 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...
112
        }
113
        $info    = new \SplFileInfo($fileName);
114
        $sResult = date('Y-m-d H:i:s', $info->getMTime());
115
        if ($resultInUtcTimeZone) {
116
            $sResult = gmdate('Y-m-d H:i:s', $info->getMTime());
117
        }
118
        return $sResult;
119
    }
120
121
    /**
122
     * Returns a complete list of packages and respective details from a composer.lock file
123
     *
124
     * @param string $fileToRead
125
     * @return array
126
     */
127
    protected function getPackageDetailsFromGivenComposerLockFile($fileToRead, $devInstead = false)
128
    {
129
        if (!file_exists($fileToRead)) {
130
            return ['error' => $fileToRead . ' was not found'];
131
        }
132
        $dNA      = '---';
133
        $alnfo    = [];
134
        $packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead);
135
        foreach ($packages[$this->decisionPackageOrPackageDev($devInstead)] as $value) {
136
            $atr                   = $this->getPkgOptAtributeAll($value, $dNA);
137
            $basic                 = $this->getPkgBasicInfo($value, $dNA);
138
            $vrs                   = $this->getPkgVersion($value, $dNA);
139
            $alnfo[$value['name']] = array_merge($atr, $basic, $vrs, $this->getPkgTiming($value, $dNA));
140
            ksort($alnfo[$value['name']]);
141
        }
142
        ksort($alnfo);
143
        return $alnfo;
144
    }
145
146
    private function getPkgAging($timePkg)
147
    {
148
        $dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today')));
149
        $dateTime      = new \DateTime(date('Y-m-d', strtotime($timePkg)));
150
        $interval      = $dateTimeToday->diff($dateTime);
151
        return $interval->format('%a days ago');
152
    }
153
154
    private function getPkgBasicInfo($value, $defaultNA)
155
    {
156
        return [
157
            'License'      => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA),
158
            'Package Name' => $value['name'],
159
            'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA),
160
            'Product'      => explode('/', $value['name'])[1],
161
            'Vendor'       => explode('/', $value['name'])[0],
162
        ];
163
    }
164
165
    private function getPkgFileInListOfPackageArrayOut($fileToRead)
166
    {
167
        $handle       = fopen($fileToRead, 'r');
168
        $fileContents = fread($handle, filesize($fileToRead));
169
        fclose($handle);
170
        return json_decode($fileContents, true);
171
    }
172
173
    private function getPkgLcns($license)
174
    {
175
        $lcns = $license;
176
        if (is_array($license)) {
177
            $lcns = implode(', ', $license);
178
        }
179
        return $lcns;
180
    }
181
182
    private function getPkgOptAtributeAll($value, $defaultNA)
183
    {
184
        $attr    = ['description', 'homepage', 'type', 'url', 'version'];
185
        $aReturn = [];
186
        foreach ($attr as $valueA) {
187
            $aReturn[ucwords($valueA)] = $defaultNA;
188
            if (array_key_exists($valueA, $value)) {
189
                $aReturn[ucwords($valueA)] = $value[$valueA];
190
            }
191
        }
192
        return $aReturn;
193
    }
194
195
    private function getPkgTiming($value, $defaultNA)
196
    {
197
        if (isset($value['time'])) {
198
            return [
199
                'Aging'           => $this->getPkgAging($value['time']),
200
                'Time'            => date('l, d F Y H:i:s', strtotime($value['time'])),
201
                'Time as PHP no.' => strtotime($value['time']),
202
            ];
203
        }
204
        return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA];
205
    }
206
207
    private function getPkgVerNo($version)
208
    {
209
        $vrs = $version;
210
        if (substr($version, 0, 1) == 'v') {
211
            $vrs = substr($version, 1, strlen($version) - 1);
212
        }
213
        if (strpos($vrs, '-') !== false) {
214
            $vrs = substr($vrs, 0, strpos($vrs, '-'));
215
        }
216
        return $vrs;
217
    }
218
219
    private function getPkgVersion($value, $defaultNA)
220
    {
221
        if (isset($value['version'])) {
222
            return [
223
                'Notification URL' => $value['notification-url'],
224
                'Version no.'      => $this->getPkgVerNo($value['version']),
225
            ];
226
        }
227
        return ['Notification URL' => $defaultNA, 'Version no.' => $defaultNA];
228
    }
229
}
230