json-api-php icon indicating copy to clipboard operation
json-api-php copied to clipboard

Cast getLimit() result to int instead of string

Open FallDi opened this issue 5 years ago • 3 comments

Currently method getLimit() returns string, which is different from PHPDoc. It could affect PHP apps which used declare(strict_types=1);

FallDi avatar Sep 22 '20 05:09 FallDi

It could affect PHP apps which used declare(strict_types=1);

As far as I know, docblocks aren't affected by strict_types, only real type hints are?

franzliedke avatar Sep 24 '20 22:09 franzliedke

Yes, you're right declare(strict_types=1) affect only real type hints.

Let me show you my case. I read PHPDoc of getLimit(), which declared return type as int. Then I implement some code in project which relay on it

<?php declare(strict_types=1);

class OffsetLimitFilter {
	public function __construct(int $limit, int $offset) {
		// ..
	}
}

Finally I'm trying to pass getLimit() to this class

$parameters = new \Tobscure\JsonApi\Parameters($request->query->all());
$offsetOrderFilter = new OffsetLimitFilter(
    $parameters->getLimit() ? (int)$parameters->getLimit() : 10, // Cast to int is necessary due to return value actually is string, and php will throw type error
    $parameters->getOffset()
);

If this patch will be applied then source code could be simplified

$parameters = new \Tobscure\JsonApi\Parameters($request->query->all());
$offsetOrderFilter = new OffsetLimitFilter(
    $parameters->getLimit() ? : 10, // Cast to int is not necessary
    $parameters->getOffset()
);

FallDi avatar Sep 25 '20 08:09 FallDi

By all means, I like the change. Sorry for not communicating this clearly. :laughing:

franzliedke avatar Sep 25 '20 08:09 franzliedke