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

Improve class constant static analysis #7154

Merged
merged 3 commits into from Jan 26, 2022

Conversation

AndrolGenhald
Copy link
Collaborator

Add class const covariance support (fixes #5589).
Add check for overriding const from interface in PHP < 8.1 (fixes #7108).
Add check for ambiguous const inheritance.

@AndrolGenhald AndrolGenhald marked this pull request as draft December 13, 2021 23:43
@AndrolGenhald
Copy link
Collaborator Author

Depends on #7110 and #7123, I'll rebase once those are merged. (I actually can't remember why I used #7110 here, that might have been an accident; It could probably be pulled out if necessary).

Comment on lines +151 to +174
} elseif ($stmt instanceof PhpParser\Node\Stmt\ClassConst) {
$member_stmts[] = $stmt;

foreach ($stmt->consts as $const) {
$const_id = strtolower($this->fq_class_name) . '::' . $const->name;

foreach ($codebase->class_constants_to_rename as $original_const_id => $new_const_name) {
if ($const_id === $original_const_id) {
$file_manipulations = [
new FileManipulation(
(int) $const->name->getAttribute('startFilePos'),
(int) $const->name->getAttribute('endFilePos') + 1,
$new_const_name
)
];

FileManipulationBuffer::add(
$this->getFilePath(),
$file_manipulations
);
}
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied this part from ClassAnalyzer. Haven't tested it, but I assume it's probably fine.

@AndrolGenhald
Copy link
Collaborator Author

@orklah Can you rerun CI for this now that the int range issue is fixed? I'm not sure if I'm able to do that myself without pushing something.

@orklah
Copy link
Collaborator

orklah commented Jan 25, 2022

Done, except for circleCI, I have no dominion there 😄

@AndrolGenhald
Copy link
Collaborator Author

AndrolGenhald commented Jan 25, 2022

Oh, I didn't realize I still had things to fix, I thought I'd handled that already.
Edit: And I'm an idiot, of course fixing that on master isn't going to affect this, I'll obviously have to rebase...

@orklah
Copy link
Collaborator

orklah commented Jan 25, 2022

I was gonna post the same thing as your edit :p we're both idiots then 😄

Add class const covariance support (fixes vimeo#5589).
Add check for overriding const from interface in PHP < 8.1 (fixes vimeo#7108).
Add check for ambiguous const inheritance.
@AndrolGenhald AndrolGenhald force-pushed the feature/class-const-improvements branch from 08e5315 to 0b81f46 Compare January 25, 2022 21:39
@AndrolGenhald AndrolGenhald marked this pull request as ready for review January 25, 2022 21:43
@AndrolGenhald
Copy link
Collaborator Author

@orklah any idea what's up with this? I've run into it when testing locally too, but only sometimes.

@orklah
Copy link
Collaborator

orklah commented Jan 25, 2022

I'm not quite sure, but I think there is something related to the order of the tests

The failing test expects this exception and message:

$this->expectException(ConfigException::class);

most of the time it works, but sometimes, there is a message coming from here:

'PHP Error: ' . $error_message . ' in ' . $error_filename . ':' . $error_line,

Normally, the error handler is installed here:

ErrorHandler::install();

but there's also one here:

ErrorHandler::install();

What I don't get is that the exception I would expect most of the time is actually RuntimeException, not ConfigException

@AndrolGenhald
Copy link
Collaborator Author

Regardless, this should be ready for review and I think everything works correctly. Hopefully it's a bit easier to understand than some of the others 😆

@orklah orklah added PR: Need review release:feature The PR will be included in 'Features' section of the release notes and removed PR: Needs work labels Jan 25, 2022
public const CONSTANT='bar';
}

interface Baz extends Foo, Bar {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an issue when the type are compatibles?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an issue even when it's the same exact thing: https://3v4l.org/tjYML

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I thought "ambiguous" was too tame a word for something that broke at compile time, I was wrong!

@orklah orklah merged commit 66343de into vimeo:master Jan 26, 2022
@orklah
Copy link
Collaborator

orklah commented Jan 26, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Need review release:feature The PR will be included in 'Features' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add issue for overriding const from interface Support class const covariance checks
3 participants