Skip to content

Commit

Permalink
Files without match are left untouched
Browse files Browse the repository at this point in the history
Signed-off-by: RJ Garcia <ragboyjr@icloud.com>
  • Loading branch information
ragboyjr committed Jun 1, 2020
1 parent fc15263 commit 9d27c96
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/GenerateStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/struct-gen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions test/Feature/StructGenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php
interface Acme {}
class Foo {}
function bar() {
}
PHP;
$this->assertEquals($input, $genStruct($input));
}
}

0 comments on commit 9d27c96

Please sign in to comment.