Skip to content

Commit 9a0c000

Browse files
committed
Fix linter issues
1 parent 5cb6d84 commit 9a0c000

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/Runtime/Value/JsonLikeValuePrinter.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ private function printOther(mixed $value): string
6969
*/
7070
private function printResource(mixed $resource): string
7171
{
72+
/** @var non-empty-string */
7273
return \get_resource_type($resource);
7374
}
7475

@@ -133,20 +134,33 @@ private function printBackedEnum(\BackedEnum $case, int $depth): string
133134
*/
134135
private function printFloat(float $value): string
135136
{
137+
/** @var non-empty-string */
136138
return match (true) {
137139
// NaN
138140
\is_nan($value) => StringTypeCoercer::NAN_TO_STRING,
139141
// Infinity
140142
$value === \INF => StringTypeCoercer::INF_TO_STRING,
141143
$value === -\INF => '-' . StringTypeCoercer::INF_TO_STRING,
142144
// Other floating point values
143-
default => \str_ends_with(
144-
haystack: $formatted = \rtrim(\sprintf('%f', $value), '0'),
145-
needle: '.',
146-
) ? $formatted . '0' : $formatted,
145+
default => $this->printWellFormedFloat($value),
147146
};
148147
}
149148

149+
/**
150+
* @return non-empty-string
151+
*/
152+
private function printWellFormedFloat(float $value): string
153+
{
154+
/** @var non-empty-string $formatted */
155+
$formatted = \rtrim(\sprintf('%f', $value), '0');
156+
157+
if (\str_ends_with($formatted, '.')) {
158+
$formatted .= '0';
159+
}
160+
161+
return $formatted;
162+
}
163+
150164
/**
151165
* @param int<0, max> $depth
152166
*

src/Type/Coercer/IntTypeCoercer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function coerce(mixed $value, Context $context): int
2626

2727
return match (true) {
2828
\is_int($value) => $value,
29-
\is_float($value) && $this->isSafeFloat($value) => (int) $value,
29+
\is_float($value) && self::isSafeFloat($value) => (int) $value,
3030
$value === false,
3131
$value === null => 0,
3232
$value === true => 1,
@@ -43,7 +43,7 @@ public function coerce(mixed $value, Context $context): int
4343
* Returns {@see true} in case of passed float value can be casting
4444
* without loss of precision and does not overflows integer min/max bounds.
4545
*/
46-
public static function isSafeFloat(float $value): bool
46+
final public static function isSafeFloat(float $value): bool
4747
{
4848
//
4949
// PHP int overflow checks.

src/Type/FloatLiteralType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function match(mixed $value, Context $context): bool
3737
public function cast(mixed $value, Context $context): float
3838
{
3939
if ($value === $this->value) {
40-
return (float) $value;
40+
return $value;
4141
}
4242

4343
if (!$context->isStrictTypesEnabled()) {

src/Type/NumericString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
) {}
2323

2424
/**
25-
* @phpstan-assert-if-true non-empty-string $value
25+
* @phpstan-assert-if-true numeric-string $value
2626
*/
2727
public function match(mixed $value, Context $context): bool
2828
{

0 commit comments

Comments
 (0)