Skip to content

Introduce __stringnotstringable#4457

Merged
ondrejmirtes merged 8 commits intophpstan:2.1.xfrom
VincentLanglet:fix/6560-string
Feb 9, 2026
Merged

Introduce __stringnotstringable#4457
ondrejmirtes merged 8 commits intophpstan:2.1.xfrom
VincentLanglet:fix/6560-string

Conversation

@VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Oct 19, 2025

Naming to debate ? __stringwithoutstringable ? __strictstring ?

intval($stringable) is never allowed, even without strict_types.

The current check in FunctionCallParameterCheck is

$this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes());

either we're doing something like

$this->ruleLevelHelper->accepts(
      $parameterType,
      $argumentValueType,
      $scope->isDeclareStrictTypes() || in_array($functionName, ['floatval', 'intval', 'doubleval'], true)
);

either we have to introduce the opposite of StringAlwaysAcceptingObjectWithToStringType like I did.

Closes phpstan/phpstan#6560
Closes #1005

@VincentLanglet VincentLanglet force-pushed the fix/6560-string branch 2 times, most recently from 20054a8 to ea50ab0 Compare October 19, 2025 19:10
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.

Look at StringAlwaysAcceptingObjectWithToStringType. Every new Type implementation also has to override isSuperTypeOf to give correct answers.

You can test that in TypeCombinatorTest::testUnion and testIntersect.

@@ -0,0 +1,98 @@
<?php
Copy link
Contributor

@staabm staabm Feb 9, 2026

Choose a reason for hiding this comment

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

in phpstan/phpstan#6560 (comment) there was mentioned this does not depend on strict types.

I think it would be useful to have another test with strict-types enabled
(in case this is not already covered somewhere)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

test added

@VincentLanglet VincentLanglet force-pushed the fix/6560-string branch 2 times, most recently from dcf10e1 to 78620a0 Compare February 9, 2026 09:41
Comment on lines +1 to +3
<?php

declare(strict_types=1);
Copy link
Contributor

@staabm staabm Feb 9, 2026

Choose a reason for hiding this comment

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

if we put declare(strict_types=1); on line 1 - right behind the starting tag - you might re-use the very same test without duplicating expectations in CallToFunctionParametersRuleTest ?

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

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

lgtm. needs cs-fix though

@ondrejmirtes ondrejmirtes enabled auto-merge (squash) February 9, 2026 13:03
@ondrejmirtes ondrejmirtes merged commit 5b85fc7 into phpstan:2.1.x Feb 9, 2026
635 of 639 checks passed
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.

Handle different ways to cast the same

3 participants