Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: nette/php-generator do not take care of replacements before converting FQCNs #867

Open
gturpin-dev opened this issue Dec 20, 2024 · 0 comments
Labels
Bug Something isn't working Generation

Comments

@gturpin-dev
Copy link
Contributor

gturpin-dev commented Dec 20, 2024

Tempest Version

dev-main

PHP Version

8.4

Operating System

Windows

Description

Working with make commands or in general with the nette/php-generator wrapper from Tempest ( in ClassManipulator for example ).
I wanted to make replacements in the stub file to replace a string by a FQCN.
I therefore expected that the replacements will transform the string to my FQCN and then the ClassManipulator will convert the FQCN in method body to class short name and use statement. ( located in SimplifiesClassNames::extractFqcnFromBody()

It seems like the replacements are made after the extractFqcnFromBody.
I suggest to dig it and check if we can just reverse the order or if this require further tweaks.

I also don't know if reversing the order could break anything or not.

Steps to Reproduce

Have a stub file like this :

final class MigrationModelStub implements DatabaseMigration
{
    public function up(): ?QueryStatement
    {
        return CreateTableStatement::forModel('DummyModel')
            ->primary()
            ->text('name')
            ->datetime('created_at')
            ->datetime('updated_at');
    }
}

If we add replacements via the StubFileGenerator like this :

$this->stubFileGenerator->generateClassFile(
    stubFile: $stubFile,
    targetPath: $targetPath,
    shouldOverride: $shouldOverride,
    replacements: $replacements = [
        "'DummyModel'" => sprintf('%s::class', '\Tempest\Database\SomethingThatImplementsDatabaseModel'),
    ]
);

Then the FQCN stay there without being processed to be shortened

The code is run through abstractions but the main instructions are those ones :

$classManipulator = (new ClassManipulator($stubFile->filePath))
    ->setNamespace($namespace)
    ->setClassName($classname);
    
foreach ($replacements as $placeholder => $replacement) {
    if (! is_string($replacement)) {
        continue;
    }

    $classManipulator->manipulate(fn (StringHelper $code) => $code->replace($placeholder, $replacement));
}

// Run all manipulations
$classManipulator = array_reduce(
    array: $manipulations,
    callback: fn (ClassManipulator $manipulator, Closure $manipulation) => $manipulation($manipulator),
    initial: $classManipulator,
);

$classManipulator->save($targetPath);
@gturpin-dev gturpin-dev added Bug Something isn't working Triage New issues that need to be reviewed by the Tempest team. labels Dec 20, 2024
@gturpin-dev gturpin-dev changed the title [Bug]: nette/php-generator support of property hooks is missing in our implementation #866 [Bug]: nette/php-generator support of property hooks is missing in our implementation #866 Dec 20, 2024
@gturpin-dev gturpin-dev changed the title [Bug]: nette/php-generator support of property hooks is missing in our implementation #866 [Bug]: nette/php-generator do not take care of replacements before converting FQCNs Dec 20, 2024
@innocenzi innocenzi added Generation and removed Triage New issues that need to be reviewed by the Tempest team. labels Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Generation
Projects
None yet
Development

No branches or pull requests

2 participants