Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern matching #102

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Zend/tests/pattern_matching/is/and.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
And pattern
--FILE--
<?php

interface A {}
interface B {}
interface C {}
class Foo implements A, B {}

var_dump(1 is int & 1);
var_dump(2 is int & (1|2));
var_dump(3 is float & 1);
var_dump(4 is int & float);
var_dump([] is [] & [...]);
var_dump('foo' is string & 'bar');
var_dump(new Foo() is A&B);
var_dump(new Foo() is (A&C));

?>
--EXPECT--
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
bool(true)
bool(false)
30 changes: 30 additions & 0 deletions Zend/tests/pattern_matching/is/array.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Array pattern
--FILE--
<?php

var_dump([] is []);
var_dump(42 is []);
var_dump('foo' is []);
var_dump([42] is [42]);
var_dump([42] is []);
var_dump([42] is [43]);
var_dump([42] is ['0' => 42]);
var_dump([42, 43] is [42]);
var_dump([42, 43] is [42, ...]);
var_dump([42] is [$a]);
var_dump($a);

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
bool(true)
bool(true)
int(42)
10 changes: 10 additions & 0 deletions Zend/tests/pattern_matching/is/array_mixed_keys.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Array pattern with mixed implicit and explicit keys
--FILE--
<?php

var_dump([] is ['foo', 1 => 'bar']);

?>
--EXPECTF--
Fatal error: Must not mix implicit and explicit array keys in array pattern in %s on line %d
32 changes: 32 additions & 0 deletions Zend/tests/pattern_matching/is/bail.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Object pattern matching
--FILE--
<?php

(function () {
$o = new stdClass();

try {
var_dump($o is self);
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}

try {
var_dump($o is parent);
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}

try {
var_dump($o is static);
} catch (Throwable $e) {
echo $e->getMessage(), "\n";
}
})();

?>
--EXPECT--
Cannot access "self" when no class scope is active
Cannot access "parent" when no class scope is active
Cannot access "static" when no class scope is active
87 changes: 87 additions & 0 deletions Zend/tests/pattern_matching/is/binding.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
--TEST--
Binding pattern
--FILE--
<?php

class Box {
public function __construct(
public $value,
) {}
}

class NotBox {
public function __construct(
public $value,
) {}
}

class Many {
public function __construct(
public $a = 1,
public $b = 2,
public $c = 3,
public $d = 4,
public $e = 5,
public $f = 6,
public $g = 7,
public $h = 8,
public $i = 9,
public $j = 10,
) {}
}

var_dump(10 is $a);
var_dump($a);

var_dump('Hello world' is $a);
var_dump($a);

var_dump(new Box(42) is Box & { value: $a });
var_dump($a);

var_dump(new NotBox(43) is Box & { value: $a });
var_dump($a);

var_dump(43 is $a & int);
var_dump($a);

var_dump([] is $a & string);
var_dump($a);

var_dump(new Many() is { $a, $b, $c, $d });
var_dump($a, $b, $c, $d, isset($e));

var_dump(new Many() is { $a, $b, $c, $d, $e, $f, $g, $h, $i, $j });
var_dump($a, $b, $c, $d, $e, $f, $g, $h, $i, $j);

?>
--EXPECT--
bool(true)
int(10)
bool(true)
string(11) "Hello world"
bool(true)
int(42)
bool(false)
int(42)
bool(true)
int(43)
bool(false)
int(43)
bool(true)
int(1)
int(2)
int(3)
int(4)
bool(false)
bool(true)
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)
int(7)
int(8)
int(9)
int(10)
28 changes: 28 additions & 0 deletions Zend/tests/pattern_matching/is/binding_destruct.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Object pattern matching destructor
--FILE--
<?php

class Foo {
public function __destruct() {
throw new Exception('Here');
}
}

$foo = new Foo();
$bar = 'bar';

try {
42 is $foo & $bar;
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

var_dump($foo);
var_dump($bar);

?>
--EXPECT--
Here
int(42)
string(3) "bar"
8 changes: 8 additions & 0 deletions Zend/tests/pattern_matching/is/binding_in_or_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Must not bind to variables in | pattern
--FILE--
<?php
var_dump(42 is $foo|$bar);
?>
--EXPECTF--
Fatal error: Must not bind to variables inside | pattern in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/pattern_matching/is/binding_in_or_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Must not bind to variables in | pattern
--FILE--
<?php
var_dump(42 is 42|(43 & $bar));
?>
--EXPECTF--
Fatal error: Must not bind to variables inside | pattern in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/pattern_matching/is/bug_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Bug 001
--FILE--
<?php

function test() {
$x = 42;
43 is $x;
var_dump($x);
}

test();

?>
--EXPECT--
int(43)
44 changes: 44 additions & 0 deletions Zend/tests/pattern_matching/is/class_constant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Class constant pattern
--FILE--
<?php

class Foo {
const A = 'a';
private const B = 'b';

public static function test() {
var_dump('b' is self::B);
var_dump('c' is self::B);
}
}
enum Bar {
case A;
}

var_dump('a' is Foo::A);
var_dump('b' is Foo::A);
try {
var_dump('a' is Foo::B);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump('a' is Foo::C);
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
Foo::test();
var_dump(Bar::A is Bar::A);
var_dump('foo' is Bar::A);

?>
--EXPECT--
bool(true)
bool(false)
Cannot access private constant Foo::B
Undefined constant Foo::C
bool(true)
bool(false)
bool(true)
bool(false)
8 changes: 8 additions & 0 deletions Zend/tests/pattern_matching/is/compound_parentheses_1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Nested compound patterns require parentheses
--FILE--
<?php
var_dump(42 is Foo|Bar&Baz);
?>
--EXPECTF--
Fatal error: Nested compound pattern must be parenthesized in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/pattern_matching/is/compound_parentheses_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Nested compound patterns require parentheses
--FILE--
<?php
var_dump(42 is Foo&Bar|Baz);
?>
--EXPECTF--
Fatal error: Nested compound pattern must be parenthesized in %s on line %d
50 changes: 50 additions & 0 deletions Zend/tests/pattern_matching/is/delayed_binding.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
Delayed binding
--FILE--
<?php

class Pair {
public function __construct(public $a, public $b) {}
}

var_dump(new Pair(1, 2) is { a: $a, b: $b });
var_dump($a, $b);
unset($a, $b);

var_dump(new Pair(1, 2) is { a: $a, b: 3 });
var_dump($a, $b);
unset($a, $b);

var_dump(new Pair(new \stdClass(), 2) is { a: $a, b: 2 });
var_dump($a, $b);
unset($a, $b);

var_dump(new Pair(new \stdClass(), 2) is { a: $a, b: 3 });
var_dump($a, $b);
unset($a, $b);

?>
--EXPECTF--
bool(true)
int(1)
int(2)
bool(false)

Warning: Undefined variable $a in %s on line %d

Warning: Undefined variable $b in %s on line %d
NULL
NULL
bool(true)

Warning: Undefined variable $b in %s on line %d
object(stdClass)#2 (0) {
}
NULL
bool(false)

Warning: Undefined variable $a in %s on line %d

Warning: Undefined variable $b in %s on line %d
NULL
NULL
Loading
Loading