Skip to content

Native types of class constants and tentative return types, walk dead catches and closure attributes - #6859

Open
ondrejmirtes wants to merge 1 commit into
2.3.xfrom
native-types-dead-catch-closure-attributes
Open

ondrejmirtes wants to merge 1 commit into
2.3.xfrom
native-types-dead-catch-closure-attributes

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Four fixes found while auditing where Rector works around PHPStan behaviour.

Native type of class constant fetches ignored @var. getNativeType() of static::FOO on a non-final class returned the @var PHPDoc type, so with treatPhpDocTypesAsCertain: false comparisons like static::FOO === '' were reported as always false. With native types promoted, the PHPDoc type is no longer used: an overridable constant resolves to its native type, or to mixed without one. Final constants and constants of final classes still resolve to their value.

Tentative return types of built-in methods are native return types. getNativeType($arrayObject->count()) was mixed. On PHP 8.1+ the tentative return type is now the native return type when the signature has none. Before PHP 8.1, OverridingMethodRule no longer reports return types that are only tentative (previously this relied on the native type being mixed).

Dead catch blocks are walked. When no throw point matched a catch, its statements were never analysed, so catch (\InvalidArgumentException $e) { $e->nonExistent(); } after a try that cannot throw only reported "Dead catch". The body is now walked with the declared catch type; the result does not flow into the rest of the function. bug-14397 and testBug4043 expectations changed accordingly (the catch body is now analysed, so $foo is maybe-defined there, and a treatPhpDocTypesAsCertain tip is no longer shown).

Attributes of closures and arrow functions are walked through AttributesHandler, like those of functions and methods, so rules such as ClassConstantRule see their arguments.

All four have the matching turbo-ext changes. Tests pass with and without the extension; make phpstan and make cs are clean; side-by-side.php, signature-parity.php and smoke.php pass.

… catches and closure attributes

- Native type of a class constant fetch no longer comes from its @var PHPDoc:
  a constant that can be overridden resolves to its native type, or mixed
- Tentative return types of built-in methods are used as their native return
  types on PHP 8.1+
- Catch blocks that no throw point reaches are walked, so rules see their statements
- Attribute groups of closures and arrow functions are walked like those of
  functions and methods

Amp-Thread-ID: https://ampcode.com/threads/T-01a179f2-995f-7745-8d8c-4c174ba5c0ce
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.

1 participant