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

Migarting from version 3 to version 4 breaks making changes to existing classes using reflection #94

Closed
iganev opened this issue Jul 12, 2021 · 5 comments
Labels

Comments

@iganev
Copy link

iganev commented Jul 12, 2021

BC Break Report

Q A
Version 3.5.1

Summary

Migrating from 3.5.1 to 4.4.2 seems impossible. Generation based on reflection is fundamentally broken.

Previous behavior

There used to be a FileReflection class in the Reflection namespace. This class no longer exists. This breaks generating classes based on existing code. FileReflection used to hold the namespace, uses and a bunch of other misc stuff. Now IN THEORY these are held in the ClassReflection, however, uses are not being parsed at all. Loading a class reflection into a class generator and then generating the code results in ALL uses getting stripped out, rendering the class source code invalid.

Current behavior

Judging by the documented examples and by reading the code, currently to perform code generation on existing classes I must use the ClassReflection object as a consutrctor parameter to the ClassGenerator class. This however fails to parse the use statements OUTSIDE the class and they get permanently lost. This makes generation based on existing code impossible to work in real-world cases. Reading the code of the ClassGenerator I see that both traits and uses are being bundled together... this sounds like no external uses are supported at all...

How to reproduce

<?php

require_once './vendor/autoload.php';

// REPLACE WITH A CLASS THAT YOU KNOW CONTAINS "use" STATAMENTS
$className = \Models\Table\Users::class; // REPLACE THIS WITH A PROPER EXAMPLE

$generator = \Laminas\Code\Generator\ClassGenerator::fromReflection(new \Laminas\Code\Reflection\ClassReflection($className));

$generator->addConstant('TEST', 'test');

echo $generator->generate();
@iganev iganev changed the title Migarting from version 3 to version 4 breaks making changes to existing classes using reflection. Migarting from version 3 to version 4 breaks making changes to existing classes using reflection Jul 12, 2021
@iganev
Copy link
Author

iganev commented Jul 12, 2021

There also used to be a whole bunch of Scanners... I just tried to find the CachingFileScanner to feed the right data into FileGenerator and work around the issue. That Scanner along with most of the rest also seems to be purged out of existence.

@iganev
Copy link
Author

iganev commented Jul 12, 2021

Cool stuff... No changelog.. but digging into the commits I found this pull request:

#58

@iganev iganev closed this as completed Jul 12, 2021
@Ocramius
Copy link
Member

Ocramius commented Jul 12, 2021 via email

@iganev
Copy link
Author

iganev commented Jul 12, 2021

Not soon enough. But eventually did. The so-called "better alternative" requires PHP >= 7.4.1 && < 7.5. So 8 is out of the question.

@Ocramius
Copy link
Member

Ocramius commented Jul 12, 2021

Yes, I and @asgrim try to do what we can during our free time: you can try and help on Roave/BetterReflection#701 if you have an active need, and you or your employer want to chip in.

Meanwhile, the scanner component was in shambles with PHP 8 anyway, and maintaining it further was out of question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants