1--TEST--
2Strict scalar type basics
3--FILE--
4<?php
5
6declare(strict_types=1);
7
8$errnames = [
9    E_NOTICE => 'E_NOTICE',
10    E_WARNING => 'E_WARNING',
11    E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR'
12];
13
14set_error_handler(function (int $errno, string $errmsg, string $file, int $line) use ($errnames) {
15    echo "$errnames[$errno]: $errmsg on line $line\n";
16    return true;
17});
18
19$functions = [
20    'int' => function (int $i) { return $i; },
21    'float' => function (float $f) { return $f; },
22    'string' => function (string $s) { return $s; },
23    'bool' => function (bool $b) { return $b; }
24];
25
26$values = [
27    1,
28    1.0,
29    "1",
30    TRUE,
31    FALSE,
32    NULL,
33    [],
34    new StdClass,
35    fopen("data:text/plain,foobar", "r")
36];
37
38function type($value) {
39    if (is_float($value)) {
40        return "float";
41    } else if (is_bool($value)) {
42        return $value ? "true" : "false";
43    } else if (is_null($value)) {
44        return "null";
45    } else {
46        return gettype($value);
47    }
48}
49
50foreach ($functions as $type => $function) {
51    echo PHP_EOL, "Testing '$type' type:", PHP_EOL;
52    foreach ($values as $value) {
53        $errored = false;
54        echo PHP_EOL . "*** Trying ", type($value), " value", PHP_EOL;
55        try {
56            var_dump($function($value));
57        } catch (TypeError $e) {
58            echo "*** Caught " . $e->getMessage() . PHP_EOL;
59        }
60    }
61}
62echo PHP_EOL . "Done";
63?>
64--EXPECTF--
65Testing 'int' type:
66
67*** Trying integer value
68int(1)
69
70*** Trying float value
71*** Caught Argument 1 passed to {closure}() must be of the type integer, float given, called in %s on line %d
72
73*** Trying string value
74*** Caught Argument 1 passed to {closure}() must be of the type integer, string given, called in %s on line %d
75
76*** Trying true value
77*** Caught Argument 1 passed to {closure}() must be of the type integer, boolean given, called in %s on line %d
78
79*** Trying false value
80*** Caught Argument 1 passed to {closure}() must be of the type integer, boolean given, called in %s on line %d
81
82*** Trying null value
83*** Caught Argument 1 passed to {closure}() must be of the type integer, null given, called in %s on line %d
84
85*** Trying array value
86*** Caught Argument 1 passed to {closure}() must be of the type integer, array given, called in %s on line %d
87
88*** Trying object value
89*** Caught Argument 1 passed to {closure}() must be of the type integer, object given, called in %s on line %d
90
91*** Trying resource value
92*** Caught Argument 1 passed to {closure}() must be of the type integer, resource given, called in %s on line %d
93
94Testing 'float' type:
95
96*** Trying integer value
97float(1)
98
99*** Trying float value
100float(1)
101
102*** Trying string value
103*** Caught Argument 1 passed to {closure}() must be of the type float, string given, called in %s on line %d
104
105*** Trying true value
106*** Caught Argument 1 passed to {closure}() must be of the type float, boolean given, called in %s on line %d
107
108*** Trying false value
109*** Caught Argument 1 passed to {closure}() must be of the type float, boolean given, called in %s on line %d
110
111*** Trying null value
112*** Caught Argument 1 passed to {closure}() must be of the type float, null given, called in %s on line %d
113
114*** Trying array value
115*** Caught Argument 1 passed to {closure}() must be of the type float, array given, called in %s on line %d
116
117*** Trying object value
118*** Caught Argument 1 passed to {closure}() must be of the type float, object given, called in %s on line %d
119
120*** Trying resource value
121*** Caught Argument 1 passed to {closure}() must be of the type float, resource given, called in %s on line %d
122
123Testing 'string' type:
124
125*** Trying integer value
126*** Caught Argument 1 passed to {closure}() must be of the type string, integer given, called in %s on line %d
127
128*** Trying float value
129*** Caught Argument 1 passed to {closure}() must be of the type string, float given, called in %s on line %d
130
131*** Trying string value
132string(1) "1"
133
134*** Trying true value
135*** Caught Argument 1 passed to {closure}() must be of the type string, boolean given, called in %s on line %d
136
137*** Trying false value
138*** Caught Argument 1 passed to {closure}() must be of the type string, boolean given, called in %s on line %d
139
140*** Trying null value
141*** Caught Argument 1 passed to {closure}() must be of the type string, null given, called in %s on line %d
142
143*** Trying array value
144*** Caught Argument 1 passed to {closure}() must be of the type string, array given, called in %s on line %d
145
146*** Trying object value
147*** Caught Argument 1 passed to {closure}() must be of the type string, object given, called in %s on line %d
148
149*** Trying resource value
150*** Caught Argument 1 passed to {closure}() must be of the type string, resource given, called in %s on line %d
151
152Testing 'bool' type:
153
154*** Trying integer value
155*** Caught Argument 1 passed to {closure}() must be of the type boolean, integer given, called in %s on line %d
156
157*** Trying float value
158*** Caught Argument 1 passed to {closure}() must be of the type boolean, float given, called in %s on line %d
159
160*** Trying string value
161*** Caught Argument 1 passed to {closure}() must be of the type boolean, string given, called in %s on line %d
162
163*** Trying true value
164bool(true)
165
166*** Trying false value
167bool(false)
168
169*** Trying null value
170*** Caught Argument 1 passed to {closure}() must be of the type boolean, null given, called in %s on line %d
171
172*** Trying array value
173*** Caught Argument 1 passed to {closure}() must be of the type boolean, array given, called in %s on line %d
174
175*** Trying object value
176*** Caught Argument 1 passed to {closure}() must be of the type boolean, object given, called in %s on line %d
177
178*** Trying resource value
179*** Caught Argument 1 passed to {closure}() must be of the type boolean, resource given, called in %s on line %d
180
181Done
182