Fix: Paginator Iterator fails on single pages and skips last page in multi-page results - #66
Fix: Paginator Iterator fails on single pages and skips last page in multi-page results#66jonathanpmartins wants to merge 4 commits into
Conversation
Sobre o commit adicional no PRPeço desculpas pela confusão. Acabei incluindo acidentalmente um commit (fb58343) que pertence a outro pull request (#65) neste PR do Paginator. Problema com PHPStan e PHP 8.4Não consegui fazer o PHPStan passar completamente devido a uma incompatibilidade específica do PHP 8.4: Este warning ocorre no construtor do public function __construct(
RequestTransport $requestTransport,
Request $listRequest,
array $lastResult = null // ❌ Implicitly nullable no PHP 8.4
)Possíveis soluçõesOpção 1: Resolver a compatibilidadePara resolver completamente, seria necessário:
Opção 2: Deprecar PHP 7.3Se o projeto estiver pronto para deprecar PHP 7.3, podemos resolver todos os warnings do PHPStan de uma vez, atualizando para: public function __construct(
RequestTransport $requestTransport,
Request $listRequest,
?array $lastResult = null
) |
Hello, thanks for the contribution! The snippet shown is specifically nullable types, not union types, supported since PHP 7.1. Therefore, we could use |






Problem Description
The
Paginatorclass implements PHP'sIteratorinterface but contains a critical bug in thevalid()method that causes:foreachloops never executeThis affects any code using
foreachloops with the Paginator, making it unusable for single-page results and unreliable for multi-page results.🔍 Root Cause Analysis
The issue is in the
valid()method logic:Why this fails:
Iteratorinterface requiresvalid()to returntruefor the current position to be processedhasNextPage = false(single page or last page),valid()returnsfalseforeachto terminate before processing the current page dataImpact scenarios:
hasNextPage = false→valid() = false→ no iterationhasNextPage = false→valid() = false→ last page skipped✅ Solution
Replace the flawed logic with proper boundary checking:
Why this works:
truewhen we haven't loaded data yet (allows first iteration)truewhen currentskipposition is within the total available datafalsewhen we've moved beyond all available data🧪 Test Coverage
Added comprehensive test suite covering:
✅ Single-page iteration - Verifies
foreachworks withhasNextPage = false✅ Multi-page last page inclusion - Ensures all pages are processed
✅ Empty results handling - Edge case verification
✅ Original bug scenario reproduction - Integration test matching the bug report
✅ Boundary condition testing - Various
skippositions✅ Updated existing
testValid()- Fixed test that was expecting buggy behavior📊 Before vs After
Before fix (BROKEN):
After fix (WORKING):
🚨 Data Loss Prevention
This bug was causing silent data loss in production applications:
The fix ensures 100% data integrity for all pagination scenarios.
📁 Files Changed
src/Paginator.php- Fixedvalid()method logictests/PaginatorTest.php- Added comprehensive test coverage🔒 Breaking Changes
None. This is a pure bug fix that:
✨ Verification
Run the test suite to verify the fix:
Expected results:
🎯 Impact
This fix resolves a critical data integrity issue that affects any application using the OpenPix PHP SDK for paginated data retrieval. Users can now confidently use
foreachloops with Paginator instances knowing that:Priority: 🔴 Critical - Fixes data loss bug affecting production applications