Skip to content
Closed
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
10 changes: 10 additions & 0 deletions Zend/tests/type_declarations/number/casting/number_cast_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--TEST--
Test that a number casting is not supported
--FILE--
<?php

$foo = (number) 12;

?>
--EXPECTF--
Parse error: syntax error, unexpected '12' (T_LNUMBER) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test that a number parameter type can't be narrowed
--FILE--
<?php

class Foo
{
public function method(number $a) {}
}

class Bar extends Foo
{
public function method(int $a) {}
}

?>
--EXPECTF--
Fatal error: Declaration of Bar::method(int $a) must be compatible with Foo::method(number $a) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test that a number parameter type supports invariance
--FILE--
<?php

class Foo
{
public function method(number $a) {}
}

class Bar extends Foo
{
public function method(number $a) {}
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test that a number parameter type supports invariance
--FILE--
<?php

class Foo
{
public function method(number $a) {}
}

class Bar extends Foo
{
public function method(int|float $a) {}
}

class Baz extends Foo
{
public function method(number $a) {}
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test that a narrower type can be overridden by the number type
--FILE--
<?php

class Foo
{
public function method(int $a) {}
}

class Bar extends Foo
{
public function method(number $a) {}
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Test that a parameter of a nullable int type can be overridden by the nullable number type
--FILE--
<?php

class Foo
{
public function method(?int $a) {}
}

class Bar extends Foo
{
public function method(?number $a) {}
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test that a property of number type can't be overridden by a property of any other type
--FILE--
<?php

class Foo
{
public number $property1;
}

class Bar extends Foo
{
public int $property1;
}

?>
--EXPECTF--
Fatal error: Type of Bar::$property1 must be number (as in class Foo) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test that a property of number type can't be overridden by a property of any other type type
--FILE--
<?php

class Foo
{
public number $property1;
}

class Bar extends Foo
{
public float $property1;
}

?>
--EXPECTF--
Fatal error: Type of Bar::$property1 must be number (as in class Foo) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test that a property of number type can't be overridden by a property of any other type type
--FILE--
<?php

class Foo
{
public number $property1;
}

class Bar extends Foo
{
public ?number $property1;
}

?>
--EXPECTF--
Fatal error: Type of Bar::$property1 must be number (as in class Foo) in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test that a number return type can't be widened
--FILE--
<?php

class Foo
{
public function method(): number {}
}

class Bar extends Foo
{
public function method(): int|float|null {}
}

?>
--EXPECTF--
Fatal error: Declaration of Bar::method(): int|float|null must be compatible with Foo::method(): number in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Test that a number return type can't be widened
--FILE--
<?php

class Foo
{
public function method(): number {}
}

class Bar extends Foo
{
public function method(): ?number {}
}

?>
--EXPECTF--
Fatal error: Declaration of Bar::method(): int|float|null must be compatible with Foo::method(): number in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test that a number return value supports invariance
--FILE--
<?php

class Foo
{
public function method(): number {}
}

class Bar extends Foo
{
public function method(): int|float {}
}

class Baz extends Bar
{
public function method(): number {}
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test that a number return type can be overridden by any subtype
--FILE--
<?php

class Foo
{
public function method(): number {}
}

class Bar extends Foo
{
public function method(): int {}
}

class Baz extends Foo
{
public function method(): float {}
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Test that the nullable number parameter type is valid
--FILE--
<?php

function foo(?number $a)
{
}

?>
--EXPECTF--
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test that the nullable number property type is valid
--FILE--
<?php

class Foo
{
public ?number $property1;
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Test that the nullable number return type is valid
--FILE--
<?php

function foo(): ?number
{
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test that the number parameter type can't be used together with any other numeric type
--FILE--
<?php

function foo(number|int $a)
{
}

?>
--EXPECTF--
Fatal error: Duplicate type int is redundant in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Test that number is a valid parameter type
--FILE--
<?php

function foo(number $a)
{
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test that the number return type can't be used together with any other numeric type
--FILE--
<?php

function foo(): number|float|null
{
}

?>
--EXPECTF--
Fatal error: Duplicate type float is redundant in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
Test that number is a valid return type
--FILE--
<?php

function foo(): number
{
}

?>
--EXPECT--
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Test that the number|void return type is not valid
--FILE--
<?php

function foo(): number|void
{
}

?>
--EXPECTF--
Fatal error: Void can only be used as a standalone type in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Test that the number parameter type doesn't accept non-numeric types in strict mode
--FILE--
<?php
declare(strict_types=1);

function foo(number $a)
{
}

try {
foo(null);
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
foo("0");
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
foo([]);
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
foo(new stdClass());
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

try {
foo(fopen(__FILE__, "r"));
} catch (TypeError $exception) {
echo $exception->getMessage() . "\n";
}

?>
--EXPECTF--
foo(): Argument #1 ($a) must be of type number, null given, called in %s on line %d
foo(): Argument #1 ($a) must be of type number, string given, called in %s on line %d
foo(): Argument #1 ($a) must be of type number, array given, called in %s on line %d
foo(): Argument #1 ($a) must be of type number, object given, called in %s on line %d
foo(): Argument #1 ($a) must be of type number, resource given, called in %s on line %d
Loading