Skip to content

Commit ea46cc7

Browse files
committed
Static types in code due to php 7.1
1 parent 2b47470 commit ea46cc7

File tree

3 files changed

+124
-129
lines changed

3 files changed

+124
-129
lines changed

src/Dplr/Dplr.php

Lines changed: 73 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Dplr;
46

57
/**
@@ -9,27 +11,60 @@
911
*/
1012
class Dplr
1113
{
12-
const DEFAULT_TIMEOUT = 3600;
14+
private const DEFAULT_TIMEOUT = 3600;
1315

14-
const STATE_INIT = 'init';
15-
const STATE_RUNNING = 'running';
16+
private const STATE_INIT = 'init';
17+
private const STATE_RUNNING = 'running';
1618

19+
/**
20+
* @var array
21+
*/
1722
protected $servers = [];
23+
24+
/**
25+
* @var array
26+
*/
1827
protected $tasks = [];
28+
29+
/**
30+
* @var int
31+
*/
1932
protected $tasksThread = 0;
33+
34+
/**
35+
* @var array
36+
*/
2037
protected $reports = [];
38+
39+
/**
40+
* @var array
41+
*/
2142
protected $timers = [];
2243

44+
/**
45+
* @var string
46+
*/
2347
protected $user;
48+
49+
/**
50+
* @var string|null
51+
*/
2452
protected $publicKey;
53+
54+
/**
55+
* @var string
56+
*/
2557
protected $gosshaPath;
2658

59+
/**
60+
* @var int
61+
*/
2762
protected $defaultTimeout;
2863

2964
// dplr state
3065
protected $state;
3166

32-
public function __construct($user, $gosshaPath, $publicKey = null)
67+
public function __construct(string $user, string $gosshaPath, string $publicKey = null)
3368
{
3469
$this->user = $user;
3570
$this->publicKey = $publicKey;
@@ -41,52 +76,44 @@ public function __construct($user, $gosshaPath, $publicKey = null)
4176
$this->defaultTimeout = self::DEFAULT_TIMEOUT;
4277
}
4378

44-
protected function resetTasks()
79+
protected function resetTasks(): void
4580
{
4681
$this->tasks = [[]];
4782
$this->tasksThread = 0;
4883
}
4984

50-
protected function checkState()
85+
protected function checkState(): void
5186
{
52-
if (self::STATE_RUNNING == $this->state) {
87+
if (self::STATE_RUNNING === $this->state) {
5388
throw new \RuntimeException('Dplr is already running.');
5489
}
5590
}
5691

5792
/**
5893
* Returns default timeout for tasks.
59-
*
60-
* @return int
6194
*/
62-
public function getDefaultTimeout()
95+
public function getDefaultTimeout(): int
6396
{
6497
return $this->defaultTimeout;
6598
}
6699

67100
/**
68101
* Set default timeout for tasks.
69-
*
70-
* @param int $timeout
71-
*
72-
* @return Dplr
73102
*/
74-
public function setDefaultTimeout($timeout)
103+
public function setDefaultTimeout(int $timeout): Dplr
75104
{
76-
$this->defaultTimeout = (int) $timeout;
105+
$this->defaultTimeout = $timeout;
77106

78107
return $this;
79108
}
80109

81110
/**
82111
* Add server for deploying.
83112
*
84-
* @param mixed $serverName
85-
* @param mixed $groups (default: null)
86-
*
87-
* @return Dplr
113+
* @param string $serverName
114+
* @param string|array|null $groups (default: null)
88115
*/
89-
public function addServer($serverName, $groups = null)
116+
public function addServer(string $serverName, $groups = null): Dplr
90117
{
91118
$this->checkState();
92119

@@ -101,10 +128,8 @@ public function addServer($serverName, $groups = null)
101128

102129
/**
103130
* Return servers list.
104-
*
105-
* @return array
106131
*/
107-
public function getServers()
132+
public function getServers(): array
108133
{
109134
return array_keys($this->servers);
110135
}
@@ -116,11 +141,11 @@ public function getServers()
116141
*
117142
* @return array
118143
*/
119-
public function getServersByGroup($group)
144+
public function getServersByGroup(string $group): array
120145
{
121146
$servers = [];
122147
foreach ($this->servers as $serverName => $groups) {
123-
if (in_array($group, $groups)) {
148+
if (in_array($group, $groups, true)) {
124149
$servers[] = $serverName;
125150
}
126151
}
@@ -130,15 +155,11 @@ public function getServersByGroup($group)
130155

131156
/**
132157
* Check server group existing.
133-
*
134-
* @param string $group
135-
*
136-
* @return bool
137158
*/
138-
public function hasGroup($group)
159+
public function hasGroup(string $group): bool
139160
{
140161
foreach ($this->servers as $serverName => $groups) {
141-
if (in_array($group, $groups)) {
162+
if (in_array($group, $groups, true)) {
142163
return true;
143164
}
144165
}
@@ -148,10 +169,8 @@ public function hasGroup($group)
148169

149170
/**
150171
* Creating new thread.
151-
*
152-
* @return Dplr
153172
*/
154-
public function newThread()
173+
public function newThread(): Dplr
155174
{
156175
// if current thread is empty, use it
157176
if (!count($this->tasks[$this->tasksThread])) {
@@ -166,25 +185,11 @@ public function newThread()
166185

167186
/**
168187
* Adding command task.
169-
*
170-
* @param string $command
171-
* @param string $serverGroup (default: null)
172-
* @param int $timeout (default: null)
173-
*
174-
* @return Dplr
175188
*/
176-
public function command($command, $serverGroup = null, $timeout = null)
189+
public function command(string $command, string $serverGroup = null, int $timeout = null): Dplr
177190
{
178-
if (!is_string($command)) {
179-
throw new \InvalidArgumentException('Command must be string');
180-
}
181-
182-
if ($serverGroup && !is_string($serverGroup)) {
183-
throw new \InvalidArgumentException('Server group must be string');
184-
}
185-
186191
$servers = null;
187-
if ($serverGroup) {
192+
if (null !== $serverGroup) {
188193
$servers = $this->getServersByGroup($serverGroup);
189194
if (!count($servers)) {
190195
throw new \InvalidArgumentException(sprintf('Not found servers for group "%s"', $serverGroup));
@@ -194,10 +199,10 @@ public function command($command, $serverGroup = null, $timeout = null)
194199
$this->checkState();
195200

196201
$data = [
197-
'Action' => 'ssh',
202+
'Action' => Task::ACTION_SSH,
198203
'Cmd' => $command,
199204
'Hosts' => $serverGroup ? $servers : $this->getServers(),
200-
'Timeout' => ((int) $timeout > 0 ? (int) $timeout : $this->defaultTimeout) * 1000,
205+
'Timeout' => ($timeout > 0 ? $timeout : $this->defaultTimeout) * 1000,
201206
];
202207

203208
$this->tasks[$this->tasksThread][] = new Task($data);
@@ -207,30 +212,11 @@ public function command($command, $serverGroup = null, $timeout = null)
207212

208213
/**
209214
* Adding uploading task.
210-
*
211-
* @param string $localFile
212-
* @param string $remoteFile
213-
* @param string $serverGroup (default: null)
214-
* @param int $timeout (default: null)
215-
*
216-
* @return Dplr
217215
*/
218-
public function upload($localFile, $remoteFile, $serverGroup = null, $timeout = null)
216+
public function upload(string $localFile, string $remoteFile, string $serverGroup = null, int $timeout = null): Dplr
219217
{
220-
if (!is_string($localFile)) {
221-
throw new \InvalidArgumentException('Local file must be string');
222-
}
223-
224-
if (!is_string($remoteFile)) {
225-
throw new \InvalidArgumentException('Remote file must be string');
226-
}
227-
228-
if ($serverGroup && !is_string($serverGroup)) {
229-
throw new \InvalidArgumentException('Server group must be string');
230-
}
231-
232218
$servers = null;
233-
if ($serverGroup) {
219+
if (null !== $serverGroup) {
234220
$servers = $this->getServersByGroup($serverGroup);
235221
if (!count($servers)) {
236222
throw new \InvalidArgumentException(sprintf('Not found servers for group "%s"', $serverGroup));
@@ -240,11 +226,11 @@ public function upload($localFile, $remoteFile, $serverGroup = null, $timeout =
240226
$this->checkState();
241227

242228
$data = [
243-
'Action' => 'scp',
229+
'Action' => Task::ACTION_SCP,
244230
'Source' => $localFile,
245231
'Target' => $remoteFile,
246232
'Hosts' => $serverGroup ? $servers : $this->getServers(),
247-
'Timeout' => ((int) $timeout > 0 ? (int) $timeout : $this->defaultTimeout) * 1000,
233+
'Timeout' => ($timeout > 0 ? $timeout : $this->defaultTimeout) * 1000,
248234
];
249235

250236
$this->tasks[$this->tasksThread][] = new Task($data);
@@ -254,12 +240,8 @@ public function upload($localFile, $remoteFile, $serverGroup = null, $timeout =
254240

255241
/**
256242
* Run tasks on servers.
257-
*
258-
* @param callable $callback (default: null)
259-
*
260-
* @return Dplr
261243
*/
262-
public function run(callable $callback = null)
244+
public function run(callable $callback = null): Dplr
263245
{
264246
$this->state = self::STATE_RUNNING;
265247

@@ -272,10 +254,8 @@ public function run(callable $callback = null)
272254

273255
/**
274256
* Check that all task executed successfully.
275-
*
276-
* @return bool
277257
*/
278-
public function isSuccessful()
258+
public function isSuccessful(): bool
279259
{
280260
foreach ($this->reports as $report) {
281261
if (!$report->isSuccessful()) {
@@ -288,13 +268,11 @@ public function isSuccessful()
288268

289269
/**
290270
* Return short report about task executing.
291-
*
292-
* @return array
293271
*/
294-
public function getReport()
272+
public function getReport(): array
295273
{
296274
$result = [
297-
'total' => sizeof($this->reports),
275+
'total' => count($this->reports),
298276
'successful' => 0,
299277
'failed' => 0,
300278
'timers' => [
@@ -316,26 +294,26 @@ public function getReport()
316294
/**
317295
* Return failed task reports.
318296
*
319-
* @return array
297+
* @return array<TaskReport>
320298
*/
321-
public function getFailed()
299+
public function getFailed(): array
322300
{
323-
return array_filter($this->reports, function ($item) {
301+
return array_filter($this->reports, function (TaskReport $item) {
324302
return !$item->isSuccessful();
325303
});
326304
}
327305

328306
/**
329307
* Return all reports.
330308
*
331-
* @return array
309+
* @return array<TaskReport>
332310
*/
333-
public function getReports()
311+
public function getReports(): array
334312
{
335313
return $this->reports;
336314
}
337315

338-
protected function runTasks($callback = null)
316+
protected function runTasks(callable $callback = null): void
339317
{
340318
$max = 0;
341319
// clear empty threads and search max thread
@@ -388,7 +366,7 @@ protected function runTasks($callback = null)
388366

389367
$task = $thread[$j];
390368
if ($callback) {
391-
call_user_func($callback, ($k > 0 ? "\n" : '') . (string) $task . ' ');
369+
call_user_func($callback, ($k > 0 ? "\n" : '') . $task . ' ');
392370
}
393371
fwrite($pipes[$i][0], $task->getJson() . "\n");
394372
++$k;

0 commit comments

Comments
 (0)