FirstClassCallableTransformerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Tokenizer\Transformer\FirstClassCallableTransformer
  19. *
  20. * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
  21. */
  22. final class FirstClassCallableTransformerTest extends AbstractTransformerTestCase
  23. {
  24. /**
  25. * @param _TransformerTestExpectedTokens $expectedTokens
  26. *
  27. * @dataProvider provideProcessCases
  28. *
  29. * @requires PHP 8.1
  30. */
  31. public function testProcess(array $expectedTokens, string $source): void
  32. {
  33. $this->doTest($source, $expectedTokens, [CT::T_FIRST_CLASS_CALLABLE]);
  34. }
  35. public static function provideProcessCases(): iterable
  36. {
  37. yield 'set' => [
  38. [
  39. 3 => CT::T_FIRST_CLASS_CALLABLE,
  40. 9 => CT::T_FIRST_CLASS_CALLABLE,
  41. 15 => CT::T_FIRST_CLASS_CALLABLE,
  42. 23 => CT::T_FIRST_CLASS_CALLABLE,
  43. 31 => CT::T_FIRST_CLASS_CALLABLE,
  44. 41 => CT::T_FIRST_CLASS_CALLABLE,
  45. 49 => CT::T_FIRST_CLASS_CALLABLE,
  46. 57 => CT::T_FIRST_CLASS_CALLABLE,
  47. 71 => CT::T_FIRST_CLASS_CALLABLE,
  48. 77 => CT::T_FIRST_CLASS_CALLABLE,
  49. 88 => CT::T_FIRST_CLASS_CALLABLE,
  50. 101 => CT::T_FIRST_CLASS_CALLABLE,
  51. ],
  52. '<?php
  53. strlen(...);
  54. $closure(...);
  55. $invokableObject(...);
  56. $obj->method(...);
  57. $obj->$methodStr(...);
  58. ($obj->property)(...);
  59. Foo::method(...);
  60. $classStr::$methodStr(...);
  61. self::{$complex . $expression}(...);
  62. \'strlen\'(...);
  63. [$obj, \'method\'](...);
  64. [Foo::class, \'method\'](...) ?>',
  65. ];
  66. yield 'comments and spacing' => [
  67. [
  68. 4 => CT::T_FIRST_CLASS_CALLABLE,
  69. 12 => CT::T_FIRST_CLASS_CALLABLE,
  70. 18 => CT::T_FIRST_CLASS_CALLABLE,
  71. 28 => CT::T_FIRST_CLASS_CALLABLE,
  72. 40 => CT::T_FIRST_CLASS_CALLABLE,
  73. ],
  74. '<?php
  75. strlen(/* */.../* */);
  76. $closure( ...);
  77. $invokableObject(... );
  78. $obj->method( ... );
  79. $obj->$methodStr( /* */ ... /* */ );
  80. ',
  81. ];
  82. yield 'not cases' => [
  83. [],
  84. '<?php
  85. array_unshift($types, ...$nulls);
  86. array_unshift(...$nulls, 1);
  87. foo(...$args, named: $arg);
  88. foo(named: $arg, ...$args);
  89. foo(...$nulls);
  90. $fruits = ["banana", "orange", ...$parts, "watermelon"];
  91. $a = [...$array1, ...$array2];
  92. ',
  93. ];
  94. }
  95. }