Skip to content

VarTagTypeRuleHelper: remove all useless/wrong complexity#2805

Closed
janedbal wants to merge 2 commits intophpstan:1.10.xfrom
janedbal:var-tag-check-type-simplify
Closed

VarTagTypeRuleHelper: remove all useless/wrong complexity#2805
janedbal wants to merge 2 commits intophpstan:1.10.xfrom
janedbal:var-tag-check-type-simplify

Conversation

@janedbal
Copy link
Contributor

@janedbal janedbal commented Dec 5, 2023

This is a better version of #2804, adding more and more conditions for edgecases will always leave rooms for errors. Lets keep it simple & correct.

Maybe we can enable this strict & proper behaviour only for bleedingEdge?

[
'PHPDoc tag @var with type array is not subtype of native type array{}.',
29,
],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All constant assignments are working fine. Those is imo invalid "init variable" approaches and should be reported.

But, this is probably solvable in shouldVarTagTypeBeReported if you really insist on previous behaviour.

@ondrejmirtes
Copy link
Member

The complexity is there precisely because I want to allow:

/** @var int[] $foo */
$foo = returnListOfIntegers();

Because it's harmless and PHPStan cannot be too annoying.

After one of your changes (#2738) I had to spend some time iterating so that the rule isn't too annoying:

As it stands right now when someone comes to me with a question regarding "PHPStan complains about my @var, what I should do?" I know how to help them and how they should improve their code.

After #2738 I found situations in real-world code that I myself didn't know how to solve (aside from deleting @var which led to other issues), that's why I needed to adjust it.

Inline @var PHPDoc is a horrible multi-faceted beast and we need to have the current heuristics so that PHPStan strikes the right balance between being useful and too pedantic.

@janedbal
Copy link
Contributor Author

janedbal commented Dec 6, 2023

I believe there should be some option to have it strictly based on type system, not based on your taste. This way (with all this complexity), there will be many more holes - just found another issue (array{id: int}|null can be widened to mixed[]|null)

Also, allowing list to array widening may seem ok-ish for one, but we utilize lists a lot and this is degrading our efforts.

All those holes are hard to spot, I invested quite some time to find all those I reported (1, 2, 3, 4). But if you insist keeping this as-is, I can always add the strict & proper behaviour to https://github.com/shipmonk-rnd/phpstan-rules

Last thing to consider: maybe if there was some way to do "declare variable type" in PHPStan (like some @type), people would stop abusing vardoc for that and many of those edgecases you want to adjust may disappear.

@ondrejmirtes
Copy link
Member

An idea: @phpstan-var where we can afford to be a bit stricter.

@janedbal
Copy link
Contributor Author

janedbal commented Dec 7, 2023

Not really a fan of that idea. I think people are used to that

  • @phpstan-param and @psalm-param and @param
  • @phpstan-return and @psalm-return and @return

are almost synonyms. With some exceptions in IDEs.

@janedbal
Copy link
Contributor Author

@ondrejmirtes What about feature flag strictVarDocChecks ?

@ondrejmirtes
Copy link
Member

@janedbal Feature flags are for things we eventually want to enable for everybody (in the next major version). Which already applies to this rule (it's only in bleeding edge).

But I don't want this proposed behaviour in the next major version.

@ondrejmirtes
Copy link
Member

If you mean a new permanent option like listed here https://phpstan.org/config-reference#stricter-analysis, then sure, I'd merge that, there's already reportWrongPhpDocTypeInVarTag that makes it a bit more strict (and is enabled in phpstan-strict-rules).

@janedbal
Copy link
Contributor Author

Yeah, I mean regular option. Thx, I'll prepare that.

@janedbal
Copy link
Contributor Author

I suggest to name it stricterPhpDocTypeCheckInVarTag to correlate with existing one:

- reportWrongPhpDocTypeInVarTag: true
- stricterPhpDocTypeCheckInVarTag: true

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.

2 participants