NamedArgumentTransformerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Tokenizer\Transformer;
  13. use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
  14. use PhpCsFixer\Tokenizer\CT;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Tokenizer\Transformer\NamedArgumentTransformer
  20. *
  21. * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
  22. */
  23. final class NamedArgumentTransformerTest extends AbstractTransformerTestCase
  24. {
  25. /**
  26. * @param _TransformerTestExpectedTokens $expectedTokens
  27. *
  28. * @dataProvider provideProcessCases
  29. *
  30. * @requires PHP 8.0
  31. */
  32. public function testProcess(string $source, array $expectedTokens): void
  33. {
  34. $this->doTest($source, $expectedTokens);
  35. }
  36. public static function provideProcessCases(): iterable
  37. {
  38. yield 'function call' => [
  39. '<?php foo(test: 1);',
  40. [
  41. 3 => CT::T_NAMED_ARGUMENT_NAME,
  42. 4 => CT::T_NAMED_ARGUMENT_COLON,
  43. ],
  44. ];
  45. yield 'dynamic function 2x' => [
  46. '<?php $foo(foo: 1, bar: 2, 3,);',
  47. [
  48. 3 => CT::T_NAMED_ARGUMENT_NAME,
  49. 4 => CT::T_NAMED_ARGUMENT_COLON,
  50. 9 => CT::T_NAMED_ARGUMENT_NAME,
  51. 10 => CT::T_NAMED_ARGUMENT_COLON,
  52. ],
  53. ];
  54. yield 'method' => [
  55. '<?php
  56. class Bar {
  57. public function a($foo){}
  58. }
  59. $foo = new Bar();
  60. $foo->a(foo: 1);
  61. ',
  62. [
  63. 36 => CT::T_NAMED_ARGUMENT_NAME,
  64. 37 => CT::T_NAMED_ARGUMENT_COLON,
  65. ],
  66. ];
  67. yield 'nested' => [
  68. '<?php
  69. foo(test: static function() {
  70. bar(test: 1);
  71. },);
  72. ',
  73. [
  74. 4 => CT::T_NAMED_ARGUMENT_NAME,
  75. 5 => CT::T_NAMED_ARGUMENT_COLON,
  76. 17 => CT::T_NAMED_ARGUMENT_NAME,
  77. 18 => CT::T_NAMED_ARGUMENT_COLON,
  78. ],
  79. ];
  80. }
  81. /**
  82. * @dataProvider provideDoNotChangeCases
  83. */
  84. public function testDoNotChange(string $source): void
  85. {
  86. self::assertNotChange($source);
  87. }
  88. /**
  89. * @return iterable<string, array{string}>
  90. */
  91. public static function provideDoNotChangeCases(): iterable
  92. {
  93. yield 'switch/case/constants' => [
  94. '<?php
  95. define(\'FOO\', 123);
  96. define(\'BAR\', 123);
  97. $a = $guard = 123;
  98. switch($a) {
  99. case FOO:
  100. echo 456;
  101. break;
  102. case 3 + FOO:
  103. echo 789;
  104. break;
  105. case ($guard ? BAR : 2):
  106. echo 456;
  107. break;
  108. }
  109. foo(1 , $a3 ? BAR : 2);
  110. $a1 = [1, BAR ? 1 : 2];
  111. $a2 = [1, (BAR) ? 1 : 2];
  112. ',
  113. ];
  114. yield 'goto' => [
  115. '<?php
  116. define(\'FOO\', 123);
  117. $guard = 1;
  118. {
  119. beginning:
  120. echo $guard ? 1 + FOO : 2;
  121. echo $guard ? 1 : 2;
  122. }
  123. ',
  124. ];
  125. yield 'return type' => ['<?php function foo(): array { return []; }'];
  126. }
  127. private static function assertNotChange(string $source): void
  128. {
  129. Tokens::clearCache();
  130. foreach (Tokens::fromCode($source) as $token) {
  131. self::assertFalse($token->isGivenKind([
  132. CT::T_NAMED_ARGUMENT_NAME,
  133. CT::T_NAMED_ARGUMENT_COLON,
  134. ]));
  135. }
  136. }
  137. }