Skip to content

From 100x to 1000x faster#419

Open
Ostico wants to merge 47 commits intoklein:masterfrom
matecat:master
Open

From 100x to 1000x faster#419
Ostico wants to merge 47 commits intoklein:masterfrom
matecat:master

Conversation

@Ostico
Copy link

@Ostico Ostico commented Oct 23, 2025

Version 3.2.0

Features

  • Lightning fast routing! From 100x to 1000x faster dispatch. Big performance improvements for large route collections with radix Tree routing. Benchmarks
  • 100% Code Coverage
  • Breaking changes, see UPGRADING.md

Ostico and others added 30 commits October 14, 2025 17:37
- Updated Phpunit to 9.6
- Implicitly marking parameter $app_context as nullable is deprecated
Removed version specification from the project description.
- Radix Tree creation by url tokens instead of characters
Improved: Radix Index backwards lookup.
@Ostico Ostico changed the title From 100x to 100x faster From 100x to 1000x faster Oct 24, 2025
mocked with PHPUnit >= 12

Because of their decision to not allow methods named "method" to be
mocked (throw Exception), we must introduce a big breaking change.

sebastianbergmann/phpunit#5415
Reorganized namespaces from `Klein\Tests` to `Matecat\Tests\Klein` for consistency. Introduced new benchmarks and optimized route validation methods for improved performance and clarity. Removed unused files and applied coding standards across classes.
Fixed 20 PHPStan errors across the codebase to improve type safety and code
quality. All changes maintain backward compatibility and existing functionality.

Changes by file:

* HeaderDataCollection.php:
  - Add PHPDoc type hint for constructor array parameter

* Klein.php:
  - Fix $requestMethod parameter type in filterMatchingRoutes() (string instead of string|array)
  - Refactor boolean expression to avoid "always true" warning by separating
    method matching logic from route URI assignment

* Route.php:
  - Remove nullable type from $regex property (never assigned null)
  - Change @type to @var annotations for PHPStan compatibility
  - Initialize $regexMatchingParams array property with default empty array
  - Remove unnecessary null coalescing operators on $namespace property
  - Add PHPStan suppressions for defensive runtime checks (is_array validation
    and nested array exception)

* RouteRegexCompiler.php:
  - Fix private constant access by using self:: instead of static::

* RadixRouteIndex.php:
  - Add complete PHPDoc type hints for constructor parameters
  - Update property types to support recursive array references in radix tree
  - Fix preg_split offset access with proper false handling
  - Add PHPStan suppressions for recursive array structure and type inference
    edge cases in loop logic

Result: PHPStan analysis now passes with zero errors.
Ostico and others added 2 commits February 19, 2026 15:41
…e safety

Add strict_types declaration to all 34 PHP files under src/Klein/.
Fix all resulting TypeError failures and resolve all PHPStan errors.

strict_types — added to 33 files (Route.php already had it):
  AbstractResponse, AbstractRouteFactory, App, DataCollection,
  HeaderDataCollection, ResponseCookieDataCollection, RouteCollection,
  ServerDataCollection, all Exceptions/*, HttpMethod, HttpStatus,
  Klein, Request, Response, ResponseCookie, RouteFactory,
  RouteRegexCompiler, ServiceProvider, IndexInterface,
  RadixRouteIndex, Validator

Type coercion fixes exposed by strict_types:
  - HeaderDataCollection: cast array key to string before explode()
  - ResponseCookieDataCollection: use $value->getName() as key
    instead of relying on implicit int-to-string coercion
  - ServiceProvider::markdown(): cast $arg to string before
    htmlentities()
  - ServiceProvider::validateParam(): explicitly cast mixed param()
    return to ?string before passing to validate()

PHPStan fixes (0 errors remaining):
  - HeaderDataCollection: add @param array type hint
  - Klein::filterMatchingRoutes(): narrow $requestMethod to string
  - Klein catch-all filter: separate setRouteMatchedAgainstUri()
    from boolean expression (was "always true" right-hand side)
  - Route::$regex: remove unused nullable type
  - Route::$regexMatchingParams/$routeMatched: change @type to @var
    for PHPStan, add default value
  - Route: remove unnecessary ?? on non-nullable $namespace
  - Route::validateMethod(): rewrite using HttpMethod::tryFrom()
    instead of duplicated 9-arm match blocks; validate array
    elements with is_string() guard (annotated @var mixed)
  - RouteRegexCompiler::getPathFor(): self:: instead of static::
    for private constant
  - RadixRouteIndex: add PHPDoc types for constructor params and
    property; handle preg_split false return; suppress recursive
    array reference and loop-inference edge cases

Code quality (incidental, already in working tree):
  - Rename snake_case properties to camelCase: protocol_version,
    is_name_prepared, number_of_skips, route_factory,
    error_callbacks, http_error_callbacks, after_filter_callbacks,
    output_buffer_level, http_messages
  - Klein: use InvalidCallableException instead of raw
    InvalidArgumentException for uncallable callbacks
  - InvalidCallableException: add constructor with message prefix,
    use proper imports
  - RouteFactory/DataCollection: remove stray blank lines after
    opening brace
  - Klein: add visibility to class constants
  - Klein::with(): require_once instead of require
  - Request: expand @param $files to full array shape
  - MockRequestFactory: add parameter types and return type
  - RequestTest: use realistic associative arrays and file
    structures in test data

Tests: 298 passed, 790 assertions. PHPStan: 0 errors.
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.

1 participant

Comments