Skip to content

Extend @ to suppress Exceptions #9

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

Open
wants to merge 13 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
20 changes: 20 additions & 0 deletions Zend/tests/silence_operator/constructor_throwing.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Silencing a new object instance
--FILE--
<?php

class A {
public function __construct() {
throw new Exception();
}
}

$var = @new A();

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Diagnostics should still be emitted if a class list is passed to @, userland
--FILE--
<?php

function test() {
trigger_error('Diagnostic message', E_USER_NOTICE);
return true;
}

$var = @<Exception>test();

var_dump($var);

echo "Done\n";
?>
--EXPECTF--
Notice: Diagnostic message in %s on line %d
bool(true)
Done
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Diagnostics should still be emitted if a class list is passed to @, internal
--FILE--
<?php

function test() {
$r = $a + 1;
return $r;
}

$var = @<Exception>test();

var_dump($var);

echo "Done\n";
?>
--EXPECTF--
Warning: Undefined variable $a in %s on line %d
int(1)
Done
18 changes: 18 additions & 0 deletions Zend/tests/silence_operator/empty_exception_list.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Suppression operator ParseError with empty class list
--FILE--
<?php

function test1() {
throw new Exception();
return true;
}

$var = @<>test1();

var_dump($var);

echo "Done\n";
?>
--EXPECTF--
Parse error: syntax error, unexpected token ">" in %s on line 8
19 changes: 19 additions & 0 deletions Zend/tests/silence_operator/exception_in_function_call.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Silence exception with @ operator
--FILE--
<?php

function test1() {
throw new Exception();
return true;
}

$var = @test1();

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
18 changes: 18 additions & 0 deletions Zend/tests/silence_operator/foreach.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Use of @ before foreach value to iterator upon
--FILE--
<?php

function foo() {
throw new Exception();
}

foreach (@foo() as $val) {
var_dump($val);
}

echo "Done\n";
?>
--EXPECTF--
Warning: foreach() argument must be of type array|object, null given in %s on line %d
Done
18 changes: 18 additions & 0 deletions Zend/tests/silence_operator/in_try_catch_diagnostic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Use of @ in try catch must not fail when a diagnostic is emitted
--FILE--
<?php

try {
$var = @$non_existent;
} catch (\Throwable $e) {
echo $e::class, \PHP_EOL;
}

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
23 changes: 23 additions & 0 deletions Zend/tests/silence_operator/in_try_catch_exception.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Use of @ in try catch must not fail when an exception is thrown
--FILE--
<?php

function test1() {
throw new Exception();
return true;
}

try {
$var = @test1();
} catch (\Throwable $e) {
echo $e::class, \PHP_EOL;
}

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
12 changes: 12 additions & 0 deletions Zend/tests/silence_operator/internal_function_arg_diagnostic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Silencing an internal function argument emitting a diagnostic
--FILE--
<?php

var_dump(@$non_existent);

echo "Done\n";
?>
--EXPECT--
NULL
Done
17 changes: 17 additions & 0 deletions Zend/tests/silence_operator/internal_function_arg_exception.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Silencing an internal function argument throwing an Exception
--FILE--
<?php

function foo() {
throw new Exception();
return 1;
}

var_dump(@foo());

echo "Done\n";
?>
--EXPECT--
NULL
Done
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Silencing an internal function supplementary argument throwing an Exception
--FILE--
<?php

function foo() {
throw new Exception();
return 1;
}

$var = error_get_last(@foo());

var_dump($var);

echo "Done\n";
?>
--EXPECTF--
Fatal error: Uncaught ArgumentCountError: error_get_last() expects exactly 0 arguments, 1 given in %s:%d
Stack trace:
#0 %s(%d): error_get_last(NULL)
#1 {main}
thrown in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Listed silencing a internal function supplementary argument throwing an Exception
--FILE--
<?php

function foo() {
throw new Exception();
return 1;
}

$var = error_get_last(@<Exception>foo());

var_dump($var);

echo "Done\n";
?>
--EXPECTF--
Fatal error: Uncaught ArgumentCountError: error_get_last() expects exactly 0 arguments, 1 given in %s:%d
Stack trace:
#0 %s(%d): error_get_last(NULL)
#1 {main}
thrown in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Listing Error in silence throwable class list should suppress
--FILE--
<?php

function test1() {
throw new Error();
return true;
}

$var = @<Error>test1();

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
19 changes: 19 additions & 0 deletions Zend/tests/silence_operator/listed_exceptions_in.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Suppression operator with class list, single
--FILE--
<?php

function test1() {
throw new Exception();
return true;
}

$var = @<Exception>test1();

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
22 changes: 22 additions & 0 deletions Zend/tests/silence_operator/listed_exceptions_not_in.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Suppression operator should only suppress listed Exceptions
--FILE--
<?php

function test1() {
throw new Error();
return true;
}

$var = @<Exception>test1();

var_dump($var);

echo "Done\n";
?>
--EXPECTF--
Fatal error: Uncaught Error in %s:4
Stack trace:
#0 %s(8): test1()
#1 {main}
thrown in %s on line 4
25 changes: 25 additions & 0 deletions Zend/tests/silence_operator/nested_silence_in_call.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
Nested at in call must not fail
--FILE--
<?php

function foo($arg) {
}

function bar() {
throw new Exception("test");
}

try {
$var = @foo(@bar());
} catch (\Throwable $e) {
echo $e::class, \PHP_EOL;
}

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
22 changes: 22 additions & 0 deletions Zend/tests/silence_operator/silence_function_many_ops.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Silence function with ops before exception with @ operator
--FILE--
<?php

function test1() {
$b = strlen("Hello");
$a = 5;
throw new Exception();
$a += 25;
return true;
}

$var = @test1();

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
24 changes: 24 additions & 0 deletions Zend/tests/silence_operator/silence_function_nested.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Silence nested function call which throws with @ operator
--FILE--
<?php

function test1() {
throw new Exception();
return true;
}

function test2($a) {
$a += 6;
return test1();
}

$var = @test2(1);

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
14 changes: 14 additions & 0 deletions Zend/tests/silence_operator/silence_multiple_use.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Multiple use of @ operator must not fail to suppress diagnostic
--FILE--
<?php

$var = @@$non_existent;

var_dump($var);

echo "Done\n";
?>
--EXPECT--
NULL
Done
Loading