1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Symfony\Component\Process\Process; |
13
|
|
|
|
14
|
|
|
class Util |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Symfony\Component\Filesystem. |
18
|
|
|
* |
19
|
|
|
* @var Filesystem |
20
|
|
|
*/ |
21
|
|
|
protected static $fs; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return Symfony\Component\Filesystem instance. |
25
|
|
|
* |
26
|
|
|
* @return Filesystem |
27
|
|
|
*/ |
28
|
|
|
public static function getFS() |
29
|
|
|
{ |
30
|
|
|
if (!self::$fs instanceof Filesystem) { |
31
|
|
|
self::$fs = new Filesystem(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return self::$fs; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Runs a Git command on the repository. |
39
|
|
|
* |
40
|
|
|
* @param string $command The command. |
41
|
|
|
* |
42
|
|
|
* @throws \RuntimeException If the command failed. |
43
|
|
|
* |
44
|
|
|
* @return string The trimmed output from the command. |
45
|
|
|
*/ |
46
|
|
|
public static function runGitCommand($command) |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$process = new Process($command, __DIR__); |
|
|
|
|
50
|
|
|
if (0 === $process->run()) { |
51
|
|
|
return trim($process->getOutput()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
throw new \RuntimeException( |
55
|
|
|
sprintf( |
56
|
|
|
'The tag or commit hash could not be retrieved from "%s": %s', |
57
|
|
|
__DIR__, |
58
|
|
|
$process->getErrorOutput() |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} catch (\RuntimeException $exception) { |
62
|
|
|
throw new \RuntimeException('Process error'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sort array items by date. |
68
|
|
|
* |
69
|
|
|
* @param $a |
70
|
|
|
* @param $b |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public static function sortByDate($a, $b) |
75
|
|
|
{ |
76
|
|
|
if (!isset($a['date'])) { |
77
|
|
|
return -1; |
78
|
|
|
} |
79
|
|
|
if (!isset($b['date'])) { |
80
|
|
|
return 1; |
81
|
|
|
} |
82
|
|
|
if ($a['date'] == $b['date']) { |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return ($a['date'] > $b['date']) ? -1 : 1; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Checks if a date is valid. |
91
|
|
|
* |
92
|
|
|
* @param string $date |
93
|
|
|
* @param string $format |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public static function dateIsValid(string $date, string $format = 'Y-m-d'): bool |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$d = \DateTime::createFromFormat($format, $date); |
100
|
|
|
|
101
|
|
|
return $d && $d->format($format) === $date; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Date to DateTime. |
106
|
|
|
* |
107
|
|
|
* @param mixed $date |
108
|
|
|
* |
109
|
|
|
* @return \DateTime |
110
|
|
|
*/ |
111
|
|
|
public static function dateToDatetime($date): \DateTime |
112
|
|
|
{ |
113
|
|
|
// DateTime |
114
|
|
|
if ($date instanceof \DateTime) { |
115
|
|
|
return $date; |
116
|
|
|
} |
117
|
|
|
// timestamp or AAAA-MM-DD |
118
|
|
|
if (is_numeric($date)) { |
119
|
|
|
return (new \DateTime())->setTimestamp($date); |
120
|
|
|
} |
121
|
|
|
// string (ie: '01/01/2019', 'today') |
122
|
|
|
if (is_string($date)) { |
123
|
|
|
return new \DateTime($date); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Format class name. |
129
|
|
|
* |
130
|
|
|
* @param \object $class |
131
|
|
|
* @param array $options |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public static function formatClassName($class, array $options = []): string |
136
|
|
|
{ |
137
|
|
|
$lowercase = false; |
138
|
|
|
extract($options); |
139
|
|
|
|
140
|
|
|
$className = substr(strrchr(get_class($class), '\\'), 1); |
141
|
|
|
if ($lowercase) { |
142
|
|
|
$className = strtolower($className); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $className; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* search multidimensional array for key and return value. |
150
|
|
|
*/ |
151
|
|
|
public function arraySearchByKey(string $keyVal, array $array) |
152
|
|
|
{ |
153
|
|
|
foreach ($array as $key => $val) { |
154
|
|
|
if ($keyVal == $key) { |
155
|
|
|
$resultSet['name'] = $val['name']; |
|
|
|
|
156
|
|
|
$resultSet['key'] = $key; |
157
|
|
|
$resultSet['id'] = $val['id']; |
158
|
|
|
|
159
|
|
|
return $resultSet; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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: