From 7d2eaeaa669782d36a4b165a83ab319dde7d62f7 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 24 May 2022 19:38:44 +0200 Subject: [PATCH 01/12] Create FileNotFoundException.php --- src/Exceptions/FileNotFoundException.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/Exceptions/FileNotFoundException.php diff --git a/src/Exceptions/FileNotFoundException.php b/src/Exceptions/FileNotFoundException.php new file mode 100644 index 00000000..b59fae61 --- /dev/null +++ b/src/Exceptions/FileNotFoundException.php @@ -0,0 +1,10 @@ + Date: Tue, 24 May 2022 19:39:09 +0200 Subject: [PATCH 02/12] Use the custom exception --- src/Concerns/ValidatesExistence.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Concerns/ValidatesExistence.php b/src/Concerns/ValidatesExistence.php index e160770b..8ed8adef 100644 --- a/src/Concerns/ValidatesExistence.php +++ b/src/Concerns/ValidatesExistence.php @@ -2,18 +2,20 @@ namespace Hyde\Framework\Concerns; -use Exception; +use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Framework\Hyde; /** * Validate the existence of a Page model's source file. + * + * @see \Tests\Unit\ValidatesExistenceTest */ trait ValidatesExistence { /** * Check if a supplied source file exists or throw an exception. * - * @throws Exception If the file does not exist. + * @throws FileNotFoundException If the file does not exist. */ public function validateExistence(string $model, string $slug): void { @@ -22,7 +24,7 @@ public function validateExistence(string $model, string $slug): void $slug.$model::$fileExtension; if (! file_exists(Hyde::path($filepath))) { - throw new Exception("File $filepath not found.", 404); + throw new FileNotFoundException("File $filepath not found.", 404); } } } From 22976372e5733942f75e1d0f300e64b7e7717376 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 24 May 2022 19:39:15 +0200 Subject: [PATCH 03/12] Create ValidatesExistenceTest.php --- tests/Unit/ValidatesExistenceTest.php | 35 +++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/Unit/ValidatesExistenceTest.php diff --git a/tests/Unit/ValidatesExistenceTest.php b/tests/Unit/ValidatesExistenceTest.php new file mode 100644 index 00000000..456a7c9f --- /dev/null +++ b/tests/Unit/ValidatesExistenceTest.php @@ -0,0 +1,35 @@ +validateExistence(BladePage::class, 'index'); + + $this->assertTrue(true); + } + + public function test_validate_existence_throws_file_not_found_exception_if_file_does_not_exist() + { + $this->expectException(\Hyde\Framework\Exceptions\FileNotFoundException::class); + + $class = new class { + use ValidatesExistence; + }; + + $class->validateExistence(BladePage::class, 'not-found'); + } +} From 33d5490ed1e1da2ddf8226340c4b5dc10bbec938 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 24 May 2022 19:49:06 +0200 Subject: [PATCH 04/12] Replace qualifier with an import --- tests/Unit/ValidatesExistenceTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Unit/ValidatesExistenceTest.php b/tests/Unit/ValidatesExistenceTest.php index 456a7c9f..568cd068 100644 --- a/tests/Unit/ValidatesExistenceTest.php +++ b/tests/Unit/ValidatesExistenceTest.php @@ -3,6 +3,7 @@ namespace Tests\Unit; use Hyde\Framework\Concerns\ValidatesExistence; +use Hyde\Framework\Exceptions\FileNotFoundException; use Hyde\Framework\Models\BladePage; use Tests\TestCase; @@ -24,7 +25,7 @@ public function test_validate_existence_does_nothing_if_file_exists() public function test_validate_existence_throws_file_not_found_exception_if_file_does_not_exist() { - $this->expectException(\Hyde\Framework\Exceptions\FileNotFoundException::class); + $this->expectException(FileNotFoundException::class); $class = new class { use ValidatesExistence; From 3de71b0dbcfa52a6bc54c8e1b74ef7c94f63207b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 24 May 2022 20:05:16 +0200 Subject: [PATCH 05/12] Create CouldNotParseDateStringException --- src/Concerns/HasDateString.php | 2 +- src/Exceptions/CouldNotParseDateStringException.php | 10 ++++++++++ src/Hyde.php | 3 --- src/Models/DateString.php | 13 +++++++++---- src/Models/MarkdownPost.php | 2 +- tests/Unit/DateStringTest.php | 11 +++++++++++ 6 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 src/Exceptions/CouldNotParseDateStringException.php diff --git a/src/Concerns/HasDateString.php b/src/Concerns/HasDateString.php index 0a3adb91..2b1ec719 100644 --- a/src/Concerns/HasDateString.php +++ b/src/Concerns/HasDateString.php @@ -14,7 +14,7 @@ trait HasDateString public ?DateString $date = null; /** - * @throws \Exception + * @throws \Hyde\Framework\Exceptions\CouldNotParseDateStringException */ public function constructDateString(): void { diff --git a/src/Exceptions/CouldNotParseDateStringException.php b/src/Exceptions/CouldNotParseDateStringException.php new file mode 100644 index 00000000..a1da344d --- /dev/null +++ b/src/Exceptions/CouldNotParseDateStringException.php @@ -0,0 +1,10 @@ +string = $string; - $this->dateTimeObject = new DateTime($this->string); + try { + $this->dateTimeObject = new DateTime($this->string); + } catch (\Exception $e) { + throw new CouldNotParseDateStringException($e->getMessage()); + } $this->datetime = $this->dateTimeObject->format('c'); $this->sentence = $this->dateTimeObject->format('l M jS, Y, \a\t g:ia'); diff --git a/src/Models/MarkdownPost.php b/src/Models/MarkdownPost.php index 0f2a699f..776e7f47 100644 --- a/src/Models/MarkdownPost.php +++ b/src/Models/MarkdownPost.php @@ -22,7 +22,7 @@ class MarkdownPost extends MarkdownDocument public static string $parserClass = MarkdownPostParser::class; /** - * @throws \Exception + * @throws \Hyde\Framework\Exceptions\CouldNotParseDateStringException */ public function __construct(array $matter, string $body, string $title = '', string $slug = '') { diff --git a/tests/Unit/DateStringTest.php b/tests/Unit/DateStringTest.php index cd1ffc22..891b6819 100644 --- a/tests/Unit/DateStringTest.php +++ b/tests/Unit/DateStringTest.php @@ -3,9 +3,13 @@ namespace Tests\Unit; use DateTime; +use Hyde\Framework\Exceptions\CouldNotParseDateStringException; use Hyde\Framework\Models\DateString; use PHPUnit\Framework\TestCase; +/** + * @covers \Hyde\Framework\Models\DateString + */ class DateStringTest extends TestCase { // Test it can parse a date string @@ -42,4 +46,11 @@ public function test_it_can_format_date_string_into_short_human_readable_string( $dateString = new DateString('2020-01-01 UTC'); $this->assertEquals('Jan 1st, 2020', $dateString->short); } + + // Test it handles invalid date strings + public function test_it_handles_invalid_date_strings() + { + $this->expectException(CouldNotParseDateStringException::class); + new DateString('foo bar'); + } } From 03fcecb6efc6a432c927ea9459e7ec9771e2db67 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 25 May 2022 13:48:50 +0200 Subject: [PATCH 06/12] Fix namespace --- tests/Feature/Actions/CreatesNewPageSourceFileTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php index 04fac809..50656fa1 100644 --- a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php +++ b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php @@ -1,6 +1,6 @@ Date: Wed, 25 May 2022 14:08:11 +0200 Subject: [PATCH 07/12] Create FileConflictException --- src/Actions/CreatesNewMarkdownPostFile.php | 6 +++--- src/Actions/CreatesNewPageSourceFile.php | 11 ++++++----- src/Exceptions/FileConflictException.php | 16 ++++++++++++++++ .../Actions/CreatesNewPageSourceFileTest.php | 5 +++-- .../Feature/Commands/HydeMakePageCommandTest.php | 5 +++-- 5 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 src/Exceptions/FileConflictException.php diff --git a/src/Actions/CreatesNewMarkdownPostFile.php b/src/Actions/CreatesNewMarkdownPostFile.php index d038a5d5..4909fda0 100644 --- a/src/Actions/CreatesNewMarkdownPostFile.php +++ b/src/Actions/CreatesNewMarkdownPostFile.php @@ -2,7 +2,7 @@ namespace Hyde\Framework\Actions; -use Exception; +use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Framework\Hyde; use Illuminate\Support\Str; @@ -95,14 +95,14 @@ public function __construct( * @param bool $force Should the file be created even if a file with the same path already exists? * @return string|false Returns the path to the file if successful, or false if the file could not be saved. * - * @throws Exception if a file with the same slug already exists and the force flag is not set. + * @throws FileConflictException if a file with the same slug already exists and the force flag is not set. */ public function save(bool $force = false): string|false { $path = Hyde::path("_posts/$this->slug.md"); if ($force !== true && file_exists($path)) { - throw new Exception("File at $path already exists! ", 409); + throw new FileConflictException($path); } $arrayWithoutSlug = ((array) $this); diff --git a/src/Actions/CreatesNewPageSourceFile.php b/src/Actions/CreatesNewPageSourceFile.php index 5d7c3db9..18527e37 100644 --- a/src/Actions/CreatesNewPageSourceFile.php +++ b/src/Actions/CreatesNewPageSourceFile.php @@ -3,6 +3,7 @@ namespace Hyde\Framework\Actions; use Exception; +use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; use Hyde\Framework\Models\DocumentationPage; @@ -53,12 +54,12 @@ public function __construct(string $title, string $type = MarkdownPage::class, p /** * Check if the file can be saved. * - * @throws Exception if the file already exists and cannot be overwritten + * @throws FileConflictException if the file already exists and cannot be overwritten */ public function canSaveFile(string $path): void { if (file_exists($path) && ! $this->force) { - throw new Exception("File $path already exists!", 409); + throw new FileConflictException($path); } } @@ -88,7 +89,7 @@ public function createPage(string $type): int|false /** * Create the Markdown file. * - * @throws Exception if the file cannot be saved. + * @throws FileConflictException if the file cannot be saved. */ public function createMarkdownFile(): int|false { @@ -105,7 +106,7 @@ public function createMarkdownFile(): int|false /** * Create the Blade file. * - * @throws Exception if the file cannot be saved. + * @throws FileConflictException if the file cannot be saved. */ public function createBladeFile(): int|false { @@ -133,7 +134,7 @@ public function createBladeFile(): int|false /** * Create the Documentation file. * - * @throws Exception if the file cannot be saved. + * @throws FileConflictException if the file cannot be saved. */ public function createDocumentationFile(): int|false { diff --git a/src/Exceptions/FileConflictException.php b/src/Exceptions/FileConflictException.php new file mode 100644 index 00000000..da20a9fa --- /dev/null +++ b/src/Exceptions/FileConflictException.php @@ -0,0 +1,16 @@ +message = $path ? "File already exists: {$path}" : $this->message; + } +} diff --git a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php index 50656fa1..d3a112a0 100644 --- a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php +++ b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php @@ -4,6 +4,7 @@ use Exception; use Hyde\Framework\Actions\CreatesNewPageSourceFile; +use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; use Hyde\Framework\Models\DocumentationPage; @@ -56,8 +57,8 @@ public function test_that_an_exception_is_thrown_if_file_already_exists_and_over $path = Hyde::path('_pages/foo.md'); file_put_contents($path, 'foo'); - $this->expectException(Exception::class); - $this->expectExceptionMessage("File $path already exists!"); + $this->expectException(FileConflictException::class); + $this->expectExceptionMessage("File already exists: $path"); $this->expectExceptionCode(409); new CreatesNewPageSourceFile('foo'); diff --git a/tests/Feature/Commands/HydeMakePageCommandTest.php b/tests/Feature/Commands/HydeMakePageCommandTest.php index 7e8840b2..3ce17bac 100644 --- a/tests/Feature/Commands/HydeMakePageCommandTest.php +++ b/tests/Feature/Commands/HydeMakePageCommandTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Commands; use Exception; +use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Framework\Hyde; use Tests\TestCase; @@ -102,8 +103,8 @@ public function test_command_fails_if_file_already_exists() { file_put_contents($this->markdownPath, 'This should not be overwritten'); - $this->expectException(Exception::class); - $this->expectExceptionMessage("File $this->markdownPath already exists!"); + $this->expectException(FileConflictException::class); + $this->expectExceptionMessage("File already exists: $this->markdownPath"); $this->expectExceptionCode(409); $this->artisan('make:page "8450de2 test page"')->assertExitCode(409); From 34919c122fa1cc5bde050abf4ed0820b34a9ca71 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 25 May 2022 14:14:04 +0200 Subject: [PATCH 08/12] Add exception message and code --- src/Concerns/ValidatesExistence.php | 2 +- src/Exceptions/FileNotFoundException.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Concerns/ValidatesExistence.php b/src/Concerns/ValidatesExistence.php index 8ed8adef..148392ee 100644 --- a/src/Concerns/ValidatesExistence.php +++ b/src/Concerns/ValidatesExistence.php @@ -24,7 +24,7 @@ public function validateExistence(string $model, string $slug): void $slug.$model::$fileExtension; if (! file_exists(Hyde::path($filepath))) { - throw new FileNotFoundException("File $filepath not found.", 404); + throw new FileNotFoundException($filepath); } } } diff --git a/src/Exceptions/FileNotFoundException.php b/src/Exceptions/FileNotFoundException.php index b59fae61..9cf2ef42 100644 --- a/src/Exceptions/FileNotFoundException.php +++ b/src/Exceptions/FileNotFoundException.php @@ -6,5 +6,11 @@ class FileNotFoundException extends Exception { - // + protected $message = 'File not found.'; + protected $code = 404; + + public function __construct(?string $path = null) + { + $this->message = $path ? "File not found: {$path}" : $this->message; + } } From d1291eaf38096636c365889d120ae10f93705488 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 25 May 2022 12:30:59 +0000 Subject: [PATCH 09/12] Apply fixes from StyleCI --- src/Exceptions/FileConflictException.php | 10 +++---- src/Exceptions/FileNotFoundException.php | 2 +- src/Models/DateString.php | 6 +++-- tests/Unit/ValidatesExistenceTest.php | 34 +++++++++++++----------- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/Exceptions/FileConflictException.php b/src/Exceptions/FileConflictException.php index da20a9fa..f6a9e489 100644 --- a/src/Exceptions/FileConflictException.php +++ b/src/Exceptions/FileConflictException.php @@ -7,10 +7,10 @@ class FileConflictException extends Exception { protected $message = 'A file already exists at this path.'; - protected $code = 409; + protected $code = 409; - public function __construct(?string $path = null) - { - $this->message = $path ? "File already exists: {$path}" : $this->message; - } + public function __construct(?string $path = null) + { + $this->message = $path ? "File already exists: {$path}" : $this->message; + } } diff --git a/src/Exceptions/FileNotFoundException.php b/src/Exceptions/FileNotFoundException.php index 9cf2ef42..1304ce47 100644 --- a/src/Exceptions/FileNotFoundException.php +++ b/src/Exceptions/FileNotFoundException.php @@ -7,7 +7,7 @@ class FileNotFoundException extends Exception { protected $message = 'File not found.'; - protected $code = 404; + protected $code = 404; public function __construct(?string $path = null) { diff --git a/src/Models/DateString.php b/src/Models/DateString.php index b41692a8..c4bc0d2e 100644 --- a/src/Models/DateString.php +++ b/src/Models/DateString.php @@ -7,6 +7,7 @@ /** * Parse a date string and create normalized formats. + * * @see \Tests\Unit\DateStringTest */ class DateString @@ -27,8 +28,9 @@ class DateString public string $short; /** - * @param string $string - * @throws \Hyde\Framework\Exceptions\CouldNotParseDateStringException + * @param string $string + * + * @throws \Hyde\Framework\Exceptions\CouldNotParseDateStringException */ public function __construct(string $string) { diff --git a/tests/Unit/ValidatesExistenceTest.php b/tests/Unit/ValidatesExistenceTest.php index 568cd068..563f0822 100644 --- a/tests/Unit/ValidatesExistenceTest.php +++ b/tests/Unit/ValidatesExistenceTest.php @@ -12,25 +12,27 @@ */ class ValidatesExistenceTest extends TestCase { - public function test_validate_existence_does_nothing_if_file_exists() - { - $class = new class { - use ValidatesExistence; - }; + public function test_validate_existence_does_nothing_if_file_exists() + { + $class = new class + { + use ValidatesExistence; + }; - $class->validateExistence(BladePage::class, 'index'); + $class->validateExistence(BladePage::class, 'index'); - $this->assertTrue(true); - } + $this->assertTrue(true); + } - public function test_validate_existence_throws_file_not_found_exception_if_file_does_not_exist() - { - $this->expectException(FileNotFoundException::class); + public function test_validate_existence_throws_file_not_found_exception_if_file_does_not_exist() + { + $this->expectException(FileNotFoundException::class); - $class = new class { - use ValidatesExistence; - }; + $class = new class + { + use ValidatesExistence; + }; - $class->validateExistence(BladePage::class, 'not-found'); - } + $class->validateExistence(BladePage::class, 'not-found'); + } } From 6a45a0dc41a7302d04c788f8ab856e3db9f4bf52 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 25 May 2022 19:18:42 +0200 Subject: [PATCH 10/12] Create UnsupportedPageTypeException --- src/Actions/CreatesNewPageSourceFile.php | 5 +++-- src/Exceptions/UnsupportedPageTypeException.php | 16 ++++++++++++++++ .../Actions/CreatesNewPageSourceFileTest.php | 4 ++-- 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 src/Exceptions/UnsupportedPageTypeException.php diff --git a/src/Actions/CreatesNewPageSourceFile.php b/src/Actions/CreatesNewPageSourceFile.php index 18527e37..d2bbad61 100644 --- a/src/Actions/CreatesNewPageSourceFile.php +++ b/src/Actions/CreatesNewPageSourceFile.php @@ -4,6 +4,7 @@ use Exception; use Hyde\Framework\Exceptions\FileConflictException; +use Hyde\Framework\Exceptions\UnsupportedPageTypeException; use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; use Hyde\Framework\Models\DocumentationPage; @@ -68,7 +69,7 @@ public function canSaveFile(string $path): void * * @param string $type FQDN of the page class * - * @throws Exception if the page type is not supported + * @throws UnsupportedPageTypeException if the page type is not supported */ public function createPage(string $type): int|false { @@ -83,7 +84,7 @@ public function createPage(string $type): int|false return $this->createDocumentationFile(); } - throw new Exception('The page type must be either "markdown", "blade", or "documentation"'); + throw new UnsupportedPageTypeException('The page type must be either "markdown", "blade", or "documentation"'); } /** diff --git a/src/Exceptions/UnsupportedPageTypeException.php b/src/Exceptions/UnsupportedPageTypeException.php new file mode 100644 index 00000000..0b1ef82a --- /dev/null +++ b/src/Exceptions/UnsupportedPageTypeException.php @@ -0,0 +1,16 @@ +message = $page ? "The page type is not supported: {$page}" : $this->message; + } +} diff --git a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php index d3a112a0..05fb3887 100644 --- a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php +++ b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php @@ -2,9 +2,9 @@ namespace Tests\Feature\Actions; -use Exception; use Hyde\Framework\Actions\CreatesNewPageSourceFile; use Hyde\Framework\Exceptions\FileConflictException; +use Hyde\Framework\Exceptions\UnsupportedPageTypeException; use Hyde\Framework\Hyde; use Hyde\Framework\Models\BladePage; use Hyde\Framework\Models\DocumentationPage; @@ -46,7 +46,7 @@ public function test_that_a_slug_is_generated_from_the_title() public function test_that_an_exception_is_thrown_for_invalid_page_type() { - $this->expectException(Exception::class); + $this->expectException(UnsupportedPageTypeException::class); $this->expectExceptionMessage('The page type must be either "markdown", "blade", or "documentation"'); (new CreatesNewPageSourceFile('682072b Test Page', 'invalid')); From f8452b9d697505733f147bf3f59e92abfb307727 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Wed, 25 May 2022 19:27:51 +0200 Subject: [PATCH 11/12] Clean up code --- src/Actions/CreatesNewPageSourceFile.php | 71 +++---------------- .../Actions/CreatesNewPageSourceFileTest.php | 4 +- 2 files changed, 13 insertions(+), 62 deletions(-) diff --git a/src/Actions/CreatesNewPageSourceFile.php b/src/Actions/CreatesNewPageSourceFile.php index d2bbad61..dbc61278 100644 --- a/src/Actions/CreatesNewPageSourceFile.php +++ b/src/Actions/CreatesNewPageSourceFile.php @@ -2,7 +2,6 @@ namespace Hyde\Framework\Actions; -use Exception; use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Framework\Exceptions\UnsupportedPageTypeException; use Hyde\Framework\Hyde; @@ -18,32 +17,10 @@ */ class CreatesNewPageSourceFile { - /** - * The Page title. - * - * @var string - */ public string $title; - - /** - * The Page slug. - */ public string $slug; + public string $outputPath; - /** - * The file path. - */ - public string $path; - - /** - * Construct the class. - * - * @param string $title - The page title, will be used to generate the slug - * @param string $type - The page type, FQDN of the page class - * @param bool $force - Overwrite any existing files? - * - * @throws Exception if the page type is not supported or the file already exists - */ public function __construct(string $title, string $type = MarkdownPage::class, public bool $force = false) { $this->title = $title; @@ -52,11 +29,7 @@ public function __construct(string $title, string $type = MarkdownPage::class, p $this->createPage($type); } - /** - * Check if the file can be saved. - * - * @throws FileConflictException if the file already exists and cannot be overwritten - */ + public function canSaveFile(string $path): void { if (file_exists($path) && ! $this->force) { @@ -64,13 +37,6 @@ public function canSaveFile(string $path): void } } - /** - * Create the page. - * - * @param string $type FQDN of the page class - * - * @throws UnsupportedPageTypeException if the page type is not supported - */ public function createPage(string $type): int|false { if ($type === MarkdownPage::class) { @@ -87,36 +53,26 @@ public function createPage(string $type): int|false throw new UnsupportedPageTypeException('The page type must be either "markdown", "blade", or "documentation"'); } - /** - * Create the Markdown file. - * - * @throws FileConflictException if the file cannot be saved. - */ public function createMarkdownFile(): int|false { - $this->path = Hyde::path("_pages/$this->slug.md"); + $this->outputPath = Hyde::path("_pages/$this->slug.md"); - $this->canSaveFile($this->path); + $this->canSaveFile($this->outputPath); return file_put_contents( - $this->path, + $this->outputPath, "---\ntitle: $this->title\n---\n\n# $this->title\n" ); } - /** - * Create the Blade file. - * - * @throws FileConflictException if the file cannot be saved. - */ public function createBladeFile(): int|false { - $this->path = Hyde::path("_pages/$this->slug.blade.php"); + $this->outputPath = Hyde::path("_pages/$this->slug.blade.php"); - $this->canSaveFile($this->path); + $this->canSaveFile($this->outputPath); return file_put_contents( - $this->path, + $this->outputPath, <<path = Hyde::path("_docs/$this->slug.md"); + $this->outputPath = Hyde::path("_docs/$this->slug.md"); - $this->canSaveFile($this->path); + $this->canSaveFile($this->outputPath); return file_put_contents( - $this->path, + $this->outputPath, "# $this->title\n" ); } diff --git a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php index 05fb3887..fb694bd7 100644 --- a/tests/Feature/Actions/CreatesNewPageSourceFileTest.php +++ b/tests/Feature/Actions/CreatesNewPageSourceFileTest.php @@ -132,12 +132,12 @@ public function test_that_the_file_path_can_be_returned() { $this->assertEquals( Hyde::path('_pages/682072b-test-page.md'), - (new CreatesNewPageSourceFile('682072b Test Page'))->path + (new CreatesNewPageSourceFile('682072b Test Page'))->outputPath ); $this->assertEquals( Hyde::path('_pages/682072b-test-page.blade.php'), - (new CreatesNewPageSourceFile('682072b Test Page', BladePage::class))->path + (new CreatesNewPageSourceFile('682072b Test Page', BladePage::class))->outputPath ); } } From 7ed4f6f1d5237d396958747d5ccd9dc0527f451e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 25 May 2022 17:28:06 +0000 Subject: [PATCH 12/12] Apply fixes from StyleCI --- src/Actions/CreatesNewPageSourceFile.php | 1 - src/Exceptions/UnsupportedPageTypeException.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Actions/CreatesNewPageSourceFile.php b/src/Actions/CreatesNewPageSourceFile.php index dbc61278..143bd2cb 100644 --- a/src/Actions/CreatesNewPageSourceFile.php +++ b/src/Actions/CreatesNewPageSourceFile.php @@ -29,7 +29,6 @@ public function __construct(string $title, string $type = MarkdownPage::class, p $this->createPage($type); } - public function canSaveFile(string $path): void { if (file_exists($path) && ! $this->force) { diff --git a/src/Exceptions/UnsupportedPageTypeException.php b/src/Exceptions/UnsupportedPageTypeException.php index 0b1ef82a..a2ecb496 100644 --- a/src/Exceptions/UnsupportedPageTypeException.php +++ b/src/Exceptions/UnsupportedPageTypeException.php @@ -7,7 +7,7 @@ class UnsupportedPageTypeException extends Exception { protected $message = 'The page type is not supported.'; - protected $code = 400; + protected $code = 400; public function __construct(?string $page = null) {