Skip to content

Conversation

@huangdijia
Copy link
Member

Summary

Fixed all 14 PHPStan type analysis errors and reorganized type test files into a more logical directory structure.

Changes

Type Test File Reorganization

  • Moved collection type tests from src/database/tests/types/ to src/collection/types/
  • Moved database type tests from src/database/tests/types/ to src/database/types/
  • Updated phpstan.types.neon.dist configuration to reflect new paths

PHPStan Type Fixes

Collection Types (src/collection/types/)

  • Collection.php (3 errors fixed):

    • Fixed mapToDictionary return type: array<int, float>array<int, float|int>
    • Fixed mapToGroups return type: Collection<int, float>Collection<int, float|int>
    • Fixed zip return type: Collection<int, float|int>Collection<int, array{float}|int>
  • Functions.php (1 error fixed):

    • Updated type assertion to use non-falsy-string instead of generic string
  • LazyCollection.php (4 errors fixed):

    • Updated all string-returning methods to use lowercase-string&numeric-string&uppercase-string type

Database Types (src/database/types/)

  • Model/Builder.php (4 errors fixed):

    • Fixed findOr and firstOr union type ordering: User|int42|User (literal type)
  • Model/Collection.php (1 error fixed):

    • Updated type assertion: string|User'string'|User (literal string type)
  • Model/Relations.php (1 error fixed):

    • Updated Relation::noConstraints return type to match PHPStan's precise inference

Testing

All PHPStan type checks now pass:

composer analyse-types
# ✅ [OK] No errors

Related Issues

Fixes type analysis errors that were preventing proper type checking in the codebase.

- Fixed all 14 PHPStan type errors in collection and database type tests
- Reorganized type test files from `src/database/tests/types` to proper locations:
  - Collection type tests moved to `src/collection/types/`
  - Database type tests moved to `src/database/types/`
- Updated PHPStan configuration to reflect new directory structure

Changes:
1. Collection type fixes:
   - Fixed float/int union types in mapToDictionary and mapToGroups
   - Fixed array type inference in zip method
   - Updated Functions.php to use proper numeric types

2. LazyCollection type fixes:
   - Updated type assertions to match PHPStan's precise string type inference
   - Fixed mapToDictionary, mapWithKeys, and flatMap type expectations

3. Database Builder type fixes:
   - Fixed union type ordering for findOr and firstOr methods (42|User instead of User|int)
   - Updated to match PHPStan's literal type inference

4. Database Collection type fixes:
   - Changed generic string type to literal string type ('string'|User)

5. Database Relations type fixes:
   - Updated Relation::noConstraints return type to match precise inference

All type checks now pass without errors.
@huangdijia huangdijia changed the title fix: Fixed PHPStan type analysis errors and reorganized type test files feature: Added PHPStan type analysis for hyperf/collection Oct 29, 2025
@limingxinleo limingxinleo merged commit 073a34f into hyperf:master Oct 29, 2025
74 checks passed
@huangdijia huangdijia deleted the fix/phpstan-type-errors branch October 29, 2025 06:18
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