Skip to content

Commit b69c9cb

Browse files
committed
Fixed issues from code review by CodeRabbit:
* min/max handling values outside of int64 range incorrectly; * toJSON failing incorrectly if json_encode fails; * indexOf having incorrect return type in doc comment; * typo in error message; * loose type check in createLambdaFromString (create_function was removed during merge of 2.0 branch). Updated CodeRabbit config to talk like Rafiki.
1 parent 8659d71 commit b69c9cb

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

Tests/Unit/EnumerableTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,12 @@ function testMax()
11801180
$this->assertSame(
11811181
5,
11821182
E::from([ 3, 5, 4 ])->max());
1183+
$this->assertSame(
1184+
-1e100,
1185+
E::from([ -1e100 ])->max());
1186+
$this->assertSame(
1187+
-PHP_INT_MAX,
1188+
E::from([ -INF, -PHP_INT_MAX, -PHP_FLOAT_MAX ])->max());
11831189

11841190
// max (selector)
11851191
$this->assertSame(
@@ -1235,6 +1241,12 @@ function testMin()
12351241
$this->assertSame(
12361242
3,
12371243
E::from([ 3, 5, 4 ])->min());
1244+
$this->assertSame(
1245+
1e100,
1246+
E::from([ 1e100 ])->min());
1247+
$this->assertSame(
1248+
PHP_INT_MAX,
1249+
E::from([ INF, PHP_INT_MAX, PHP_FLOAT_MAX ])->min());
12381250

12391251
// min (selector)
12401252
$this->assertSame(

YaLinqo/.github/.coderabbit.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Complete reference: https://docs.coderabbit.ai/reference/configuration
55

66
language: "en-US" # Set the language for reviews by using the corresponding ISO language code.
7-
tone_instructions: "" # Set the tone of reviews and chat. Example: 'You must use talk like Mr. T. I pity the fool who doesn't!' # Default: ""
7+
tone_instructions: "You must talk like the wise baboon Rafiki from The Lion King." # Set the tone of reviews and chat. Example: 'You must use talk like Mr. T. I pity the fool who doesn't!' # Default: ""
88
early_access: false # Enable early-access features. # Default: false
99
enable_free_tier: true # Enable free tier features for users not on a paid plan. # Default: true
1010

YaLinqo/Enumerable.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public function max($selector = null)
507507
{
508508
$selector = Utils::createLambda($selector, 'v,k', Functions::$value);
509509

510-
$max = -PHP_INT_MAX;
510+
$max = -INF;
511511
$assigned = false;
512512
foreach ($this as $k => $v) {
513513
$max = max($max, $selector($v, $k));
@@ -555,7 +555,7 @@ public function min($selector = null)
555555
{
556556
$selector = Utils::createLambda($selector, 'v,k', Functions::$value);
557557

558-
$min = PHP_INT_MAX;
558+
$min = INF;
559559
$assigned = false;
560560
foreach ($this as $k => $v) {
561561
$min = min($min, $selector($v, $k));
@@ -995,12 +995,16 @@ public function toDictionary($keySelector = null, $valueSelector = null): array
995995
* <p>This function only works with UTF-8 encoded data.
996996
* @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE. Default: 0.
997997
* @return string A JSON encoded string on success or false on failure.
998+
* @throws \UnexpectedValueException If json_encode fails.
998999
* @see json_encode
9991000
* @package YaLinqo\Conversion
10001001
*/
10011002
public function toJSON(int $options = 0): string
10021003
{
1003-
return json_encode($this->toArrayDeep(), $options);
1004+
$result = json_encode($this->toArrayDeep(), $options);
1005+
if ($result === false)
1006+
throw new \UnexpectedValueException(json_last_error_msg());
1007+
return $result;
10041008
}
10051009

10061010
/**

YaLinqo/EnumerablePagination.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public function singleOrFallback($fallback, $predicate = null)
319319
* <p><b>Syntax</b>: indexOf (value)
320320
* <p>To search for the zero-based index of the first occurence, call {@link toValues} method first.
321321
* @param mixed $value The value to locate in the sequence.
322-
* @return mixed The key of the first occurrence of value, if found; otherwise, false.
322+
* @return mixed The key of the first occurrence of value, if found; otherwise, null.
323323
* @package YaLinqo\Pagination
324324
*/
325325
public function indexOf($value)

YaLinqo/Errors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ class Errors
3030
/** Error message: "step must be a positive value." */
3131
const STEP_NEGATIVE = 'step must be a positive value.';
3232
/** Error message: "type must by one of built-in types." */
33-
const UNSUPPORTED_BUILTIN_TYPE = 'type must by one of built-in types.';
33+
const UNSUPPORTED_BUILTIN_TYPE = 'type must be one of built-in types.';
3434
}

YaLinqo/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private static function createLambdaFromString(string $closure, string $closureA
152152
if (strlen($code) > 0 && $code[0] != '{')
153153
$code = "return {$code};";
154154
$fun = eval("return function($args) { $code };");
155-
if (!$fun)
155+
if (!is_callable($fun))
156156
throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA);
157157
self::$lambdaCache[$closure][$closureArgs] = $fun;
158158
return $fun;

0 commit comments

Comments
 (0)