Skip to content

Commit 40bd78c

Browse files
committed
Clean up some files and add some type hints
1 parent 0cd8856 commit 40bd78c

10 files changed

+42
-32
lines changed

.gitattributes

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
* text=auto
22
/.github export-ignore
3-
.styleci.yml export-ignore
4-
.scrutinizer.yml export-ignore
5-
BACKERS.md export-ignore
6-
CONTRIBUTING.md export-ignore
7-
CHANGELOG.md export-ignore
3+
.editorconfig export-ignore
4+
takeout-banner.png export-ignore

app/InitializesCommands.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
trait InitializesCommands
99
{
10-
public function initializeCommand()
10+
public function initializeCommand(): void
1111
{
1212
app()->bind('console', function () {
1313
return $this;

app/Services/BaseService.php

+13-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ abstract class BaseService
3535
protected $shell;
3636
protected $environment;
3737
protected $docker;
38+
protected $dockerTags;
3839

3940
public function __construct(Shell $shell, Environment $environment, Docker $docker, DockerTags $dockerTags)
4041
{
@@ -56,7 +57,7 @@ public function __construct(Shell $shell, Environment $environment, Docker $dock
5657
];
5758
}
5859

59-
public function install()
60+
public function install(): void
6061
{
6162
$this->prompts();
6263
$this->ensureImageIsDownloaded();
@@ -71,7 +72,7 @@ public function install()
7172

7273
$this->info("\nInstallation complete!");
7374
} catch (Throwable $e) {
74-
return $this->error("\nInstallation failed!");
75+
$this->error("\nInstallation failed!");
7576
}
7677
}
7778

@@ -90,7 +91,12 @@ public function shortName(): string
9091
return strtolower(class_basename(static::class));
9192
}
9293

93-
protected function ensureImageIsDownloaded()
94+
public function defaultPort(): int
95+
{
96+
return $this->defaultPort;
97+
}
98+
99+
protected function ensureImageIsDownloaded(): void
94100
{
95101
if ($this->docker->imageIsDownloaded($this->organization, $this->imageName, $this->tag)) {
96102
return;
@@ -100,7 +106,7 @@ protected function ensureImageIsDownloaded()
100106
$this->docker->downloadImage($this->organization, $this->imageName, $this->tag);
101107
}
102108

103-
protected function prompts()
109+
protected function prompts(): void
104110
{
105111
foreach ($this->defaultPrompts as $prompt) {
106112
$this->askQuestion($prompt);
@@ -118,7 +124,7 @@ protected function prompts()
118124
$this->tag = $this->resolveTag($this->promptResponses['tag']);
119125
}
120126

121-
protected function askQuestion($prompt): void
127+
protected function askQuestion(array $prompt): void
122128
{
123129
$this->promptResponses[$prompt['shortname']] = app('console')->ask($prompt['prompt'], $prompt['default'] ?? null);
124130
}
@@ -132,11 +138,12 @@ protected function resolveTag($responseTag)
132138
return $responseTag;
133139
}
134140

135-
protected function buildParameters()
141+
protected function buildParameters(): array
136142
{
137143
$parameters = $this->promptResponses;
138144
$parameters['container_name'] = $this->containerName();
139145
$parameters['tag'] = $this->tag; // Overwrite "latest" with actual latest tag
146+
140147
return $parameters;
141148
}
142149

app/Shell/Docker.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function stopContainer(string $containerId)
3434
}
3535
}
3636

37-
public function isInstalled(): Bool
37+
public function isInstalled(): bool
3838
{
3939
$process = $this->shell->execQuietly('docker --version 2>&1');
4040

@@ -54,7 +54,7 @@ public function containers(): array
5454
}, explode("\n", $output)));
5555
}
5656

