Completed
Push — master ( 0cf574...7325fd )
by Daniel
01:58
created

ComposerPackagesListing::exposePhpDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 19
nc 1
nop 0
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
    protected function exposeEnvironmentDetails()
40
    {
41
        $knownValues = [
42
            'AMD64' => 'x64 (64 bit)',
43
            'i386'  => 'x86 (32 bit)',
44
            'i586'  => 'x86 (32 bit)',
45
        ];
46
        return [
47
            'Host Name'                     => php_uname('n'),
48
            'Machine Type'                  => php_uname('m'),
49
            'Operating System Architecture' => $knownValues[php_uname('m')],
50
            'Operating System Name'         => php_uname('s'),
51
            'Operating System Version'      => php_uname('r') . ' ' . php_uname('v'),
52
        ];
53
    }
54
55
    /**
56
     *
57
     * @return array
58
     */
59
    protected function exposePhpDetails()
60
    {
61
        $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...
62
        return [
63
            'Aging'            => $this->getPkgAging($packageReleaseTimestamp),
64
            'Description'      => 'PHP is a popular general-purpose scripting language'
65
            . ' that is especially suited to web development',
66
            'Homepage'         => 'https://secure.php.net/',
67
            'License'          => 'PHP License v3.01',
68
            'Notification URL' => '---',
69
            'PHP required'     => '---',
70
            'Package Name'     => 'ZendEngine/PHP',
71
            'Product'          => 'PHP',
72
            'Time'             => date('l, d F Y H:i:s', strtotime($packageReleaseTimestamp)),
73
            'Time as PHP no.'  => strtotime($packageReleaseTimestamp),
74
            'Type'             => 'scripting language',
75
            'Url'              => 'https://github.com/php/php-src',
76
            'Vendor'           => 'The PHP Group',
77
            'Version'          => PHP_VERSION,
78
            'Version no.'      => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION,
79
        ];
80
    }
81
82
    /**
83
     * Returns Modified date and time of a given file
84
     *
85
     * @param type $fileName
86
     * @return string
87
     */
88
    protected function getFileModifiedTimestampOfFile($fileName, $resultInUtcTimeZone = false)
89
    {
90
        if (!file_exists($fileName)) {
91
            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...
92
        }
93
        $info    = new \SplFileInfo($fileName);
94
        $sResult = date('Y-m-d H:i:s', $info->getMTime());
95
        if ($resultInUtcTimeZone) {
96
            $sResult = gmdate('Y-m-d H:i:s', $info->getMTime());
97
        }
98
        return $sResult;
99
    }
100
101
    /**
102
     * Returns a complete list of packages and respective details from a composer.lock file
103
     *
104
     * @param string $fileToRead
105
     * @return array
106
     */
107
    protected function getPackageDetailsFromGivenComposerLockFile($fileToRead)
108
    {
109
        if (!file_exists($fileToRead)) {
110
            return ['error' => $fileToRead . ' was not found'];
111
        }
112
        $dNA      = '---';
113
        $alnfo    = [];
114
        $packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead);
115
        foreach ($packages['packages'] as $value) {
116
            $atr                   = $this->getPkgOptAtributeAll($value, $dNA);
117
            $basic                 = $this->getPkgBasicInfo($value, $dNA);
118
            $vrs                   = $this->getPkgVersion($value, $dNA);
119
            $alnfo[$value['name']] = array_merge($atr, $basic, $vrs, $this->getPkgTiming($value, $dNA));
120
            ksort($alnfo[$value['name']]);
121
        }
122
        ksort($alnfo);
123
        return $alnfo;
124
    }
125
126
    private function getPkgAging($timePkg)
127
    {
128
        $dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today')));
129
        $dateTime      = new \DateTime(date('Y-m-d', strtotime($timePkg)));
130
        $interval      = $dateTimeToday->diff($dateTime);
131
        return $interval->format('%a days ago');
132
    }
133
134
    private function getPkgBasicInfo($value, $defaultNA)
135
    {
136
        return [
137
            'License'      => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA),
138
            'Package Name' => $value['name'],
139
            'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA),
140
            'Product'      => explode('/', $value['name'])[1],
141
            'Vendor'       => explode('/', $value['name'])[0],
142
        ];
143
    }
144
145
    private function getPkgFileInListOfPackageArrayOut($fileToRead)
146
    {
147
        $handle       = fopen($fileToRead, 'r');
148
        $fileContents = fread($handle, filesize($fileToRead));
149
        fclose($handle);
150
        return json_decode($fileContents, true);
151
    }
152
153
    private function getPkgLcns($license)
154
    {
155
        $lcns = $license;
156
        if (is_array($license)) {
157
            $lcns = implode(', ', $license);
158
        }
159
        return $lcns;
160
    }
161
162
    private function getPkgOptAtributeAll($value, $defaultNA)
163
    {
164
        $attr    = ['description', 'homepage', 'type', 'url', 'version'];
165
        $aReturn = [];
166
        foreach ($attr as $valueA) {
167
            $aReturn[ucwords($valueA)] = $defaultNA;
168
            if (array_key_exists($valueA, $value)) {
169
                $aReturn[ucwords($valueA)] = $value[$valueA];
170
            }
171
        }
172
        return $aReturn;
173
    }
174
175
    private function getPkgTiming($value, $defaultNA)
176
    {
177
        if (isset($value['time'])) {
178
            return [
179
                'Aging'           => $this->getPkgAging($value['time']),
180
                'Time'            => date('l, d F Y H:i:s', strtotime($value['time'])),
181
                'Time as PHP no.' => strtotime($value['time']),
182
            ];
183
        }
184
        return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA];
185
    }
186
187
    private function getPkgVerNo($version)
188
    {
189
        $vrs = $version;
190
        if (substr($version, 0, 1) == 'v') {
191
            $vrs = substr($version, 1, strlen($version) - 1);
192
        }
193
        if (strpos($vrs, '-') !== false) {
194
            $vrs = substr($vrs, 0, strpos($vrs, '-'));
195
        }
196
        return $vrs;
197
    }
198
199
    private function getPkgVersion($value, $defaultNA)
200
    {
201
        if (isset($value['version'])) {
202
            return [
203
                'Notification URL' => $value['notification-url'],
204
                'Version no.'      => $this->getPkgVerNo($value['version']),
205
            ];
206
        }
207
        return ['Notification URL' => $defaultNA, 'Version no.' => $defaultNA];
208
    }
209
}
210