From 9d27c9680b8dcde78a0b726546a6006a95634db3 Mon Sep 17 00:00:00 2001 From: RJ Garcia Date: Mon, 1 Jun 2020 18:06:24 -0400 Subject: [PATCH] Files without match are left untouched Signed-off-by: RJ Garcia --- src/GenerateStruct.php | 4 ++++ src/struct-gen.php | 2 +- test/Feature/StructGenTest.php | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/GenerateStruct.php b/src/GenerateStruct.php index c4f38ef..b7aa011 100644 --- a/src/GenerateStruct.php +++ b/src/GenerateStruct.php @@ -37,6 +37,10 @@ public function __invoke(string $code): string { $nodeFinder = new NodeFinder; $factory = new BuilderFactory(); $classesToGenerateStructs = $this->findClassesToGenerateStructs($nodeFinder, $ast); + if (!$classesToGenerateStructs) { + return $code; + } + $alreadyExistingGeneratedTraits = $this->findAlreadyExistingGeneratedTraits($nodeFinder, $ast, $classesToGenerateStructs); $code = $this->removeExistingTraitsFromOriginalCode($code, $alreadyExistingGeneratedTraits); diff --git a/src/struct-gen.php b/src/struct-gen.php index 301b2c5..405beca 100644 --- a/src/struct-gen.php +++ b/src/struct-gen.php @@ -38,7 +38,7 @@ function generateStructsForFiles(iterable $files, LoggerInterface $logger = null $logger->debug('No changes detected.'); } else { $logger->info("New Struct Info: \n". $updated); + file_put_contents($file->getPathname(), $updated); } - file_put_contents($file->getPathname(), $updated); } } diff --git a/test/Feature/StructGenTest.php b/test/Feature/StructGenTest.php index ba84945..6774f71 100644 --- a/test/Feature/StructGenTest.php +++ b/test/Feature/StructGenTest.php @@ -49,4 +49,19 @@ public function can_generate_structs_for_files() { trim(file_get_contents(self::GENERATED_FILE_PATH)) ); } + + /** @test */ + public function files_without_any_matching_classes_are_left_untouched() { + $genStruct = new GenerateStruct(); + $input = <<<'PHP' +assertEquals($input, $genStruct($input)); + } }