Skip to content

Conversation

@mattiabasone
Copy link
Contributor

@mattiabasone mattiabasone commented Jan 26, 2026

What is the purpose of the change

This PR improves the code quality and the strict type usage introduced in the previous PRs.

Verifying this change

This change is already covered by the existing test suite.

Documentation

  • Does this pull request introduce a new feature? no
  • If yes, how is the feature documented? not applicable

@github-actions github-actions bot added the PHP label Jan 26, 2026
@mattiabasone mattiabasone marked this pull request as ready for review January 26, 2026 14:33
foreach ($fieldSchema->fields() as $field) {
$field_name = $field->name();
if (!$json_val = $default_value[$field_name]) {
if (!$json_val = $defaultValue[$field_name]) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this work well when $defaultValue[$field_name] returns a falsy value like 0, false or '' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

uhm, it's wrong, nice catch.

break;
case self::SEEK_CUR:
if (0 > $this->current_index + $whence) {
if (0 > $this->currentIndex + $whence) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (0 > $this->currentIndex + $whence) {
if (0 > $this->currentIndex + $offset) {

?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure about this?

Copy link
Member

Choose a reason for hiding this comment

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

Well, I have 0 experience with PHP but AFAIU $whence is used to tell from which position in the file to seek (beginning, current, end). In the other switch arms it checks whether the calculated position is a negative value. Just for SEEK_CUR it uses the $whence instead of $offset.
I am 99.9% certain this is a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, it's a bug

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e997da5

AvroSchema::isPrimitiveType($writersSchemaType)
&& AvroSchema::isPrimitiveType($readersSchemaType)
) {
return $writersSchemaType === $readersSchemaType;
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong! It break type promotion, e.g. writer=Schema::Int and reader=Schema::Long. This is allowed by the Avro spec and there is code for it below (line 186+) but this check for equality returns early and does not allow it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, also the old check was wrong, right? Should be something like this:

if (
 $writersSchemaType === $readersSchemaType
 && AvroSchema::isPrimitiveType($writersSchemaType)
 && AvroSchema::isPrimitiveType($readersSchemaType)
 ) {
    return true;
}

Copy link
Member

Choose a reason for hiding this comment

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

This looks better but probably isPrimitiveType() is less expensive than === for complex schema types and should be first

Copy link
Contributor Author

@mattiabasone mattiabasone Jan 27, 2026

Choose a reason for hiding this comment

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

I've updated the check in 53f5894, wdyt?

@martin-g martin-g merged commit c499eef into apache:main Jan 30, 2026
22 checks passed
@martin-g
Copy link
Member

Thank you, @mattiabasone !

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants