ConstructorPromotionTransformerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\ConstructorPromotionTransformer
  20. *
  21. * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
  22. */
  23. final class ConstructorPromotionTransformerTest extends AbstractTransformerTestCase
  24. {
  25. /**
  26. * @param _TransformerTestExpectedTokens $expectedTokens
  27. *
  28. * @dataProvider provideProcessCases
  29. *
  30. * @requires PHP 8.0
  31. */
  32. public function testProcess(array $expectedTokens, string $source): void
  33. {
  34. $this->doTest(
  35. $source,
  36. $expectedTokens,
  37. [
  38. CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  39. CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
  40. CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
  41. ]
  42. );
  43. }
  44. public static function provideProcessCases(): iterable
  45. {
  46. yield [
  47. [
  48. 14 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  49. 25 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
  50. 36 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
  51. ],
  52. '<?php
  53. class Point {
  54. public function __construct(
  55. public float $x = 0.0,
  56. protected float $y = 0.0,
  57. private float $z = 0.0,
  58. ) {}
  59. }
  60. ',
  61. ];
  62. yield [
  63. [
  64. 16 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  65. 22 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
  66. 28 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
  67. ],
  68. '<?php $a = new class {function/* 1 */__CONSTRUCT/* 2 */(/* 3 */public float $x,protected float $y,private float $z) {}};',
  69. ];
  70. }
  71. public function testNotChange(): void
  72. {
  73. $code = '<?php
  74. // class Foo1 {
  75. // function __construct(
  76. // private float $z = new class {
  77. // public function __construct() {}
  78. // }
  79. // ) {}
  80. // }
  81. // class Foo2 {
  82. // function __construct(
  83. // private array $z = [new class {}],
  84. // ) {}
  85. // }
  86. // class Foo3 {
  87. // public function __construct(
  88. // public float $x = 0.0,
  89. // protected float $y = 0.0,
  90. // private float $z = 0.0,
  91. // ) {}
  92. // }
  93. function __construct(/* public */ $foo){}
  94. class Foo4 {
  95. public function construct(/* public */ $foo)
  96. {}
  97. }
  98. class Foo5 {
  99. public $foo1;
  100. protected $foo2;
  101. private $foo3;
  102. public function __construct(/* public */ $foo){} public $foo4;
  103. }
  104. ';
  105. Tokens::clearCache();
  106. foreach (Tokens::fromCode($code) as $token) {
  107. self::assertFalse($token->isGivenKind([
  108. CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  109. CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED,
  110. CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
  111. ]));
  112. }
  113. }
  114. /**
  115. * @param _TransformerTestExpectedTokens $expectedTokens
  116. *
  117. * @dataProvider provideFix81Cases
  118. *
  119. * @requires PHP 8.1
  120. */
  121. public function testFix81(array $expectedTokens, string $source): void
  122. {
  123. $this->doTest(
  124. $source,
  125. $expectedTokens,
  126. [
  127. CT::T_TYPE_ALTERNATION,
  128. ]
  129. );
  130. }
  131. public static function provideFix81Cases(): iterable
  132. {
  133. yield 'readonly' => [
  134. [
  135. 14 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  136. 23 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE,
  137. 36 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  138. 52 => CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC,
  139. ],
  140. '<?php
  141. class Test {
  142. public function __construct(
  143. public readonly float $f,
  144. private readonly int $i = 0,
  145. public readonly array $ary = [],
  146. readonly public array $bar = [],
  147. ) {}
  148. }',
  149. ];
  150. }
  151. }