Repository navigation
Fix --generate-stubs signatures for intersections, true/never/static/parent, enums and readonly - #14114
Conversation
✅ Performance: unchanged · Types: unchanged
How to read this table
head 7997be1 · base cd22a51 (merge-base) · workflow run |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 The changes are in the owning layer (StubsGenerator / ClassLikeStubGenerator, plus the one storage field a typed constant needs), and the generated output looks right. I ran --generate-stubs with the PR branch on a small project with enums (backed, with self::CONST and 1 << 1 case values), a readonly promoted property, final/abstract, A&B and (A&B)|null, static/never/true/callable, a new Dep() default, a typed interface constant and a class extending RuntimeException. The result:
- enums are emitted as
enum Suit : string implements \App\L { case Hearts = 'h'; ... }without the syntheticname/valueproperties andUnitEnum/BackedEnum; implementsis kept for classes that also have a parent;- inherited interface constants are no longer re-declared, typed constants keep their type;
- the built-in classes that master dumps into the stub (hundreds of lines for
Closure,Countable,mysqli_*,Normalizer, ...) are gone.
No blocking findings.
Recommendations (non-blocking)
- The PR bundles about eight independent generator fixes. Nothing here is wrong, but splitting by theme (types, enums, modifiers, built-in exclusion) would make a later bisect or revert cheaper.
|
Thanks for testing it! I kept it as one PR because the parts depend on each other: once |
orklah
left a comment
There was a problem hiding this comment.
Thanks, the type, modifier and enum output looks right. I ran --generate-stubs on a project covering every construct in the title and php -l (PHP 8.5) accepts the result, except for two cases where the new code writes a stub that is a compile error. On master both cases still lint: the first gives an enum without cases, the second a string placeholder.
<?php
namespace App;
final class Dep {}
enum S: string { case A = self::P . "c"; const P = "x"; } // case A; -> Fatal: Case A of backed enum App\S must have a value
define('App\OBJ', new Dep());
class UsesGlobal { const G = OBJ; } // public const G = new \App\Dep(); -> Fatal: New expressions are not supported in this context(The first input is valid PHP. Psalm reports InvalidEnumCaseValue on it because it doesn't resolve the constant declared after the case, which is a separate bug. An enum case that refers to a constant of an unscanned file gets the same null value.)
Please also add a test that lints the generated stub, or parses it back, over a sample that covers these constructs. Substring assertions can't catch output that is invalid.
| $case_name, | ||
| $case_value instanceof TLiteralInt | ||
| ? new VirtualInt($case_value->value) | ||
| : ($case_value instanceof TLiteralString ? new VirtualString($case_value->value) : null), |
There was a problem hiding this comment.
getValue() returns null when the case value can't be resolved, and in a backed enum ($storage->enum_type !== null) case X; is a compile error. This needs a valid fallback for backed enums. A placeholder value has to stay unique, since duplicate case values are also a compile error. Skipping the case is another option.
|
|
||
| // a `new` default: its arguments are not kept | ||
| if ($atomic_type instanceof TNamedObject) { | ||
| return new VirtualNew(new VirtualFullyQualified($atomic_type->value)); |
There was a problem hiding this comment.
getExpressionFromType() is also used for class constants, global constants, property defaults and ArgumentsAnalyzer, not only for parameter defaults. A class constant whose type is a TNamedObject (e.g. const G = SOME_GLOBAL_OBJECT;) now prints public const G = new \App\Dep();, which PHP rejects. Please emit new only where PHP allows it (parameter defaults, global constants), e.g. from getFunctionParamNodes(), and keep the previous fallback elsewhere.
|
@orklah thanks, both reproduced, fixed in 41c5080:
|
021e803 to
5b3d414
Compare
orklah
left a comment
There was a problem hiding this comment.
Thanks, both cases from my last review are fixed and the lint test covers them.
Two things before merging:
-
Please rebase on master. #14413 (515bfbe) added the same
ClassConstantStorage::signature_typefield, set the same way inClassLikeNodeScanner::visitClassConstDeclaration()and copied inPopulator::inheritConstantsFromTrait(). The PR's changes to those three files can be dropped; the generator can use master's field as is. -
A native
finalmethod of a class that is only@finalin its docblock loses itsfinal:
/** @final */
class DocFinal {
final public function f(): void {} // stub: public function f(): void
}The class is written without final (correctly, since final_from_docblock), but the method's final is skipped because $storage->final is true. The condition in getMethodNodes() should only skip it when the class is written as final, e.g. !($storage->final && !$storage->final_from_docblock). Please add this case to testModifiersAndTypedConstantsAreKept.
…parent, enums and readonly Keep intersection and DNF types, true, false, never, void, mixed, null, callable, static and parent in generated signatures. Generate enums with their backing type and cases, keep new defaults, the readonly, final and abstract modifiers and the types of typed constants, keep the interfaces of a class that extends another, and leave out built-in classes and the constants a class inherits. Fixes #13596
…nstants A backed enum case whose value Psalm can't resolve gets a unique placeholder, and new is only written where PHP allows it: parameter defaults and global constants.
5b3d414 to
e65253f
Compare
|
@orklah thanks, both done:
The scanner change has one more effect: overriding a method that is declared |
orklah
left a comment
There was a problem hiding this comment.
Thanks @shu-chen-dot-ca!
Signature types
--generate-stubsnarrows intersection types, widenstrue/false, dropsnever,void,mixed,nullandcallable, and writesstatic/parentas class names.Before
After
Enums
An enum is written as a class implementing
BackedEnum/UnitEnum, with$name/$valueproperties and no cases.Before
After
newdefaultsBefore
After
Modifiers, typed constants, interfaces, inherited constants and built-in classes
readonly,finalandabstractand the type of a typed constant are dropped, a class that extends another loses its interfaces, inherited constants are declared again (a fatal error for afinalone), and built-in classes the code uses are copied in (php -lrejects the stub).Before
After
The arguments of a
newdefault are not kept:= new Arg(3)is written as= new \Arg().Fixes #13596