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

Verify json serializability of collector data in single process run #2881

Open
wants to merge 3 commits into
base: 1.10.x
Choose a base branch
from

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jan 18, 2024

@staabm staabm force-pushed the collector-serializing branch from 5ded329 to 64e17de Compare January 18, 2024 14:31
@staabm staabm force-pushed the collector-serializing branch from 64e17de to 00a20d4 Compare January 18, 2024 14:44
@staabm staabm marked this pull request as ready for review January 18, 2024 15:06
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the collector-serializing branch from df9698e to dc140e0 Compare January 18, 2024 15:39

$errors[] = $this->buildCollectedDataError(
sprintf(
'Data collected by %s is not JSON decodable and will not work in parallel analysis.',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we need to be more precise here. I could not come up with a better error message.

the most important point is, that a datastructure needs to fulfill json_decode(json_encode($data)) === $data

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.

What I'd like here too is a new PHPStan rule in Api namespace that would check this too.

Best way is a new method on Type - isJsonEncodable.

}

$restored = json_decode($json);
if ($restored === $collected->getData()) {
Copy link
Contributor

@mad-briller mad-briller Jan 18, 2024

Choose a reason for hiding this comment

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

thinking about this, is this actually the correct check?

if there is an object that implements JsonSerializable returned from a collector it will encode fine, but when decoded it will be an stdClass meaning the === check would fail

if it's intended that you can use JsonSerializable in collectors then the check needs to be more robust

Copy link
Contributor Author

@staabm staabm Jan 18, 2024

Choose a reason for hiding this comment

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

you describe exactly the case, why this rule was created. the behaviour of phpstan is different depending on whether phpstan is running with parallel worker or with a single worker (e.g. in --debug), since the collected data is only json encoded in the the worker and decoded in the main thread in parallel, which means when you put a JsonSerializable object into collected data it will work differently depending on the worker scenario.

in single worker mode the collector data is passed thru the php process 1:1 and does not get influenced by json encode/decode

Copy link
Contributor

Choose a reason for hiding this comment

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

ah my misunderstanding, when Ondrej said

This type would not be precise. Anything that is JSON-serializable works.

i thought the intention was that JsonSerializable should be permitted, thanks for clearing that up

@staabm
Copy link
Contributor Author

staabm commented Jan 18, 2024

What I'd like here too is a new PHPStan rule in Api namespace that would check this too.

@ondrejmirtes would the new rule check the generic template type of TValue of each collector? or do you have something different in mind?

——-

Best way is a new method on Type - isJsonEncodable.

we need two way json encode/decodability. Encode-ability alone - as we have it before this PR - only works in single worker mode, which actually means JsonSerializable objects are hard to work with in real world use-cases.

see #2881 (comment)

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

Successfully merging this pull request may close these issues.

4 participants