Skip to content

Support indirect Stringable interface via __toString() in trait#1524

Merged
Ocramius merged 6 commits intoRoave:6.63.xfrom
staabm:trait
Sep 9, 2025
Merged

Support indirect Stringable interface via __toString() in trait#1524
Ocramius merged 6 commits intoRoave:6.63.xfrom
staabm:trait

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented Sep 9, 2025

required for phpstan/phpstan#13469

Copy link
Copy Markdown
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Patch meaningful, but the implementation (just inside the private method) needs to be refactored

Comment on lines +1335 to +1352
foreach ($this->getTraits() as $trait) {
foreach (array_keys($trait->immediateMethods) as $immediateMethodName) {
if (strtolower($immediateMethodName) === '__tostring') {
try {
$stringableInterfaceReflection = $this->reflector->reflectClass($stringableClassName);

if ($stringableInterfaceReflection->isInternal()) {
$interfaces[$stringableClassName] = $stringableInterfaceReflection;
}
} catch (IdentifierNotFound) {
// Stringable interface does not exist on target PHP version
}

// @infection-ignore-all Break_: There's no difference between break and continue - break is just optimization
break 2;
}
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of repeating this whole block, merge methods from the traits into immediate methods above?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

good point. fixed.

Copy link
Copy Markdown
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

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

Excellent, thanks @staabm!

@Ocramius Ocramius self-assigned this Sep 9, 2025
@Ocramius Ocramius changed the title Support indirect Stringable interface via __toString() in trait Support indirect Stringable interface via __toString() in trait Sep 9, 2025
@Ocramius Ocramius merged commit 0df4a31 into Roave:6.63.x Sep 9, 2025
37 checks passed
@staabm staabm deleted the trait branch September 9, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants