Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add failing tests for MissingReadOnlyPropertyAssignRule #339

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

Merged

Conversation

@herndlm
Copy link
Contributor

@herndlm herndlm commented May 30, 2022
edited
Loading

Closes phpstan/phpstan#7337

Looks like the problem is actually here in PropertiesExtension (which is now used at least). https://github.com/phpstan/phpstan-doctrine/blob/1.3.6/src/Rules/Doctrine/ORM/PropertiesExtension.php#L85 returns always false, because $metadata->isReadOnly (which marks if this is a Doctrine readonly entity?) is already false.

Does this need another early exit dealing with native readonly or smth like that? I gave it a shot, but don't know much about this here. Feel free to finish it :)

@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch from d4606d0 to 84328de Compare May 30, 2022 20:42
@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch from 30c140f to 34ec40e Compare May 30, 2022 21:06
@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch from 34ec40e to c5a8966 Compare May 30, 2022 21:08
@herndlm herndlm marked this pull request as ready for review May 30, 2022 21:18
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Also the PropertieExtension is completely untested - when I initially wrote it, I was happy it removed all false-positives from a project I test each release with. (And I thought I'll never have to touch it again, haha.)

Can you write a few tests for the current behaviour too? Especially - if the whole entity is marked as readonly by Doctrine metadata + doesn't have a constructor, it means that the entity is inserted into database by other means and the ORM is used only for reading it -> properties should be always initialized. But if it's not readonly / has a constructor, it means the properties should not be initialized.

@herndlm herndlm marked this pull request as draft May 31, 2022 12:41
@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch from c5a8966 to fd75bfb Compare June 1, 2022 08:16
@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch from fd75bfb to c3834aa Compare June 1, 2022 08:19
Copy link
Contributor Author

herndlm commented Jun 1, 2022
edited
Loading

ok, that looks better I think. The isAlwaysWritten and isAlwaysRead are still not tested though. Any good test case ideas? UPDATE: ah, that's another rule.. let me quickly add some tests for that too

Or anything else you need? I can add it here or in follow-ups, whatever is preferred.

@herndlm herndlm marked this pull request as ready for review June 1, 2022 09:07
@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch 4 times, most recently from d95669e to e88fff5 Compare June 1, 2022 09:47
Copy link
Contributor Author

herndlm commented Jun 1, 2022

Most of PropertiesExtension should be covered now, sorry for the many force-pushes 😅


public function __construct(int $doubleAssigned)
{
$this->doubleAssigned = $doubleAssigned;
Copy link
Member

@ondrejmirtes ondrejmirtes Jun 1, 2022

Choose a reason for hiding this comment

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

I just got an idea for a rule improvement in phpstan/src. If an extension says isInitialized=true about a property, even the first assignment should say "readonly property is already assigned" :)

Copy link
Contributor Author

@herndlm herndlm Jun 1, 2022

Choose a reason for hiding this comment

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

interesting, I guess I can take a look at this in phpstan-src

@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch 2 times, most recently from e36b309 to 7c5f72a Compare June 1, 2022 13:04
@herndlm herndlm force-pushed the missing-read-only-property-assign-rule branch from 7c5f72a to 909516a Compare June 1, 2022 13:06
Copy link
Contributor Author

herndlm commented Jun 1, 2022
edited
Loading

based on your comments I found another simplification that I did. Not sure why it is there exactly, but I also took over that getIdentifierFieldNames exception handling. do you know still why this was added?

Copy link
Member

@herndlm Because ClassMetadata can throw various exceptions and I don't want PHPStan crashing on that. There's a new bleedingEdge rule (https://github.com/phpstan/phpstan-doctrine/blob/1.3.x/src/Rules/Doctrine/ORM/EntityMappingExceptionRule.php) that's gonna tell you if Doctrine doesn't like your mappings.

@ondrejmirtes ondrejmirtes merged commit 85339d7 into phpstan:1.3.x Jun 1, 2022
Copy link
Member

Thank you very much :)

@herndlm herndlm deleted the missing-read-only-property-assign-rule branch June 1, 2022 13:19
Copy link
Contributor Author

herndlm commented Jun 1, 2022

@herndlm Because ClassMetadata can throw various exceptions and I don't want PHPStan crashing on that. There's a new bleedingEdge rule (https://github.com/phpstan/phpstan-doctrine/blob/1.3.x/src/Rules/Doctrine/ORM/EntityMappingExceptionRule.php) that's gonna tell you if Doctrine doesn't like your mappings.

thx, makes sense. I assumed something like that, but didn't see the exception in the class/interface and wasn't sure. but I didn't look too long, maybe I just missed it ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Class App\Entity\User has an uninitialized readonly property $id. Assign it in the constructor.

AltStyle によって変換されたページ (->オリジナル) /