Skip to content

Commit 52fc803

Browse files
authored
Functional\match() to Functional\matching() (#224)
1 parent 1a775c7 commit 52fc803

File tree

5 files changed

+136
-89
lines changed

5 files changed

+136
-89
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"src/Functional/LessThanOrEqual.php",
7474
"src/Functional/LexicographicCompare.php",
7575
"src/Functional/Map.php",
76-
"src/Functional/Match.php",
76+
"src/Functional/Matching.php",
7777
"src/Functional/Maximum.php",
7878
"src/Functional/Memoize.php",
7979
"src/Functional/Minimum.php",

src/Functional/Functional.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,16 @@ final class Functional
254254
const map = '\Functional\map';
255255

256256
/**
257-
* @see \Functional\match
257+
* @see \Functional\matching
258+
* @deprecated
258259
*/
259260
const match = '\Functional\match';
260261

262+
/**
263+
* @see \Functional\matching
264+
*/
265+
const matching = '\Functional\matching';
266+
261267
/**
262268
* @see \Functional\maximum
263269
*/
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
use function Functional\tail;
1717
use function Functional\if_else;
1818

19+
use const PHP_VERSION_ID;
20+
1921
/**
2022
* Performs an operation checking for the given conditions
2123
*
2224
* @param array $conditions the conditions to check against
2325
*
2426
* @return callable|null the function that calls the callable of the first truthy condition
2527
*/
26-
function match(array $conditions)
28+
function matching(array $conditions)
2729
{
2830
MatchException::assert($conditions, __FUNCTION__);
2931

@@ -34,6 +36,19 @@ function match(array $conditions)
3436

3537
list($if, $then) = head($conditions);
3638

37-
return if_else($if, $then, match(tail($conditions)))($value);
39+
return if_else($if, $then, matching(tail($conditions)))($value);
3840
};
3941
}
42+
43+
44+
if (PHP_VERSION_ID < 80000) {
45+
eval(<<<'ALIAS'
46+
namespace Functional;
47+
48+
function match(array $conditions) {
49+
trigger_error('Functional\match() is will be unavailable with PHP 8. Use Functional\matching() instead', E_USER_DEPRECATED);
50+
return matching($conditions);
51+
}
52+
ALIAS
53+
);
54+
}

tests/Functional/MatchTest.php

Lines changed: 0 additions & 85 deletions
This file was deleted.

tests/Functional/MatchingTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
/**
4+
* @package Functional-php
5+
* @author Lars Strojny <lstrojny@php.net>
6+
* @copyright 2011-2017 Lars Strojny
7+
* @license https://opensource.org/licenses/MIT MIT
8+
* @link https://github.com/lstrojny/functional-php
9+
*/
10+
11+
namespace Functional\Tests;
12+
13+
use PHPUnit\Framework\Error\Deprecated;
14+
15+
use function Functional\matching;
16+
use function Functional\match;
17+
use function Functional\equal;
18+
use function Functional\const_function;
19+
20+
use const PHP_VERSION_ID;
21+
22+
class MatchingTest extends AbstractTestCase
23+
{
24+
public function testMatching()
25+
{
26+
$test = matching(
27+
[
28+
[equal('foo'), const_function('is foo')],
29+
[equal('bar'), const_function('is bar')],
30+
[equal('baz'), const_function('is baz')],
31+
[
32+
const_function(true),
33+
function ($x) {
34+
return 'default is ' . $x;
35+
}
36+
],
37+
]
38+
);
39+
40+
self::assertEquals('is foo', $test('foo'));
41+
self::assertEquals('is bar', $test('bar'));
42+
self::assertEquals('is baz', $test('baz'));
43+
self::assertEquals('default is qux', $test('qux'));
44+
}
45+
46+
public function testNothingMatching()
47+
{
48+
$test = matching(
49+
[
50+
[equal('foo'), const_function('is foo')],
51+
[equal('bar'), const_function('is bar')],
52+
]
53+
);
54+
55+
self::assertNull($test('baz'));
56+
}
57+
58+
public function testMatchingConditionIsArray()
59+
{
60+
$this->expectArgumentError('Functional\matching() expects condition at key 1 to be array, string given');
61+
62+
matching(
63+
[
64+
[const_function(null), const_function(null)],
65+
'',
66+
]
67+
);
68+
}
69+
70+
public function testMatchingConditionLength()
71+
{
72+
$this->expectArgumentError(
73+
'Functional\matching() expects size of condition at key 1 to be greater than or equals to 2, 1 given'
74+
);
75+
76+
matching(
77+
[
78+
[const_function(''), const_function('')],
79+
[''],
80+
]
81+
);
82+
}
83+
84+
public function testMatchingConditionCallables()
85+
{
86+
$this->expectException(\Functional\Exceptions\InvalidArgumentException::class);
87+
$this->expectExceptionMessage(
88+
'Functional\matching() expects first two items of condition at key 1 to be callables'
89+
);
90+
91+
matching(
92+
[
93+
[const_function(null), const_function(null)],
94+
[const_function(null), ''],
95+
]
96+
);
97+
}
98+
99+
public function testDeprecatedAlias()
100+
{
101+
if (PHP_VERSION_ID >= 80000) {
102+
self::markTestSkipped('Only works with PHP <8.0');
103+
}
104+
105+
$this->expectException(
106+
Deprecated::class,
107+
'Functional\match() is deprecated as it is incompatible with PHP 8. Use Functional\matching() instead'
108+
);
109+
match([]);
110+
}
111+
}

0 commit comments

Comments
 (0)