Skip to content

Commit 246d3af

Browse files
committed
Optimizing PHP performance by using
fully-qualified function calls
1 parent c66255f commit 246d3af

2 files changed

Lines changed: 37 additions & 37 deletions

File tree

src/CLI/CLI.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CLI {
6969
*/
7070
public function __construct(array $args = [])
7171
{
72-
if (php_sapi_name() !== "cli") {
72+
if (\php_sapi_name() !== "cli") {
7373
throw new Exception('CLI tasks can only work from the command line');
7474
}
7575

@@ -79,7 +79,7 @@ public function __construct(array $args = [])
7979
Console::error($error->getMessage());
8080
};
8181

82-
@cli_set_process_title($this->command);
82+
@\cli_set_process_title($this->command);
8383
}
8484

8585
/**
@@ -143,10 +143,10 @@ public function task($name)
143143
*/
144144
public function parse(array $args)
145145
{
146-
array_shift($args); // Remove script path from args
146+
\array_shift($args); // Remove script path from args
147147

148148
if(isset($args[0])) {
149-
$this->command = array_shift($args);
149+
$this->command = \array_shift($args);
150150
}
151151
else {
152152
throw new Exception('Missing command');
@@ -155,8 +155,8 @@ public function parse(array $args)
155155
$output = [];
156156

157157
foreach ($args as $arg) {
158-
if(substr($arg, 0, 2) === '--') {
159-
$arg = explode('=', substr($arg, 2));
158+
if(\substr($arg, 0, 2) === '--') {
159+
$arg = \explode('=', \substr($arg, 2));
160160

161161
$output[$arg[0]] = (isset($arg[1])) ? $arg[1] : true;
162162
}
@@ -175,7 +175,7 @@ public function run()
175175
try {
176176
if($command) {
177177
foreach($this->init as $init) {
178-
call_user_func_array($init, array());
178+
\call_user_func_array($init, array());
179179
}
180180

181181
$params = [];
@@ -190,18 +190,18 @@ public function run()
190190
}
191191

192192
// Call the callback with the matched positions as params
193-
call_user_func_array($command->getAction(), $params);
193+
\call_user_func_array($command->getAction(), $params);
194194

195195
foreach($this->shutdown as $shutdown) {
196-
call_user_func_array($shutdown, array());
196+
\call_user_func_array($shutdown, array());
197197
}
198198
}
199199
else {
200200
throw new Exception('No command found');
201201
}
202202
}
203203
catch (Exception $e) {
204-
call_user_func_array($this->error, array($e));
204+
\call_user_func_array($this->error, array($e));
205205
}
206206

207207
return $this;
@@ -223,7 +223,7 @@ protected function validate($key, $param, $value)
223223
// checking whether the class exists
224224
$validator = $param['validator'];
225225

226-
if(is_callable($validator)) {
226+
if(\is_callable($validator)) {
227227
$validator = $validator();
228228
}
229229

src/CLI/Console.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Console
1414
*/
1515
static public function log(string $message)
1616
{
17-
return fwrite(STDOUT, $message . "\n");
17+
return \fwrite(STDOUT, $message . "\n");
1818
}
1919

2020
/**
@@ -27,7 +27,7 @@ static public function log(string $message)
2727
*/
2828
static public function success(string $message)
2929
{
30-
return fwrite(STDOUT, "\033[32m" . $message . "\033[0m\n");
30+
return \fwrite(STDOUT, "\033[32m" . $message . "\033[0m\n");
3131
}
3232

3333
/**
@@ -40,7 +40,7 @@ static public function success(string $message)
4040
*/
4141
static public function error(string $message)
4242
{
43-
return fwrite(STDERR, "\033[31m" . $message . "\033[0m\n");
43+
return \fwrite(STDERR, "\033[31m" . $message . "\033[0m\n");
4444
}
4545

4646
/**
@@ -52,7 +52,7 @@ static public function error(string $message)
5252
* @return bool|int
5353
*/
5454
static public function info(string $message) {
55-
return fwrite(STDOUT, "\033[34m" . $message . "\033[0m\n");
55+
return \fwrite(STDOUT, "\033[34m" . $message . "\033[0m\n");
5656
}
5757

5858
/**
@@ -64,7 +64,7 @@ static public function info(string $message) {
6464
* @return bool|int
6565
*/
6666
static public function warning(string $message) {
67-
return fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n");
67+
return \fwrite(STDERR, "\033[1;33m" . $message . "\033[0m\n");
6868
}
6969

7070
/**
@@ -78,10 +78,10 @@ static public function warning(string $message) {
7878
static public function confirm(string $question) {
7979
self::log($question);
8080

81-
$handle = fopen('php://stdin', 'r');
82-
$line = trim(fgets($handle));
81+
$handle = \fopen('php://stdin', 'r');
82+
$line = \trim(\fgets($handle));
8383

84-
fclose($handle);
84+
\fclose($handle);
8585

8686
return $line;
8787
}
@@ -94,46 +94,46 @@ static public function confirm(string $question) {
9494
static public function execute(string $cmd, string $stdin = null, string &$stdout, string &$stderr, int $timeout = -1):int
9595
{
9696
$pipes = [];
97-
$process = proc_open(
97+
$process = \proc_open(
9898
$cmd,
9999
[['pipe','r'],['pipe','w'],['pipe','w']],
100100
$pipes
101101
);
102-
$start = time();
102+
$start = \time();
103103
$stdout = '';
104104
$stderr = '';
105105

106-
if (is_resource($process)) {
107-
stream_set_blocking($pipes[0], 0);
108-
stream_set_blocking($pipes[1], 0);
109-
stream_set_blocking($pipes[2], 0);
106+
if (\is_resource($process)) {
107+
\stream_set_blocking($pipes[0], 0);
108+
\stream_set_blocking($pipes[1], 0);
109+
\stream_set_blocking($pipes[2], 0);
110110

111-
fwrite($pipes[0], $stdin);
112-
fclose($pipes[0]);
111+
\fwrite($pipes[0], $stdin);
112+
\fclose($pipes[0]);
113113
}
114114

115-
while(is_resource($process))
115+
while(\is_resource($process))
116116
{
117-
$stdout .= stream_get_contents($pipes[1]);
118-
$stderr .= stream_get_contents($pipes[2]);
117+
$stdout .= \stream_get_contents($pipes[1]);
118+
$stderr .= \stream_get_contents($pipes[2]);
119119

120-
if($timeout > 0 && time() - $start > $timeout)
120+
if($timeout > 0 && \time() - $start > $timeout)
121121
{
122-
proc_terminate($process, 9);
122+
\proc_terminate($process, 9);
123123
return 1;
124124
}
125125

126-
$status = proc_get_status($process);
126+
$status = \proc_get_status($process);
127127

128128
if(!$status['running']) {
129-
fclose($pipes[1]);
130-
fclose($pipes[2]);
131-
proc_close($process);
129+
\fclose($pipes[1]);
130+
\fclose($pipes[2]);
131+
\proc_close($process);
132132

133133
return $status['exitcode'];
134134
}
135135

136-
usleep(100000);
136+
\usleep(100000);
137137
}
138138

139139
return 1;

0 commit comments

Comments
 (0)