Reported automatically by a code-commandments agent.
|
|
| Prophet |
PreferEnumForClosedSetField |
| Location |
src/Http/Data/Field.php:47 |
What's wrong
AUTO-FIX leaves SAME-FILE readers of the retyped field unconverted. #28 correctly says CROSS-FILE call sites are the dev's job, but usages WITHIN the declaring file are reachable by the single-file rewrite and should be converted alongside the retype. Concrete: after 'repent --input enum-class=SchemaFieldType --input field=type' on src/Http/Data/Field.php (a Spatie Data class), the property + default were fixed correctly (v1.95.0), but two same-file methods broke:
(1) public function wireSocketType(): string { ...; return $this->type; } -> now returns SchemaFieldType, not string => TypeError. Should become 'return $this->type->value;'.
(2) public function elementWireType(): string|null { ...; return $this->type === 'array' ? null : $this->type; } -> '=== "array"' compares an enum to a string (always false), and it returns the enum where string|null is declared. Should become 'return SchemaFieldType::Array->equals($this->type) ? null : $this->type->value;' (or '=== SchemaFieldType::Array' + '->value').
Suggested same-file rewrites when retyping $ to EnumX:
(a) '$this->' used in a string context — returned from a ': string' method, string concatenation, passed to a string param — append '->value';
(b) '$this-> === ""' / '!==' — rewrite to the enum case (project convention here is the CompareSelf form 'EnumX::Case->equals($this->)', else '=== EnumX::Case').
Harder than the default-value fix, but in-file and deterministic. Cross-file readers remain manual per #28.
Flagged code
return $this->type->value;
For the fixer
Decide whether this is a false positive (tighten/guard the prophet), a wrong rule (adjust the rule or its config), or correct-but-unclear (improve the message/scripture). Add a fixture from the flagged code above.
Reported automatically by a code-commandments agent.
PreferEnumForClosedSetFieldsrc/Http/Data/Field.php:47What's wrong
AUTO-FIX leaves SAME-FILE readers of the retyped field unconverted. #28 correctly says CROSS-FILE call sites are the dev's job, but usages WITHIN the declaring file are reachable by the single-file rewrite and should be converted alongside the retype. Concrete: after 'repent --input enum-class=SchemaFieldType --input field=type' on src/Http/Data/Field.php (a Spatie Data class), the property + default were fixed correctly (v1.95.0), but two same-file methods broke:
(1) public function wireSocketType(): string { ...; return $this->type; } -> now returns SchemaFieldType, not string => TypeError. Should become 'return $this->type->value;'.
(2) public function elementWireType(): string|null { ...; return $this->type === 'array' ? null : $this->type; } -> '=== "array"' compares an enum to a string (always false), and it returns the enum where string|null is declared. Should become 'return SchemaFieldType::Array->equals($this->type) ? null : $this->type->value;' (or '=== SchemaFieldType::Array' + '->value').
Suggested same-file rewrites when retyping $ to EnumX:
(a) '$this->' used in a string context — returned from a ': string' method, string concatenation, passed to a string param — append '->value';
(b) '$this-> === ""' / '!==' — rewrite to the enum case (project convention here is the CompareSelf form 'EnumX::Case->equals($this->)', else '=== EnumX::Case').
Harder than the default-value fix, but in-file and deterministic. Cross-file readers remain manual per #28.
Flagged code
For the fixer
Decide whether this is a false positive (tighten/guard the prophet), a wrong rule (adjust the rule or its config), or correct-but-unclear (improve the message/scripture). Add a fixture from the flagged code above.