From 5edc84b0c429b35a57df3f481a06c7f9189a636d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:19:31 +0100 Subject: [PATCH 01/28] Move constant to base command --- packages/framework/src/Console/Concerns/Command.php | 2 ++ packages/publications/src/Commands/ValidatingCommand.php | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 9751a965bac..2cc758876ac 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -12,6 +12,8 @@ */ abstract class Command extends BaseCommand { + public const USER_EXIT = 130; + /** * Create a filepath that can be opened in the browser from a terminal. */ diff --git a/packages/publications/src/Commands/ValidatingCommand.php b/packages/publications/src/Commands/ValidatingCommand.php index 2a39363e0f8..88be23a14da 100644 --- a/packages/publications/src/Commands/ValidatingCommand.php +++ b/packages/publications/src/Commands/ValidatingCommand.php @@ -21,8 +21,6 @@ */ class ValidatingCommand extends Command { - public const USER_EXIT = 130; - /** @var int How many times can the validation loop run? It is high enough to not affect normal usage. */ protected final const MAX_RETRIES = 30; From 037b997648f271e3529def43ef7d9126d320c1e3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:20:24 +0100 Subject: [PATCH 02/28] Move handle method to base command --- packages/framework/src/Console/Concerns/Command.php | 13 +++++++++++++ .../publications/src/Commands/ValidatingCommand.php | 11 ----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 2cc758876ac..1a5a26e836a 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -4,6 +4,7 @@ namespace Hyde\Console\Concerns; +use Exception; use Hyde\Hyde; use LaravelZero\Framework\Commands\Command as BaseCommand; @@ -14,6 +15,18 @@ abstract class Command extends BaseCommand { public const USER_EXIT = 130; + /** + * @return int The exit code. + */ + public function handle(): int + { + try { + return $this->safeHandle(); + } catch (Exception $exception) { + return $this->handleException($exception); + } + } + /** * Create a filepath that can be opened in the browser from a terminal. */ diff --git a/packages/publications/src/Commands/ValidatingCommand.php b/packages/publications/src/Commands/ValidatingCommand.php index 88be23a14da..7ef40105532 100644 --- a/packages/publications/src/Commands/ValidatingCommand.php +++ b/packages/publications/src/Commands/ValidatingCommand.php @@ -24,17 +24,6 @@ class ValidatingCommand extends Command /** @var int How many times can the validation loop run? It is high enough to not affect normal usage. */ protected final const MAX_RETRIES = 30; - /** - * @return int The exit code. - */ - public function handle(): int - { - try { - return $this->safeHandle(); - } catch (Exception $exception) { - return $this->handleException($exception); - } - } /** * This method can be overridden by child classes to provide automatic exception handling. From 38012f6edf8431f19139ad85cd763e9ff0b7a986 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:20:42 +0100 Subject: [PATCH 03/28] Move safeHandle method to base command --- packages/framework/src/Console/Concerns/Command.php | 11 +++++++++++ .../publications/src/Commands/ValidatingCommand.php | 12 ------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 1a5a26e836a..36db12ee05d 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -27,6 +27,17 @@ public function handle(): int } } + /** + * This method can be overridden by child classes to provide automatic exception handling. + * Existing code can be converted simply by renaming the handle() method to safeHandle(). + * + * @return int The exit code. + */ + protected function safeHandle(): int + { + return Command::SUCCESS; + } + /** * Create a filepath that can be opened in the browser from a terminal. */ diff --git a/packages/publications/src/Commands/ValidatingCommand.php b/packages/publications/src/Commands/ValidatingCommand.php index 7ef40105532..2dc0e40eb43 100644 --- a/packages/publications/src/Commands/ValidatingCommand.php +++ b/packages/publications/src/Commands/ValidatingCommand.php @@ -24,18 +24,6 @@ class ValidatingCommand extends Command /** @var int How many times can the validation loop run? It is high enough to not affect normal usage. */ protected final const MAX_RETRIES = 30; - - /** - * This method can be overridden by child classes to provide automatic exception handling. - * Existing code can be converted simply by renaming the handle() method to safeHandle(). - * - * @return int The exit code. - */ - protected function safeHandle(): int - { - return Command::SUCCESS; - } - /** * Ask for a CLI input value until we pass validation rules. * From b561422ff682ff3c6b79f26865af1e482789bbad Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:21:09 +0100 Subject: [PATCH 04/28] Move handleException method to base command --- .../src/Console/Concerns/Command.php | 26 +++++++++++++++++++ .../src/Commands/ValidatingCommand.php | 23 ---------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 36db12ee05d..bbd5c242ecf 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -8,6 +8,9 @@ use Hyde\Hyde; use LaravelZero\Framework\Commands\Command as BaseCommand; +use function config; +use function sprintf; + /** * @see \Hyde\Framework\Testing\Feature\CommandTest */ @@ -38,6 +41,29 @@ protected function safeHandle(): int return Command::SUCCESS; } + /** + * Handle an exception that occurred during command execution. + * + * @param string|null $file The file where the exception occurred. Leave null to auto-detect. + * @return int The exit code + */ + public function handleException(Exception $exception, ?string $file = null, ?int $line = null): int + { + // When testing it might be more useful to see the full stack trace, so we have an option to actually throw the exception. + if (config('app.throw_on_console_exception', false)) { + throw $exception; + } + + // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. + if (str_ends_with($file ?? $exception->getFile(), 'Command.php')) { + $this->error("Error: {$exception->getMessage()}"); + } else { + $this->error(sprintf('Error: %s at ', $exception->getMessage()).sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine())); + } + + return Command::FAILURE; + } + /** * Create a filepath that can be opened in the browser from a terminal. */ diff --git a/packages/publications/src/Commands/ValidatingCommand.php b/packages/publications/src/Commands/ValidatingCommand.php index 2dc0e40eb43..bea94bba043 100644 --- a/packages/publications/src/Commands/ValidatingCommand.php +++ b/packages/publications/src/Commands/ValidatingCommand.php @@ -69,29 +69,6 @@ public function reloadableChoice(callable $options, string $question, string $re return $selection; } - /** - * Handle an exception that occurred during command execution. - * - * @param string|null $file The file where the exception occurred. Leave null to auto-detect. - * @return int The exit code - */ - public function handleException(Exception $exception, ?string $file = null, ?int $line = null): int - { - // When testing it might be more useful to see the full stack trace, so we have an option to actually throw the exception. - if (config('app.throw_on_console_exception', false)) { - throw $exception; - } - - // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. - if (str_ends_with($file ?? $exception->getFile(), 'Command.php')) { - $this->error("Error: {$exception->getMessage()}"); - } else { - $this->error(sprintf('Error: %s at ', $exception->getMessage()).sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine())); - } - - return Command::FAILURE; - } - protected function translate(string $name, string $error): string { return __($error, [ From 5806fc55b768ca072ad0db5c57c53e707310effa Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:24:11 +0100 Subject: [PATCH 05/28] Bypass inheritance conflict --- .../src/Console/Commands/RebuildStaticSiteCommand.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php b/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php index 1854f39a63f..8a4c35cdd91 100644 --- a/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php +++ b/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php @@ -47,7 +47,7 @@ public function handle(): int try { $this->validate(); } catch (Exception $exception) { - return $this->handleException($exception); + return $this->customHandleException($exception); } (new RebuildService($this->path))->execute(); @@ -100,9 +100,11 @@ public function validate(): void /** * Output the contents of an exception. * + * @fixme Refactor to use new base command helpers + * * @return int Error code */ - public function handleException(Exception $exception): int + public function customHandleException(Exception $exception): int { $this->error('Something went wrong!'); $this->warn($exception->getMessage()); From bb99a62b61c5fc0c88c672c3093836c379f680c0 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:29:14 +0100 Subject: [PATCH 06/28] Move tests for moved methods --- .../framework/tests/Feature/CommandTest.php | 95 +++++++++++++++++++ .../tests/Feature/ValidatingCommandTest.php | 90 ------------------ 2 files changed, 95 insertions(+), 90 deletions(-) diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index 7e172a2a6b1..bf0fe753c08 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -8,8 +8,12 @@ use Hyde\Console\Concerns\Command; use Hyde\Hyde; use Hyde\Testing\TestCase; +use Mockery; +use RuntimeException; use Symfony\Component\Console\Style\OutputStyle; +use function config; + /** * @covers \Hyde\Console\Concerns\Command */ @@ -122,6 +126,72 @@ public function testIndentedLine() $command->setMockedOutput($output); $command->handle(); } + + public function testSafeHandle() + { + $this->assertSame(0, (new SafeTestCommand())->handle()); + } + + public function testSafeHandleException() + { + $command = new SafeThrowingTestCommand(); + $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); + $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { + return str_starts_with($message, 'Error: This is a test at '.__FILE__.':'); + }); + $command->setOutput($output); + + $code = $command->handle(); + + $this->assertSame(1, $code); + } + + public function testHandleException() + { + $command = new ThrowingTestCommand(); + $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); + + $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { + return $message === 'Error: This is a test'; + }); + + $command->setOutput($output); + $code = $command->handle(); + + $this->assertSame(1, $code); + } + + public function testHandleExceptionWithErrorLocation() + { + $command = new ThrowingTestCommand(); + $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); + + $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { + return $message === 'Error: This is a test at TestClass.php:10'; + }); + + $command->setOutput($output); + $code = $command->handle('TestClass.php', 10); + + $this->assertSame(1, $code); + } + + public function testCanEnableThrowOnException() + { + config(['app.throw_on_console_exception' => true]); + $command = new ThrowingTestCommand(); + + $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('This is a test'); + + $command->setOutput($output); + $code = $command->handle(); + + $this->assertSame(1, $code); + } + } class MockableTestCommand extends Command @@ -140,3 +210,28 @@ public function setMockedOutput($output) $this->output = $output; } } + +class SafeTestCommand extends Command +{ + // +} + +class SafeThrowingTestCommand extends Command +{ + public function safeHandle(): int + { + throw new RuntimeException('This is a test'); + } +} + +class ThrowingTestCommand extends Command +{ + public function handle(?string $file = 'TestCommand.php', ?int $line = null): int + { + try { + throw new RuntimeException('This is a test'); + } catch (RuntimeException $exception) { + return $this->handleException($exception, $file, $line); + } + } +} diff --git a/packages/publications/tests/Feature/ValidatingCommandTest.php b/packages/publications/tests/Feature/ValidatingCommandTest.php index ea320d4de45..4329dbd1599 100644 --- a/packages/publications/tests/Feature/ValidatingCommandTest.php +++ b/packages/publications/tests/Feature/ValidatingCommandTest.php @@ -20,25 +20,6 @@ */ class ValidatingCommandTest extends TestCase { - public function testSafeHandle() - { - $this->assertSame(0, (new SafeValidatingTestCommand())->handle()); - } - - public function testSafeHandleException() - { - $command = new SafeThrowingValidatingTestCommand(); - $output = Mockery::mock(OutputStyle::class); - $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return str_starts_with($message, 'Error: This is a test at '.__FILE__.':'); - }); - $command->setOutput($output); - - $code = $command->handle(); - - $this->assertSame(1, $code); - } - public function testAskWithValidationCapturesInput() { $command = new ValidationTestCommand(); @@ -197,52 +178,6 @@ public function testReloadableChoiceHelperSelectingReload() $command->handle(); } - public function testHandleException() - { - $command = new ThrowingValidatingTestCommand(); - $output = Mockery::mock(OutputStyle::class); - - $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'Error: This is a test'; - }); - - $command->setOutput($output); - $code = $command->handle(); - - $this->assertSame(1, $code); - } - - public function testHandleExceptionWithErrorLocation() - { - $command = new ThrowingValidatingTestCommand(); - $output = Mockery::mock(OutputStyle::class); - - $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'Error: This is a test at TestClass.php:10'; - }); - - $command->setOutput($output); - $code = $command->handle('TestClass.php', 10); - - $this->assertSame(1, $code); - } - - public function testCanEnableThrowOnException() - { - config(['app.throw_on_console_exception' => true]); - $command = new ThrowingValidatingTestCommand(); - - $output = Mockery::mock(OutputStyle::class); - - $this->expectException(RuntimeException::class); - $this->expectExceptionMessage('This is a test'); - - $command->setOutput($output); - $code = $command->handle(); - - $this->assertSame(1, $code); - } - protected function assertEqualsAsBoolean($expected, $question): bool { try { @@ -255,19 +190,6 @@ protected function assertEqualsAsBoolean($expected, $question): bool } } -class SafeValidatingTestCommand extends ValidatingCommand -{ - // -} - -class SafeThrowingValidatingTestCommand extends ValidatingCommand -{ - public function safeHandle(): int - { - throw new RuntimeException('This is a test'); - } -} - class ValidationTestCommand extends ValidatingCommand { public function handle(): int @@ -279,18 +201,6 @@ public function handle(): int } } -class ThrowingValidatingTestCommand extends ValidatingCommand -{ - public function handle(?string $file = 'TestCommand.php', ?int $line = null): int - { - try { - throw new RuntimeException('This is a test'); - } catch (RuntimeException $exception) { - return $this->handleException($exception, $file, $line); - } - } -} - class ReloadableChoiceTestCommand extends ValidatingCommand { protected bool $isFirstRun = true; From 833eda1eab3cce61a7568bc2e8f6a9b09a7add79 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:34:07 +0100 Subject: [PATCH 07/28] Split out test for increased clarity --- .../framework/tests/Feature/CommandTest.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index bf0fe753c08..b83d45d2334 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -127,9 +127,14 @@ public function testIndentedLine() $command->handle(); } - public function testSafeHandle() + public function testHandleCallsBaseSafeHandle() { - $this->assertSame(0, (new SafeTestCommand())->handle()); + $this->assertSame(0, (new TestCommand())->handle()); + } + + public function testHandleCallsChildSafeHandle() + { + $this->assertSame(1, (new SafeHandleTestCommand())->handle()); } public function testSafeHandleException() @@ -211,11 +216,19 @@ public function setMockedOutput($output) } } -class SafeTestCommand extends Command +class TestCommand extends Command { // } +class SafeHandleTestCommand extends Command +{ + public function safeHandle(): int + { + return 1; + } +} + class SafeThrowingTestCommand extends Command { public function safeHandle(): int From 22a7322643007698c285d40f6d27ce8858c1c30c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:35:39 +0100 Subject: [PATCH 08/28] Add PHPDoc method description --- packages/framework/src/Console/Concerns/Command.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index bbd5c242ecf..ef0d5a54867 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -19,6 +19,11 @@ abstract class Command extends BaseCommand public const USER_EXIT = 130; /** + * The base handle method that can be overridden by child classes. + * + * Alternatively, implement the safeHandle method in your child class + * to utilize the automatic exception handling provided by this method. + * * @return int The exit code. */ public function handle(): int From 23db27f8be193f4af50ef23573ff615bab85bc45 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:36:47 +0100 Subject: [PATCH 09/28] Add line break --- packages/framework/src/Console/Concerns/Command.php | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index ef0d5a54867..0ea2c21f3e7 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -37,6 +37,7 @@ public function handle(): int /** * This method can be overridden by child classes to provide automatic exception handling. + * * Existing code can be converted simply by renaming the handle() method to safeHandle(). * * @return int The exit code. From 048d9be694194001eaadd4ea88c444327438ec62 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:39:42 +0100 Subject: [PATCH 10/28] Introduce local variable to make unreadable code readable --- packages/framework/src/Console/Concerns/Command.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 0ea2c21f3e7..3f5eaaab77a 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -64,7 +64,8 @@ public function handleException(Exception $exception, ?string $file = null, ?int if (str_ends_with($file ?? $exception->getFile(), 'Command.php')) { $this->error("Error: {$exception->getMessage()}"); } else { - $this->error(sprintf('Error: %s at ', $exception->getMessage()).sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine())); + $location = sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine()); + $this->error(sprintf('Error: %s at ', $exception->getMessage()).$location); } return Command::FAILURE; From ddad74450ecd15ae2323c621c2fde71c4c733a2b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:40:11 +0100 Subject: [PATCH 11/28] Convert a 'sprintf()' call and concatenation to string interpolation --- packages/framework/src/Console/Concerns/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 3f5eaaab77a..66445452151 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -65,7 +65,7 @@ public function handleException(Exception $exception, ?string $file = null, ?int $this->error("Error: {$exception->getMessage()}"); } else { $location = sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine()); - $this->error(sprintf('Error: %s at ', $exception->getMessage()).$location); + $this->error("Error: {$exception->getMessage()} at $location"); } return Command::FAILURE; From cde73ae3b5896076d172c201539172dfad1e3e24 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:41:06 +0100 Subject: [PATCH 12/28] Inline local variable --- packages/framework/src/Console/Concerns/Command.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 66445452151..2c3b2ec3067 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -64,8 +64,7 @@ public function handleException(Exception $exception, ?string $file = null, ?int if (str_ends_with($file ?? $exception->getFile(), 'Command.php')) { $this->error("Error: {$exception->getMessage()}"); } else { - $location = sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine()); - $this->error("Error: {$exception->getMessage()} at $location"); + $this->error("Error: {$exception->getMessage()} at ".sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine())); } return Command::FAILURE; From 5e342ff8bb9fcc8c4234c2d7bb2b974c3c2661fd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:42:00 +0100 Subject: [PATCH 13/28] Format sprintf arguments --- packages/framework/src/Console/Concerns/Command.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 2c3b2ec3067..2409d3cc7a6 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -64,7 +64,9 @@ public function handleException(Exception $exception, ?string $file = null, ?int if (str_ends_with($file ?? $exception->getFile(), 'Command.php')) { $this->error("Error: {$exception->getMessage()}"); } else { - $this->error("Error: {$exception->getMessage()} at ".sprintf('%s:%s', $file ?? $exception->getFile(), $line ?? $exception->getLine())); + $this->error("Error: {$exception->getMessage()} at ".sprintf('%s:%s', + $file ?? $exception->getFile(), $line ?? $exception->getLine() + )); } return Command::FAILURE; From 0c1af73528186145ce5315894aeea26111be071c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:43:50 +0100 Subject: [PATCH 14/28] Deprecate arguments only used in tests adding unnecessary complexity --- packages/framework/src/Console/Concerns/Command.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 2409d3cc7a6..d9d99b94562 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -6,6 +6,7 @@ use Exception; use Hyde\Hyde; +use JetBrains\PhpStorm\Deprecated; use LaravelZero\Framework\Commands\Command as BaseCommand; use function config; @@ -53,7 +54,7 @@ protected function safeHandle(): int * @param string|null $file The file where the exception occurred. Leave null to auto-detect. * @return int The exit code */ - public function handleException(Exception $exception, ?string $file = null, ?int $line = null): int + public function handleException(Exception $exception, #[Deprecated(reason: 'Only used in tests')]?string $file = null, #[Deprecated(reason: 'Only used in tests')]?int $line = null): int { // When testing it might be more useful to see the full stack trace, so we have an option to actually throw the exception. if (config('app.throw_on_console_exception', false)) { From 0fc3ad70e216f500b920094941b3f2fba32b4cba Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 30 Jan 2023 19:47:05 +0000 Subject: [PATCH 15/28] Apply fixes from StyleCI --- packages/framework/src/Console/Concerns/Command.php | 3 +-- packages/framework/tests/Feature/CommandTest.php | 4 +--- packages/publications/src/Commands/ValidatingCommand.php | 2 -- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index d9d99b94562..e501be02c44 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -4,12 +4,11 @@ namespace Hyde\Console\Concerns; +use function config; use Exception; use Hyde\Hyde; use JetBrains\PhpStorm\Deprecated; use LaravelZero\Framework\Commands\Command as BaseCommand; - -use function config; use function sprintf; /** diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index b83d45d2334..13aeaa3e459 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Testing\Feature; use Closure; +use function config; use Hyde\Console\Concerns\Command; use Hyde\Hyde; use Hyde\Testing\TestCase; @@ -12,8 +13,6 @@ use RuntimeException; use Symfony\Component\Console\Style\OutputStyle; -use function config; - /** * @covers \Hyde\Console\Concerns\Command */ @@ -196,7 +195,6 @@ public function testCanEnableThrowOnException() $this->assertSame(1, $code); } - } class MockableTestCommand extends Command diff --git a/packages/publications/src/Commands/ValidatingCommand.php b/packages/publications/src/Commands/ValidatingCommand.php index bea94bba043..0967317ecd0 100644 --- a/packages/publications/src/Commands/ValidatingCommand.php +++ b/packages/publications/src/Commands/ValidatingCommand.php @@ -6,12 +6,10 @@ use function __; use function array_merge; -use Exception; use Hyde\Console\Concerns\Command; use Illuminate\Support\Facades\Validator; use function in_array; use RuntimeException; -use function str_ends_with; use function ucfirst; /** From a517b32c4d4445656dbb520201c5997817428734 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:53:15 +0100 Subject: [PATCH 16/28] Create test case helper for setting safeHandle throw state --- packages/testing/src/TestCase.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/testing/src/TestCase.php b/packages/testing/src/TestCase.php index 589832c003a..458b8b0f6b6 100644 --- a/packages/testing/src/TestCase.php +++ b/packages/testing/src/TestCase.php @@ -4,6 +4,7 @@ namespace Hyde\Testing; +use function config; use function file_get_contents; use Hyde\Facades\Features; use Hyde\Hyde; @@ -55,4 +56,14 @@ protected function assertFileEqualsString(string $string, string $path, bool $st $this->assertEquals(normalize_newlines($string), normalize_newlines(file_get_contents(Hyde::path($path)))); } } + + /** + * Disable the throwing of exceptions on console commands for the duration of the test. + * + * Note that this only affects commands using the {@see \Hyde\Console\Concerns\Command::safeHandle()} method. + */ + protected function throwOnConsoleException(bool $throw = true): void + { + config(['app.throw_on_console_exception' => $throw]); + } } From 4246db72b077305c3d6aed39a1f53b196eccda3a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 20:54:57 +0100 Subject: [PATCH 17/28] Use the added throw control helper --- packages/framework/tests/Feature/CommandTest.php | 3 +-- .../tests/Feature/MakePublicationCommandTest.php | 15 +++++++-------- .../Feature/MakePublicationTypeCommandTest.php | 9 ++++----- .../StaticSiteBuilderPublicationModuleTest.php | 2 +- .../ValidatePublicationTypesCommandTest.php | 6 +++--- .../Feature/ValidatePublicationsCommandTest.php | 7 +++---- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index 13aeaa3e459..88f57f1f33e 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -5,7 +5,6 @@ namespace Hyde\Framework\Testing\Feature; use Closure; -use function config; use Hyde\Console\Concerns\Command; use Hyde\Hyde; use Hyde\Testing\TestCase; @@ -182,7 +181,7 @@ public function testHandleExceptionWithErrorLocation() public function testCanEnableThrowOnException() { - config(['app.throw_on_console_exception' => true]); + $this->throwOnConsoleException(); $command = new ThrowingTestCommand(); $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); diff --git a/packages/publications/tests/Feature/MakePublicationCommandTest.php b/packages/publications/tests/Feature/MakePublicationCommandTest.php index 2b21d230964..5643a275c81 100644 --- a/packages/publications/tests/Feature/MakePublicationCommandTest.php +++ b/packages/publications/tests/Feature/MakePublicationCommandTest.php @@ -5,7 +5,6 @@ namespace Hyde\Publications\Testing\Feature; use function array_merge; -use function config; use function file_get_contents; use function file_put_contents; use Hyde\Facades\Filesystem; @@ -24,7 +23,7 @@ class MakePublicationCommandTest extends TestCase protected function setUp(): void { parent::setUp(); - config(['app.throw_on_console_exception' => true]); + $this->throwOnConsoleException(); Filesystem::makeDirectory('test-publication'); Carbon::setTestNow(Carbon::create(2022)); @@ -54,7 +53,7 @@ public function test_command_creates_publication() public function test_command_with_no_publication_types() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->artisan('make:publication') ->expectsOutputToContain('Creating a new publication!') ->expectsOutput('Error: Unable to locate any publication types. Did you create any?') @@ -167,7 +166,7 @@ public function test_command_with_publication_type_passed_as_argument() public function test_command_with_invalid_publication_type_passed_as_argument() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->makeSchemaFile(); $this->artisan('make:publication foo') @@ -376,7 +375,7 @@ public function test_command_with_multiple_tag_inputs() public function test_media_input_with_no_images() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->makeSchemaFile([ 'canonicalField' => '__createdAt', 'fields' => [[ @@ -427,7 +426,7 @@ public function test_media_input_with_no_files_but_skips() public function test_tag_input_with_no_tags() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->makeSchemaFile([ 'canonicalField' => '__createdAt', 'fields' => [[ @@ -480,7 +479,7 @@ public function test_tag_input_with_no_tags_but_skips() public function test_tag_input_for_field_without_tagGroup_specified() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->makeSchemaFile([ 'canonicalField' => '__createdAt', 'fields' => [[ @@ -499,7 +498,7 @@ public function test_tag_input_for_field_without_tagGroup_specified() public function test_handleEmptyOptionsCollection_for_required_field() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->makeSchemaFile([ 'canonicalField' => '__createdAt', 'fields' => [[ diff --git a/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php b/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php index 2e6cfa7821d..7fa0439c1f0 100644 --- a/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php +++ b/packages/publications/tests/Feature/MakePublicationTypeCommandTest.php @@ -4,7 +4,6 @@ namespace Hyde\Publications\Testing\Feature; -use function config; use Hyde\Facades\Filesystem; use Hyde\Hyde; use Hyde\Publications\Commands\Helpers\InputStreamHandler; @@ -26,7 +25,7 @@ protected function setUp(): void { parent::setUp(); - config(['app.throw_on_console_exception' => true]); + $this->throwOnConsoleException(); } protected function tearDown(): void @@ -171,7 +170,7 @@ public function test_with_multiple_fields_of_the_same_name() public function test_with_existing_file_of_the_same_name() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->file('test-publication'); @@ -182,7 +181,7 @@ public function test_with_existing_file_of_the_same_name() public function test_with_existing_publication_of_the_same_name() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->directory('test-publication'); $this->file('test-publication/foo'); @@ -242,7 +241,7 @@ public function testWithTagFieldInput() public function testWithTagFieldInputButNoTags() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->directory('test-publication'); $this->artisan('make:publicationType "Test Publication"') diff --git a/packages/publications/tests/Feature/StaticSiteBuilderPublicationModuleTest.php b/packages/publications/tests/Feature/StaticSiteBuilderPublicationModuleTest.php index bb13bd948b1..fe7ca5e09ff 100644 --- a/packages/publications/tests/Feature/StaticSiteBuilderPublicationModuleTest.php +++ b/packages/publications/tests/Feature/StaticSiteBuilderPublicationModuleTest.php @@ -26,7 +26,7 @@ protected function setUp(): void { parent::setUp(); - config(['app.throw_on_console_exception' => true]); + $this->throwOnConsoleException(); } public function testCompilingWithPublicationTypeWithSeededFilesContainingAllFieldTypes() diff --git a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php index 3152ba98bff..989fec7c329 100644 --- a/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationTypesCommandTest.php @@ -16,12 +16,12 @@ protected function setUp(): void { parent::setUp(); - config(['app.throw_on_console_exception' => true]); + $this->throwOnConsoleException(); } public function testWithNoPublicationTypes() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->artisan('validate:publicationTypes') ->expectsOutputToContain('Validating publication schemas!') @@ -31,7 +31,7 @@ public function testWithNoPublicationTypes() public function testWithValidSchemaFile() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->directory('test-publication'); $publicationType = new PublicationType('test-publication', fields: [ diff --git a/packages/publications/tests/Feature/ValidatePublicationsCommandTest.php b/packages/publications/tests/Feature/ValidatePublicationsCommandTest.php index 31f0653a8c5..999737ef255 100644 --- a/packages/publications/tests/Feature/ValidatePublicationsCommandTest.php +++ b/packages/publications/tests/Feature/ValidatePublicationsCommandTest.php @@ -4,7 +4,6 @@ namespace Hyde\Publications\Testing\Feature; -use function config; use function file_put_contents; use Hyde\Hyde; use Hyde\Publications\Models\PublicationType; @@ -20,12 +19,12 @@ protected function setUp(): void { parent::setUp(); - config(['app.throw_on_console_exception' => true]); + $this->throwOnConsoleException(); } public function testWithNoPublicationTypes() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->artisan('validate:publications') ->expectsOutput('Error: No publication types to validate!') @@ -34,7 +33,7 @@ public function testWithNoPublicationTypes() public function testWithInvalidPublicationType() { - config(['app.throw_on_console_exception' => false]); + $this->throwOnConsoleException(false); $this->artisan('validate:publications', ['publicationType' => 'invalid']) ->expectsOutput('Error: Publication type [invalid] does not exist') From c3e01c7cbe3ffe0aee96892bcdda675760e62e3a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:00:50 +0100 Subject: [PATCH 18/28] Revert "Bypass inheritance conflict" This reverts commit 5806fc55b768ca072ad0db5c57c53e707310effa. --- .../src/Console/Commands/RebuildStaticSiteCommand.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php b/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php index 8a4c35cdd91..1854f39a63f 100644 --- a/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php +++ b/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php @@ -47,7 +47,7 @@ public function handle(): int try { $this->validate(); } catch (Exception $exception) { - return $this->customHandleException($exception); + return $this->handleException($exception); } (new RebuildService($this->path))->execute(); @@ -100,11 +100,9 @@ public function validate(): void /** * Output the contents of an exception. * - * @fixme Refactor to use new base command helpers - * * @return int Error code */ - public function customHandleException(Exception $exception): int + public function handleException(Exception $exception): int { $this->error('Something went wrong!'); $this->warn($exception->getMessage()); From d1d695bda8e13358c4a04ba6b6119804a247d080 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:02:00 +0100 Subject: [PATCH 19/28] Rename helper method for conflicting method name --- .../src/Console/Commands/RebuildStaticSiteCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php b/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php index 1854f39a63f..ffab098d709 100644 --- a/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php +++ b/packages/framework/src/Console/Commands/RebuildStaticSiteCommand.php @@ -47,7 +47,7 @@ public function handle(): int try { $this->validate(); } catch (Exception $exception) { - return $this->handleException($exception); + return $this->withException($exception); } (new RebuildService($this->path))->execute(); @@ -102,7 +102,7 @@ public function validate(): void * * @return int Error code */ - public function handleException(Exception $exception): int + public function withException(Exception $exception): int { $this->error('Something went wrong!'); $this->warn($exception->getMessage()); From 6af9fe25cd3739c4e39686b3216a3c638b23398c Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:19:39 +0100 Subject: [PATCH 20/28] Remove deprecated arguments only used in test --- packages/framework/src/Console/Concerns/Command.php | 7 +++---- packages/framework/tests/Feature/CommandTest.php | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index e501be02c44..a4d0af511d1 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -50,10 +50,9 @@ protected function safeHandle(): int /** * Handle an exception that occurred during command execution. * - * @param string|null $file The file where the exception occurred. Leave null to auto-detect. * @return int The exit code */ - public function handleException(Exception $exception, #[Deprecated(reason: 'Only used in tests')]?string $file = null, #[Deprecated(reason: 'Only used in tests')]?int $line = null): int + public function handleException(Exception $exception): int { // When testing it might be more useful to see the full stack trace, so we have an option to actually throw the exception. if (config('app.throw_on_console_exception', false)) { @@ -61,11 +60,11 @@ public function handleException(Exception $exception, #[Deprecated(reason: 'Only } // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. - if (str_ends_with($file ?? $exception->getFile(), 'Command.php')) { + if (str_ends_with($exception->getFile(), 'Command.php')) { $this->error("Error: {$exception->getMessage()}"); } else { $this->error("Error: {$exception->getMessage()} at ".sprintf('%s:%s', - $file ?? $exception->getFile(), $line ?? $exception->getLine() + $exception->getFile(), $exception->getLine() )); } diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index 88f57f1f33e..68a842fffc7 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -174,7 +174,7 @@ public function testHandleExceptionWithErrorLocation() }); $command->setOutput($output); - $code = $command->handle('TestClass.php', 10); + $code = $command->handle(); $this->assertSame(1, $code); } @@ -236,12 +236,12 @@ public function safeHandle(): int class ThrowingTestCommand extends Command { - public function handle(?string $file = 'TestCommand.php', ?int $line = null): int + public function handle(): int { try { throw new RuntimeException('This is a test'); } catch (RuntimeException $exception) { - return $this->handleException($exception, $file, $line); + return $this->handleException($exception); } } } From 395996de28a6756d4a37c06ed2c6aa33d4605bec Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:20:08 +0100 Subject: [PATCH 21/28] Remove tests causing more trouble than they're worth --- .../framework/tests/Feature/CommandTest.php | 44 +------------------ 1 file changed, 1 insertion(+), 43 deletions(-) diff --git a/packages/framework/tests/Feature/CommandTest.php b/packages/framework/tests/Feature/CommandTest.php index 68a842fffc7..2dcc215d73a 100644 --- a/packages/framework/tests/Feature/CommandTest.php +++ b/packages/framework/tests/Feature/CommandTest.php @@ -149,40 +149,10 @@ public function testSafeHandleException() $this->assertSame(1, $code); } - public function testHandleException() - { - $command = new ThrowingTestCommand(); - $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); - - $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'Error: This is a test'; - }); - - $command->setOutput($output); - $code = $command->handle(); - - $this->assertSame(1, $code); - } - - public function testHandleExceptionWithErrorLocation() - { - $command = new ThrowingTestCommand(); - $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); - - $output->shouldReceive('writeln')->once()->withArgs(function (string $message) { - return $message === 'Error: This is a test at TestClass.php:10'; - }); - - $command->setOutput($output); - $code = $command->handle(); - - $this->assertSame(1, $code); - } - public function testCanEnableThrowOnException() { $this->throwOnConsoleException(); - $command = new ThrowingTestCommand(); + $command = new SafeThrowingTestCommand(); $output = Mockery::mock(\Illuminate\Console\OutputStyle::class); @@ -233,15 +203,3 @@ public function safeHandle(): int throw new RuntimeException('This is a test'); } } - -class ThrowingTestCommand extends Command -{ - public function handle(): int - { - try { - throw new RuntimeException('This is a test'); - } catch (RuntimeException $exception) { - return $this->handleException($exception); - } - } -} From 0a1c4d38f143edece43e52c140bc1096ebae05c3 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:20:44 +0100 Subject: [PATCH 22/28] Split string in two strings and concatenation --- packages/framework/src/Console/Concerns/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index a4d0af511d1..2c018e40b20 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -63,7 +63,7 @@ public function handleException(Exception $exception): int if (str_ends_with($exception->getFile(), 'Command.php')) { $this->error("Error: {$exception->getMessage()}"); } else { - $this->error("Error: {$exception->getMessage()} at ".sprintf('%s:%s', + $this->error("Error: {$exception->getMessage()}"." at ".sprintf('%s:%s', $exception->getFile(), $exception->getLine() )); } From 7cac05cfb0a347ed0e40851a632ff0229c204187 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:21:13 +0100 Subject: [PATCH 23/28] Introduce local variable --- packages/framework/src/Console/Concerns/Command.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 2c018e40b20..4225050706f 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -63,9 +63,10 @@ public function handleException(Exception $exception): int if (str_ends_with($exception->getFile(), 'Command.php')) { $this->error("Error: {$exception->getMessage()}"); } else { - $this->error("Error: {$exception->getMessage()}"." at ".sprintf('%s:%s', + $location = " at ".sprintf('%s:%s', $exception->getFile(), $exception->getLine() - )); + ); + $this->error("Error: {$exception->getMessage()}".$location); } return Command::FAILURE; From 5f9fa62167d71bf5a307db898c4bbaa44f08f143 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:22:23 +0100 Subject: [PATCH 24/28] Extract common parts from if statement --- packages/framework/src/Console/Concerns/Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 4225050706f..7ff1370605f 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -61,13 +61,13 @@ public function handleException(Exception $exception): int // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. if (str_ends_with($exception->getFile(), 'Command.php')) { - $this->error("Error: {$exception->getMessage()}"); + $location = ''; } else { $location = " at ".sprintf('%s:%s', $exception->getFile(), $exception->getLine() ); - $this->error("Error: {$exception->getMessage()}".$location); } + $this->error("Error: {$exception->getMessage()}".$location); return Command::FAILURE; } From 4c055686102114ca74572af3e5990449d9b5d3ca Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:22:27 +0100 Subject: [PATCH 25/28] Simplify 'if' --- packages/framework/src/Console/Concerns/Command.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 7ff1370605f..8bd19b49cf0 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -60,13 +60,9 @@ public function handleException(Exception $exception): int } // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. - if (str_ends_with($exception->getFile(), 'Command.php')) { - $location = ''; - } else { - $location = " at ".sprintf('%s:%s', + $location = str_ends_with($exception->getFile(), 'Command.php') ? '' : " at ".sprintf('%s:%s', $exception->getFile(), $exception->getLine() ); - } $this->error("Error: {$exception->getMessage()}".$location); return Command::FAILURE; From 984f4c757c8a1c55bd6b7c7223460e96f5dc4fe4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:23:16 +0100 Subject: [PATCH 26/28] Merge concatenated string into sprintf call --- packages/framework/src/Console/Concerns/Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 8bd19b49cf0..a6c09a879fa 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -60,7 +60,7 @@ public function handleException(Exception $exception): int } // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. - $location = str_ends_with($exception->getFile(), 'Command.php') ? '' : " at ".sprintf('%s:%s', + $location = str_ends_with($exception->getFile(), 'Command.php') ? '' : sprintf(' at %s:%s', $exception->getFile(), $exception->getLine() ); $this->error("Error: {$exception->getMessage()}".$location); From 986b5c854a4b38ce7834ee2bddc5c933d7e1e56b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 30 Jan 2023 21:23:44 +0100 Subject: [PATCH 27/28] Format sprintf call --- packages/framework/src/Console/Concerns/Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index a6c09a879fa..71c83613ebc 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -61,8 +61,8 @@ public function handleException(Exception $exception): int // If the exception was thrown from the same file as a command, then we don't need to show which file it was thrown from. $location = str_ends_with($exception->getFile(), 'Command.php') ? '' : sprintf(' at %s:%s', - $exception->getFile(), $exception->getLine() - ); + $exception->getFile(), $exception->getLine() + ); $this->error("Error: {$exception->getMessage()}".$location); return Command::FAILURE; From c27acf29e09b7e07e477d40b72a8a63b53f0a508 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 30 Jan 2023 20:24:12 +0000 Subject: [PATCH 28/28] Apply fixes from StyleCI --- packages/framework/src/Console/Concerns/Command.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/framework/src/Console/Concerns/Command.php b/packages/framework/src/Console/Concerns/Command.php index 71c83613ebc..a68e21d24f0 100644 --- a/packages/framework/src/Console/Concerns/Command.php +++ b/packages/framework/src/Console/Concerns/Command.php @@ -7,7 +7,6 @@ use function config; use Exception; use Hyde\Hyde; -use JetBrains\PhpStorm\Deprecated; use LaravelZero\Framework\Commands\Command as BaseCommand; use function sprintf;