57-
public function imageIsDownloaded($organization, $imageName, $tag): Bool
57+
public function imageIsDownloaded(string $organization, string $imageName, ?string $tag): bool
5858
{
5959
$process = $this->shell->execQuietly(sprintf(
6060
'docker image inspect %s/%s:%s',
@@ -66,7 +66,7 @@ public function imageIsDownloaded($organization, $imageName, $tag): Bool
6666
return $process->isSuccessful();
6767
}
6868

69-
public function downloadImage($organization, $imageName, $tag)
69+
public function downloadImage(string $organization, string $imageName, ?string $tag): void
7070
{
7171
$this->shell->exec(sprintf(
7272
'docker pull %s/%s:%s',

app/Shell/DockerTags.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client;
66
use GuzzleHttp\Psr7\Stream;
7+
use Psr\Http\Message\StreamInterface;
78

89
class DockerTags
910
{
@@ -14,14 +15,14 @@ public function __construct(Client $guzzle)
1415
$this->guzzle = $guzzle;
1516
}
1617

17-
public function getLatestTag($organization, $imageName): string
18+
public function getLatestTag(string $organization, string $imageName): string
1819
{
1920
return collect($this->getTags($organization, $imageName))->first(function ($tag) {
2021
return $tag !== 'latest';
2122
});
2223
}
2324

24-
public function getTags($organization, $imageName): array
25+
public function getTags(string $organization, string $imageName): array
2526
{
2627
return $this->filterResponseForTags(
2728
$this->getTagsResponse($organization, $imageName)
@@ -37,7 +38,7 @@ protected function filterResponseForTags(Stream $stream): array
3738
->toArray();
3839
}
3940

40-
protected function getTagsResponse($organization, $imageName): Stream
41+
protected function getTagsResponse($organization, $imageName): StreamInterface
4142
{
4243
return $this->guzzle
4344
->get($this->buildTagsUrl($organization, $imageName))

app/Shell/Environment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function __construct(Shell $shell)
1111
$this->shell = $shell;
1212
}
1313

14-
public function portIsAvailable($port)
14+
public function portIsAvailable($port): bool
1515
{
1616
// Check to see if the system is running a service with the desired port
1717
$process = $this->shell->execQuietly("netstat -vanp tcp | grep {$port}");

app/Shell/Shell.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(ConsoleOutput $output)
1414
$this->output = $output;
1515
}
1616

17-
public function exec($command, $parameters = [], $quiet = false): Process
17+
public function exec(string $command, array $parameters = [], bool $quiet = false): Process
1818
{
1919
$process = $this->buildProcess($command);
2020
$process->run(function ($type, $buffer) use ($quiet) {
@@ -32,27 +32,27 @@ public function exec($command, $parameters = [], $quiet = false): Process
3232
return $process;
3333
}
3434

35-
public function execQuietly($command, $parameters = []): Process
35+
public function execQuietly(string $command, array $parameters = []): Process
3636
{
3737
return $this->exec($command, $parameters, $quiet = true);
3838
}
3939

40-
public function formatStartMessage(string $buffer)
40+
public function formatStartMessage(string $buffer): string
4141
{
4242
return rtrim(sprintf('<bg=blue;fg=white> RUN </> <fg=blue>%s</>', $buffer));
4343
}
4444

45-
public function formatErrorMessage(string $buffer)
45+
public function formatErrorMessage(string $buffer): string
4646
{
4747
return rtrim(sprintf('<bg=red;fg=white> ERR </> %s', $buffer));
4848
}
4949

50-
public function formatMessage(string $buffer)
50+
public function formatMessage(string $buffer): string
5151
{
5252
return rtrim(sprintf('<bg=green;fg=white> OUT </> %s', $buffer));
5353
}
5454

55-
public function buildProcess($command): Process
55+
public function buildProcess(string $command): Process
5656
{
5757
$process = Process::fromShellCommandline($command);
5858
$process->setTimeout(null);

app/WritesToConsole.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55
trait WritesToConsole
66
{
7-
public function alert(string $message)
7+
public function alert(string $message): void
88
{
99
app('console')->alert($message);
1010
}
1111

12-
public function warn(string $message)
12+
public function warn(string $message): void
1313
{
1414
app('console')->warn($message);
1515
}
1616

17-
public function error(string $message)
17+
public function error(string $message): void
1818
{
1919
app('console')->error($message);
2020
}
2121

22-
public function line(string $message)
22+
public function line(string $message): void
2323
{
2424
app('console')->line($message);
2525
}
2626

27-
public function info(string $message)
27+
public function info(string $message): void
2828
{
2929
app('console')->info($message);
3030
}

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tightenco/takeout",
3-
"description": "Takeout, by Tighten -- Simple one-off Docker containers.",
3+
"description": "Manage your dev dependencies with simple one-off Docker containers.",
44
"keywords": ["cli", "console", "docker", "containers", "dependency management"],
55
"homepage": "https://takeout.tighten.co/",
66
"type": "project",
@@ -19,7 +19,8 @@
1919
"php": "^7.2.5",
2020
"guzzlehttp/guzzle": "^6.5",
2121
"laravel-zero/framework": "^7.0",
22-
"nunomaduro/laravel-console-menu": "^3.0"
22+
"nunomaduro/laravel-console-menu": "^3.0",
23+
"ext-json": "*"
2324
},
2425
"require-dev": {
2526
"mockery/mockery": "^1.3.1",

tests/Feature/BaseServiceTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Mockery as M;
1010
use Symfony\Component\Process\Process;
1111
use Tests\TestCase;
12+
use function app;
1213

1314
class BaseServiceTest extends TestCase
1415
{
@@ -23,6 +24,9 @@ function it_generates_shortname()
2324
function it_installs_services()
2425
{
2526
app()->instance('console', M::mock(Command::class, function ($mock) {
27+
// $service = new MeiliSearch;
28+
// $mock->shouldReceive('ask')->with('Which host port would you like this service to use?', $service->defaultPort())->andReturn(7700);
29+
// $mock->shouldReceive('ask')->with('Which tag (version) of this service would you like to use?', 'latest')->andReturn('v1.1.1');
2630
$mock->shouldIgnoreMissing();
2731
}));
2832

0 commit comments

Comments
 (0)