Skip to content

Commit 9d27c96

Browse files
committed
Files without match are left untouched
Signed-off-by: RJ Garcia <ragboyjr@icloud.com>
1 parent fc15263 commit 9d27c96

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/GenerateStruct.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function __invoke(string $code): string {
3737
$nodeFinder = new NodeFinder;
3838
$factory = new BuilderFactory();
3939
$classesToGenerateStructs = $this->findClassesToGenerateStructs($nodeFinder, $ast);
40+
if (!$classesToGenerateStructs) {
41+
return $code;
42+
}
43+
4044
$alreadyExistingGeneratedTraits = $this->findAlreadyExistingGeneratedTraits($nodeFinder, $ast, $classesToGenerateStructs);
4145
$code = $this->removeExistingTraitsFromOriginalCode($code, $alreadyExistingGeneratedTraits);
4246

src/struct-gen.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function generateStructsForFiles(iterable $files, LoggerInterface $logger = null
3838
$logger->debug('No changes detected.');
3939
} else {
4040
$logger->info("New Struct Info: \n". $updated);
41+
file_put_contents($file->getPathname(), $updated);
4142
}
42-
file_put_contents($file->getPathname(), $updated);
4343
}
4444
}

test/Feature/StructGenTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,19 @@ public function can_generate_structs_for_files() {
4949
trim(file_get_contents(self::GENERATED_FILE_PATH))
5050
);
5151
}
52+
53+
/** @test */
54+
public function files_without_any_matching_classes_are_left_untouched() {
55+
$genStruct = new GenerateStruct();
56+
$input = <<<'PHP'
57+
<?php
58+
59+
interface Acme {}
60+
class Foo {}
61+
function bar() {
62+
63+
}
64+
PHP;
65+
$this->assertEquals($input, $genStruct($input));
66+
}
5267
}

0 commit comments

Comments
 (0)