Skip to content

Commit 3bd29a0

Browse files
authored
Merge pull request #888 from hydephp/refactor-validating-command
Refactor the validating command class to reduce repeated code
2 parents 78a6e4b + 87e880e commit 3bd29a0

6 files changed

+11
-51
lines changed

packages/publications/src/Commands/MakePublicationCommand.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function safeHandle(): int
6262
}
6363
$creator->create();
6464

65-
$this->infoComment('All done! Created file', $creator->getOutputPath());
65+
$this->infoComment("All done! Created file [{$creator->getOutputPath()}]");
6666

6767
return Command::SUCCESS;
6868
}
@@ -128,21 +128,21 @@ protected function captureFieldInput(PublicationFieldDefinition $field): ?Public
128128

129129
protected function captureTextFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
130130
{
131-
$this->infoComment('Enter lines for field', $field->name, '</>(end with an empty line)');
131+
$this->infoComment("Enter lines for field [$field->name]");
132132

133133
return new PublicationFieldValue(PublicationFieldTypes::Text, implode("\n", InputStreamHandler::call()));
134134
}
135135

136136
protected function captureArrayFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
137137
{
138-
$this->infoComment('Enter values for field', $field->name, '</>(end with an empty line)');
138+
$this->infoComment("Enter values for field [$field->name]");
139139

140140
return new PublicationFieldValue(PublicationFieldTypes::Array, InputStreamHandler::call());
141141
}
142142

143143
protected function captureMediaFieldInput(PublicationFieldDefinition $field): ?PublicationFieldValue
144144
{
145-
$this->infoComment('Select file for image field', $field->name);
145+
$this->infoComment("Select file for image field [$field->name]");
146146

147147
$mediaFiles = PublicationService::getMediaForPubType($this->publicationType);
148148
if ($mediaFiles->isEmpty()) {
@@ -155,7 +155,7 @@ protected function captureMediaFieldInput(PublicationFieldDefinition $field): ?P
155155
protected function captureTagFieldInput(PublicationFieldDefinition $field): ?PublicationFieldValue
156156
{
157157
$tagGroup = $field->tagGroup ?? throw new InvalidArgumentException("Tag field '$field->name' is missing the 'tagGroup' property");
158-
$this->infoComment('Select a tag for field', $field->name, "from the $tagGroup group");
158+
$this->infoComment(/** @lang Text */ "Select a tag for field [$field->name] from the $tagGroup group");
159159

160160
$options = PublicationService::getValuesForTagName($tagGroup);
161161
if ($options->isEmpty()) {

packages/publications/src/Commands/MakePublicationTagCommand.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function getTagName(): void
5454
protected function getTagNameFromArgument(?string $value): ?string
5555
{
5656
if ($value) {
57-
$this->infoComment('Using tag name', $value, 'from command line argument');
57+
$this->infoComment("Using tag name [$value] from command line argument");
5858
$this->newLine();
5959

6060
return $value;
@@ -87,9 +87,9 @@ protected function printSelectionInformation(): void
8787

8888
protected function saveTagsToDisk(): void
8989
{
90-
$this->infoComment('Saving tag data to',
90+
$this->infoComment(sprintf('Saving tag data to [%s]',
9191
\Hyde\Console\Concerns\Command::createClickableFilepath('tags.yml')
92-
);
92+
));
9393

9494
(new PublicationTags)->addTagGroups($this->tags)->save();
9595
}

packages/publications/src/Commands/ValidatePublicationTypesCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function validateSchemaFiles(): void
8181
protected function displayResults(): void
8282
{
8383
foreach ($this->results as $name => $errors) {
84-
$this->infoComment('Validating schema file for', $name);
84+
$this->infoComment("Validating schema file for [$name]");
8585

8686
$schemaErrors = $errors['schema'];
8787
if (empty($schemaErrors)) {

packages/publications/src/Commands/ValidatePublicationsCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function validatePublicationType(PublicationType $publicationType): vo
122122
protected function displayResults(): void
123123
{
124124
foreach ($this->results as $publicationTypeName => $publications) {
125-
$this->infoComment('Validating publication type', $publicationTypeName);
125+
$this->infoComment("Validating publication type [$publicationTypeName]");
126126
foreach ($publications ?? [] as $publicationName => $errors) {
127127
$this->displayPublicationResults($publicationName, $errors);
128128
}

packages/publications/src/Commands/ValidatingCommand.php

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
use function __;
88
use function array_merge;
99
use Exception;
10+
use Hyde\Console\Concerns\Command;
1011
use Illuminate\Support\Facades\Validator;
1112
use function in_array;
12-
use LaravelZero\Framework\Commands\Command;
1313
use RuntimeException;
1414
use function str_ends_with;
1515
use function ucfirst;
@@ -119,14 +119,6 @@ public function handleException(Exception $exception, ?string $file = null, ?int
119119
return Command::FAILURE;
120120
}
121121

122-
/**
123-
* Write a nicely formatted and consistent message to the console. Using InfoComment for a lack of a better term.
124-
*/
125-
public function infoComment(string $info, string $comment, ?string $moreInfo = null): void
126-
{
127-
$this->line("<info>$info</info> [<comment>$comment</comment>]".($moreInfo ? " <info>$moreInfo</info>" : ''));
128-
}
129-
130122
protected function translate(string $name, string $error): string
131123
{
132124
return __($error, [

packages/publications/tests/Feature/ValidatingCommandTest.php

-32
Original file line numberDiff line numberDiff line change
@@ -243,38 +243,6 @@ public function testCanEnableThrowOnException()
243243
$this->assertSame(1, $code);
244244
}
245245

246-
public function testInfoComment()
247-
{
248-
$command = new DynamicValidatingTestCommand();
249-
$command->closure = function (ValidatingCommand $command) {
250-
$command->infoComment('foo', 'bar');
251-
};
252-
$output = Mockery::mock(OutputStyle::class);
253-
254-
$output->shouldReceive('writeln')->once()->withArgs(function (string $message) {
255-
return $message === '<info>foo</info> [<comment>bar</comment>]';
256-
});
257-
258-
$command->setOutput($output);
259-
$command->handle();
260-
}
261-
262-
public function testInfoCommentWithExtraInfo()
263-
{
264-
$command = new DynamicValidatingTestCommand();
265-
$command->closure = function (ValidatingCommand $command) {
266-
$command->infoComment('foo', 'bar', 'baz');
267-
};
268-
$output = Mockery::mock(OutputStyle::class);
269-
270-
$output->shouldReceive('writeln')->once()->withArgs(function (string $message) {
271-
return $message === '<info>foo</info> [<comment>bar</comment>] <info>baz</info>';
272-
});
273-
274-
$command->setOutput($output);
275-
$command->handle();
276-
}
277-
278246
protected function assertEqualsAsBoolean($expected, $question): bool
279247
{
280248
try {

0 commit comments

Comments
 (0)