PHP8_0.test 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. --TEST--
  2. PHP 8.0 test.
  3. --RULESET--
  4. {
  5. "@PHP80Migration": true,
  6. "@PHP80Migration:risky": true,
  7. "@PhpCsFixer": true,
  8. "@PhpCsFixer:risky": true,
  9. "phpdoc_to_return_type": true
  10. }
  11. --REQUIREMENTS--
  12. {"php": 80000}
  13. --EXPECT--
  14. <?php
  15. declare(strict_types=1);
  16. // https://wiki.php.net/rfc/named_params
  17. array_fill(start_index: 0, num: 100, value: 50);
  18. htmlspecialchars($string, double_encode: false);
  19. array_slice($array, $offset, $length, preserve_keys: true);
  20. new ParamNode($name, variadic: $isVariadic, byRef: $passByRef);
  21. array_foobar(array: $value); // reserved keyword as parameter name!
  22. // https://wiki.php.net/rfc/nullsafe_operator
  23. $country = $session?->user?->getAddress()?->country;
  24. // https://wiki.php.net/rfc/attributes_v2
  25. // https://wiki.php.net/rfc/attribute_amendments
  26. // https://wiki.php.net/rfc/shorter_attribute_syntax
  27. // https://wiki.php.net/rfc/shorter_attribute_syntax_change
  28. #[MyAttribute]
  29. #[MyExample\MyAttribute]
  30. #[MyAttribute(1234)]
  31. #[MyAttribute(value: 1234)]
  32. #[MyAttribute(MyAttribute::VALUE)]
  33. #[MyAttribute(['key' => 'value'])]
  34. #[MyAttribute(100 + 200)]
  35. #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
  36. #[MyAttribute(1234), MyAttribute(5678)]
  37. class PostsController
  38. {
  39. #[Route('/api/posts/{id}', methods: ['GET'])]
  40. public function get($id): void {}
  41. }
  42. $object = new #[ExampleAttribute] class {
  43. #[ExampleAttribute]
  44. public function foo(#[ExampleAttribute] $bar): void {}
  45. };
  46. // https://wiki.php.net/rfc/union_types_v2
  47. class Number
  48. {
  49. private null|float|int $number;
  50. public function setNumber(float|int $number): void
  51. {
  52. $this->number = $number;
  53. }
  54. public function getNumber(): null|float|int
  55. {
  56. return $this->number;
  57. }
  58. }
  59. // https://wiki.php.net/rfc/class_name_literal_on_object
  60. // nothing we can do
  61. // https://wiki.php.net/rfc/static_return_type
  62. class T
  63. {
  64. public function Foo(object $A): ?static {}
  65. public function something(): static {}
  66. }
  67. // https://wiki.php.net/rfc/variable_syntax_tweaks
  68. // nothing we can do
  69. // https://wiki.php.net/rfc/trailing_comma_in_parameter_list
  70. function foo(?string $param = null): void {}
  71. call_user_func('foo', 1);
  72. // https://wiki.php.net/rfc/trailing_comma_in_closure_use_list
  73. $foo1a = static function (): void {};
  74. $foo1b = static function (): void {};
  75. $foo2 = static function () use ($bar, $baz): void { echo $bar.$baz; };
  76. // https://wiki.php.net/rfc/mixed_type_v2
  77. class T_Mixed
  78. {
  79. public function Foo(mixed $a): mixed {}
  80. }
  81. // https://wiki.php.net/rfc/throw_expression
  82. if (1) {
  83. foo() ?? throw new Exception();
  84. $a = $condition && throw new Exception();
  85. $callable = static fn () => throw new Exception();
  86. $value = $falsableValue ?: throw new InvalidArgumentException();
  87. }
  88. // https://wiki.php.net/rfc/non-capturing_catches
  89. // TODO: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/5175 - when implementing new rule for fixing such cases, add exception variable in --INPUT-- section
  90. try {
  91. foo();
  92. } catch (Exception) {
  93. // ignore exception
  94. }
  95. // https://wiki.php.net/rfc/constructor_promotion
  96. class Point
  97. {
  98. public function __construct(
  99. public float $x = 0.0,
  100. protected float $y = 0.0,
  101. private float $z = 0.0,
  102. private ?string $desc = null,
  103. ) {}
  104. }
  105. // https://wiki.php.net/rfc/match_expression_v2
  106. echo match ($x) {
  107. 1, 2 => 'Same for 1 and 2',
  108. };
  109. // https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
  110. // no syntax changes
  111. // https://wiki.php.net/rfc/str_contains
  112. // no syntax changes
  113. // https://wiki.php.net/rfc/deprecate_curly_braces_array_access
  114. // syntax was deprecated, nothing we can do - a `normalize_index_brace` rule already exists and can be used on PHP <8 (or PHP 8 if errors are ignored)
  115. // https://wiki.php.net/rfc/concatenation_precedence
  116. // nothing we can do
  117. // https://wiki.php.net/rfc/inheritance_private_methods
  118. // nothing we can do
  119. // https://wiki.php.net/rfc/ternary_associativity
  120. // nothing we can do about the warning - a rule can be implemented to run on PHP <8 (or PHP 8 if errors are ignored)
  121. // https://wiki.php.net/rfc/stringable
  122. // nothing we can do
  123. // Date: mktime() and gmmktime() now require at least one argument. time() can be used to get the current timestamp.
  124. time();
  125. time();
  126. mktime($a);
  127. gmmktime(1, 2, 3, 4, 5, 6);
  128. // Exif: Removed read_exif_data(). exif_read_data() should be used instead.
  129. exif_read_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
  130. // The imap_header() function which is an alias of imap_headerinfo() has been removed.
  131. imap_headerinfo();
  132. // parse_str() can no longer be used without specifying a result array.
  133. // nothing we can do
  134. // Calling implode() with parameters in a reverse order ($pieces, $glue) is no longer supported.
  135. implode('', $pieces);
  136. implode('', $pieces);
  137. // Mbstring A number of deprecated mbregex aliases have been removed.
  138. // nothing we can do
  139. // Namespaced names can no longer contain whitespace (nor comments)
  140. // nothing we can do
  141. --INPUT--
  142. <?php
  143. // https://wiki.php.net/rfc/named_params
  144. array_fill(start_index: 0, num: 100, value: 50);
  145. htmlspecialchars($string, double_encode: false);
  146. array_slice($array, $offset, $length, preserve_keys: true);
  147. new ParamNode($name, variadic: $isVariadic, byRef: $passByRef);
  148. array_foobar(array: $value); // reserved keyword as parameter name!
  149. // https://wiki.php.net/rfc/nullsafe_operator
  150. $country = $session?->user?->getAddress()?->country;
  151. // https://wiki.php.net/rfc/attributes_v2
  152. // https://wiki.php.net/rfc/attribute_amendments
  153. // https://wiki.php.net/rfc/shorter_attribute_syntax
  154. // https://wiki.php.net/rfc/shorter_attribute_syntax_change
  155. #[MyAttribute]
  156. #[\MyExample\MyAttribute]
  157. #[MyAttribute(1234)]
  158. #[MyAttribute(value: 1234)]
  159. #[MyAttribute(MyAttribute::VALUE)]
  160. #[MyAttribute(array("key" => "value"))]
  161. #[MyAttribute(100 + 200)]
  162. #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_FUNCTION)]
  163. #[MyAttribute(1234), MyAttribute(5678)]
  164. class PostsController
  165. {
  166. #[Route("/api/posts/{id}", methods: ["GET"])]
  167. public function get($id): void
  168. {
  169. }
  170. }
  171. $object = new #[ExampleAttribute] class ()
  172. {
  173. #[ExampleAttribute]
  174. public function foo(#[ExampleAttribute] $bar): void
  175. {
  176. }
  177. };
  178. // https://wiki.php.net/rfc/union_types_v2
  179. class Number
  180. {
  181. private int|float|null $number;
  182. public function setNumber(int | float $number): void
  183. {
  184. $this->number = $number;
  185. }
  186. public function getNumber(): int|float | null
  187. {
  188. return $this->number;
  189. }
  190. }
  191. // https://wiki.php.net/rfc/class_name_literal_on_object
  192. // nothing we can do
  193. // https://wiki.php.net/rfc/static_return_type
  194. class T
  195. {
  196. public function Foo(object $A): ?StatiC
  197. {
  198. }
  199. /** @return static */
  200. public function something()
  201. {
  202. }
  203. }
  204. // https://wiki.php.net/rfc/variable_syntax_tweaks
  205. // nothing we can do
  206. // https://wiki.php.net/rfc/trailing_comma_in_parameter_list
  207. function foo(string $param = null, ) {}
  208. call_user_func('foo', 1, );
  209. // https://wiki.php.net/rfc/trailing_comma_in_closure_use_list
  210. $foo1a = function() use ($bar,) {};
  211. $foo1b = function() use ($bar, ) {};
  212. $foo2 = function() use ($bar,$baz,) { echo $bar.$baz; };
  213. // https://wiki.php.net/rfc/mixed_type_v2
  214. class T_Mixed
  215. {
  216. public function Foo(Mixed$a): MIXED
  217. {
  218. }
  219. }
  220. // https://wiki.php.net/rfc/throw_expression
  221. if (1) {
  222. foo() ?? throw new \Exception();
  223. $a = $condition && throw new Exception();
  224. $callable = fn() => throw new Exception();
  225. $value = $falsableValue ?: throw new InvalidArgumentException();
  226. }
  227. // https://wiki.php.net/rfc/non-capturing_catches
  228. // TODO: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/5175 - when implementing new rule for fixing such cases, add exception variable in --INPUT-- section
  229. try {
  230. foo();
  231. } catch (Exception) {
  232. // ignore exception
  233. }
  234. // https://wiki.php.net/rfc/constructor_promotion
  235. class Point
  236. {
  237. public function __construct(
  238. PUBLIC float $x = 0.0,
  239. Protected float $y = 0.0,
  240. privatE float $z = 0.0,
  241. private ?string $desc = null,
  242. ) {
  243. }
  244. }
  245. // https://wiki.php.net/rfc/match_expression_v2
  246. echo match($x) {
  247. 1, 2 => 'Same for 1 and 2',
  248. };
  249. // https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions
  250. // no syntax changes
  251. // https://wiki.php.net/rfc/str_contains
  252. // no syntax changes
  253. // https://wiki.php.net/rfc/deprecate_curly_braces_array_access
  254. // syntax was deprecated, nothing we can do - a `normalize_index_brace` rule already exists and can be used on PHP <8 (or PHP 8 if errors are ignored)
  255. // https://wiki.php.net/rfc/concatenation_precedence
  256. // nothing we can do
  257. // https://wiki.php.net/rfc/inheritance_private_methods
  258. // nothing we can do
  259. // https://wiki.php.net/rfc/ternary_associativity
  260. // nothing we can do about the warning - a rule can be implemented to run on PHP <8 (or PHP 8 if errors are ignored)
  261. // https://wiki.php.net/rfc/stringable
  262. // nothing we can do
  263. // Date: mktime() and gmmktime() now require at least one argument. time() can be used to get the current timestamp.
  264. mktime();
  265. gmmktime();
  266. mktime($a);
  267. gmmktime(1, 2, 3, 4, 5, 6);
  268. // Exif: Removed read_exif_data(). exif_read_data() should be used instead.
  269. read_exif_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
  270. // The imap_header() function which is an alias of imap_headerinfo() has been removed.
  271. imap_header();
  272. // parse_str() can no longer be used without specifying a result array.
  273. // nothing we can do
  274. // Calling implode() with parameters in a reverse order ($pieces, $glue) is no longer supported.
  275. implode($pieces, '');
  276. implode($pieces);
  277. // Mbstring A number of deprecated mbregex aliases have been removed.
  278. // nothing we can do
  279. // Namespaced names can no longer contain whitespace (nor comments)
  280. // nothing we can do