Skip to content

Commit

Permalink
PHP 8.4 Compatibility (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
nie7321 authored Jan 28, 2025
1 parent 82e43d5 commit 81b9763
Show file tree
Hide file tree
Showing 33 changed files with 1,215 additions and 887 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
fail-fast: false
matrix:
include:
- php: 8.0
- php: 8.1
- php: 8.2

- php: 8.3
- php: 8.4

steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [v2.0.0] - 2025-01-28
### Changed
- Support for PHP 8.4 added, and PHP 8.1 dropped.
- Fixups for PHP 8.4 implicit nullable type declarations have been made.
- This is a breaking change if you were extending the `ListCache` or `MapCache` classes and overriding their constructors.
- All other fixups impact functions and should not have an impact.
- The `_` base class has been renamed `_Lodash`, as `_` as a class name has been deprecated.
- The *namespace* `_` remains, so invocations of the functions as `\_\map()` will continue to function.
- The `_` *constant* remains, so accessing the `_` class with that via the constant will continue to work.

## [v1.1.1] - 2024-05-15
### Changed
- Added support for `sebastian/comparator` ^6

## [v1.1.0] - 2024-06-26
### Changed
- Added support for `sebastian/comparator` ^5

## [v1.0.0] - 2023-03-22
### Changed
- Fixups for PHP 8.2 string interpolation deprecations.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Lodash-PHP also comes with a global `_` class that can be used globally.
```php
<?php

_::each([1, 2, 3], function (int $item) {
_Lodash::each([1, 2, 3], function (int $item) {
var_dump($item);
});
```
Expand Down Expand Up @@ -1637,13 +1637,14 @@ with two arguments: (arrVal, othVal).
@return array the new duplicate free array.

Example:

```php
<?php
use function _\uniqWith;

$objects = [['x' => 1, 'y' => 2], ['x' => 2, 'y' => 1], ['x' => 1, 'y' => 2]]

uniqWith($objects, '_::isEqual')
uniqWith($objects, '_Lodash::isEqual')
// => [['x' => 1, 'y' => 2], ['x' => 2, 'y' => 1]]

```
Expand Down Expand Up @@ -1701,14 +1702,15 @@ elements of each group: (...group).
@return array the new array of regrouped elements.

Example:

```php
<?php
use function _\unzipWith;

$zipped = zip([1, 2], [10, 20], [100, 200])
// => [[1, 10, 100], [2, 20, 200]]

unzipWith(zipped, '_::add')
unzipWith(zipped, '_Lodash::add')
// => [3, 30, 300]

```
Expand Down Expand Up @@ -4516,6 +4518,7 @@ RegExp $options['interpolate'] = _::$templateSettings['interpolate'] The "interp
@return callable Returns the compiled template function.

Example:

```php
<?php
use function _\template;
Expand Down Expand Up @@ -4552,7 +4555,7 @@ $compiled([ 'users' => ['fred', 'barney'] ])
// => '<li>fred</li><li>barney</li>'
// Use custom template delimiters.
\_::$templateSettings['interpolate'] = '{{([\s\S]+?)}}'
\_Lodash::$templateSettings['interpolate'] = '{{([\s\S]+?)}}'
$compiled = template('hello {{ user }}!')
$compiled([ 'user' => 'mustache' ])
// => 'hello mustache!'
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
]
},
"require": {
"php": ">=8.0",
"php": ">=8.2",
"sebastian/comparator": "^1.2 | ^2.0 | ^2.1 | ^3.0 | ^4.0 | ^5.0 | ^6.0",
"symfony/property-access": "^2.7 | ^3.0 | ^4.0 | ^5.0"
},
Expand Down
4 changes: 2 additions & 2 deletions src/Array/findIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @param array $array The array to inspect.
* @param callable $predicate The function invoked per iteration.
* @param int $fromIndex The index to search from.
* @param ?int $fromIndex The index to search from.
*
* @return int the index of the found element, else `-1`.
* @example
Expand All @@ -47,7 +47,7 @@
* // => 2
* </code>
*/
function findIndex(array $array, $predicate, int $fromIndex = null): int
function findIndex(array $array, $predicate, ?int $fromIndex = null): int
{
$length = \count($array);
if (!$length) {
Expand Down
4 changes: 2 additions & 2 deletions src/Array/findLastIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @param array $array The array to inspect.
* @param mixed $predicate The function invoked per iteration.
* @param int $fromIndex The index to search from.
* @param ?int $fromIndex The index to search from.
*
* @return int the index of the found element, else `-1`.
* @example
Expand All @@ -36,7 +36,7 @@
* // => 2
* </code>
*/
function findLastIndex(array $array, $predicate, int $fromIndex = null): int
function findLastIndex(array $array, $predicate, ?int $fromIndex = null): int
{
$length = \count($array);
$index = $fromIndex ?? $length - 1;
Expand Down
4 changes: 2 additions & 2 deletions src/Array/flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @category Array
*
* @param array $array The array to flatten.
* @param ?array $array The array to flatten.
*
* @return array the new flattened array.
* @example
Expand All @@ -27,7 +27,7 @@
* // => [1, 2, [3, [4]], 5]
* </code>
*/
function flatten(array $array = null): array
function flatten(?array $array = null): array
{
return baseFlatten($array, 1);
}
4 changes: 2 additions & 2 deletions src/Array/indexOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @param array $array The array to inspect.
* @param mixed $value The value to search for.
* @param int $fromIndex The index to search from.
* @param ?int $fromIndex The index to search from.
*
* @return int the index of the matched value, else `-1`.
* @example
Expand All @@ -34,7 +34,7 @@
* // => 3
* </code>
*/
function indexOf(array $array, $value, int $fromIndex = null): int
function indexOf(array $array, $value, ?int $fromIndex = null): int
{
$inc = true;
$index = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/Array/lastIndexOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @param array $array The array to inspect.
* @param mixed $value The value to search for.
* @param int $fromIndex The index to search from.
* @param ?int $fromIndex The index to search from.
*
* @return int the index of the matched value, else `-1`.
* @example
Expand All @@ -32,7 +32,7 @@
* // => 1
* </code>
*/
function lastIndexOf(array $array, $value, int $fromIndex = null): int
function lastIndexOf(array $array, mixed $value, ?int $fromIndex = null): int
{
$index = \count($array) - 1;

Expand Down
4 changes: 2 additions & 2 deletions src/Array/slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*
* @param array $array The array to slice.
* @param int $start The start position.
* @param int $end The end position.
* @param ?int $end The end position.
*
* @return array the slice of `array`.
*/
function slice(array $array, int $start, int $end = null): array
function slice(array $array, int $start, ?int $end = null): array
{
return \array_slice($array, $start, $end);
}
2 changes: 1 addition & 1 deletion src/Function/memoize.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
* // => ['a', 'b']
* </code>
*/
function memoize(callable $func, callable $resolver = null)
function memoize(callable $func, ?callable $resolver = null)
{
$memoized = new class($func, $resolver ?? null) {

Expand Down
6 changes: 3 additions & 3 deletions src/Function/wrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*
* @category Function
*
* @param mixed $value The value to wrap.
* @param callable $wrapper The wrapper function.
* @param mixed $value The value to wrap.
* @param ?callable $wrapper The wrapper function.
*
* @return callable the new function.
* @example
Expand All @@ -32,7 +32,7 @@
* // => '<p>fred, barney, &amp; pebbles</p>'
* </code>
*/
function wrap($value, callable $wrapper = null): callable
function wrap($value, ?callable $wrapper = null): callable
{
return partial($wrapper ?? '\_\identity', $value);
}
2 changes: 1 addition & 1 deletion src/ListCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ListCache implements CacheInterface
{
use CacheDataTrait;

public function __construct(iterable $entries = null)
public function __construct(?iterable $entries = null)
{
$this->clear();

Expand Down
12 changes: 6 additions & 6 deletions src/Lodash.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @copyright Copyright (c) 2017
*/

final class _
final class _Lodash
{
public $__chain__ = false;

Expand Down Expand Up @@ -82,20 +82,20 @@ public function value()
}
}

function lodash($value): _
function lodash($value): _Lodash
{
return new _($value);
return new _Lodash($value);
}

// We can't use "_" as a function name, since it collides with the "_" function in the gettext extension
// Laravel uses a function __, so only register the alias if the function name is not in use
if (!function_exists('__')) {
function __($value): _
function __($value): _Lodash
{
return new _($value);
return new _Lodash($value);
}
}

if (!defined('_')) {
define('_', _::class);
define('_', _Lodash::class);
}
2 changes: 1 addition & 1 deletion src/MapCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class MapCache implements CacheInterface
{
use CacheDataTrait;

public function __construct(iterable $entries = null)
public function __construct(?iterable $entries = null)
{
$this->clear();

Expand Down
10 changes: 5 additions & 5 deletions src/Seq/chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* chain sequences enabled. The result of such sequences must be unwrapped
* with `->value()`.
*
* @category Seq
*
* @param mixed $value The value to wrap.
*
* @return \_ Returns the new `lodash` wrapper instance.
* @return \_Lodash Returns the new `lodash` wrapper instance.
* @category Seq
*
* @example
* <code>
* $users = [
Expand All @@ -39,9 +39,9 @@
* // => 'pebbles is 1'
* </code>
*/
function chain($value): \_
function chain($value): \_Lodash
{
/** @var \_ $result */
/** @var \_Lodash $result */
$result = __($value);
$result->__chain__ = true;

Expand Down
4 changes: 2 additions & 2 deletions src/String/endsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @param string $string The string to inspect.
* @param string $target The string to search for.
* @param int $position The position to search up to.
* @param ?int $position The position to search up to.
*
* @return boolean Returns `true` if `string` ends with `target`, else `false`.
* @example
Expand All @@ -33,7 +33,7 @@
* // => true
* </code>
*/
function endsWith(string $string, string $target, int $position = null): bool
function endsWith(string $string, string $target, ?int $position = null): bool
{
$length = \strlen($string);
$position = null === $position ? $length : +$position;
Expand Down
4 changes: 2 additions & 2 deletions src/String/parseInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @category String
*
* @param int|float|string $string The string to convert.
* @param int $radix The radix to interpret `string` by.
* @param ?int $radix The radix to interpret `string` by.
*
* @return int Returns the converted integer.
*
Expand All @@ -32,7 +32,7 @@
* // => 8
* </code>
*/
function parseInt($string, int $radix = null): int
function parseInt(int|float|string $string, ?int $radix = null): int
{
if (null === $radix) {
$radix = 10;
Expand Down
4 changes: 2 additions & 2 deletions src/String/startsWith.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @param string $string The string to inspect.
* @param string $target The string to search for.
* @param int $position The position to search from.
* @param ?int $position The position to search from.
*
* @return boolean Returns `true` if `string` starts with `target`, else `false`.
* @example
Expand All @@ -33,7 +33,7 @@
* // => true
* </code>
*/
function startsWith(string $string, string $target, int $position = null): bool
function startsWith(string $string, string $target, ?int $position = null): bool
{
$length = \strlen($string);
$position = null === $position ? 0 : +$position;
Expand Down
4 changes: 2 additions & 2 deletions src/String/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@
*/
function template(string $string, array $options = []): callable
{
$options = \array_merge_recursive(\_::$templateSettings, $options);
$options = \array_merge_recursive(\_Lodash::$templateSettings, $options);

$interpolate = $options['interpolate'] ?? reNoMatch;

$reDelimiters = \implode('|', [
($options['escape'] ?? reNoMatch),
($interpolate === \_::reInterpolate ? reEsTemplate : reNoMatch),
($interpolate === \_Lodash::reInterpolate ? reEsTemplate : reNoMatch),
$interpolate,
($options['evaluate'] ?? reNoMatch),
]);
Expand Down
Loading

0 comments on commit 81b9763

Please sign in to comment.