ReferenceAnalyzerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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\Analyzer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Analyzer\ReferenceAnalyzer;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @author Kuba Werłos <werlos@gmail.com>
  18. *
  19. * @covers \PhpCsFixer\Tokenizer\Analyzer\ReferenceAnalyzer
  20. *
  21. * @internal
  22. */
  23. final class ReferenceAnalyzerTest extends TestCase
  24. {
  25. public function testNonAmpersand(): void
  26. {
  27. $analyzer = new ReferenceAnalyzer();
  28. self::assertFalse($analyzer->isReference(Tokens::fromCode('<?php $foo;$bar;$baz;'), 3));
  29. }
  30. public function testReferenceAndNonReferenceTogether(): void
  31. {
  32. $analyzer = new ReferenceAnalyzer();
  33. $tokens = Tokens::fromCode('<?php function foo(&$bar = BAZ & QUX) {};');
  34. self::assertTrue($analyzer->isReference($tokens, 5));
  35. self::assertFalse($analyzer->isReference($tokens, 12));
  36. }
  37. /**
  38. * @dataProvider provideReferenceCases
  39. */
  40. public function testReference(string $code): void
  41. {
  42. $this->doTestCode(true, $code);
  43. }
  44. /**
  45. * @return iterable<array{string}>
  46. */
  47. public static function provideReferenceCases(): iterable
  48. {
  49. yield ['<?php $foo =& $bar;'];
  50. yield ['<?php $foo =& find_var($bar);'];
  51. yield ['<?php $foo["bar"] =& $baz;'];
  52. yield ['<?php function foo(&$bar) {};'];
  53. yield ['<?php function foo($bar, &$baz) {};'];
  54. yield ['<?php function &() {};'];
  55. yield ['<?php
  56. class Foo {
  57. public $value = 42;
  58. public function &getValue() {
  59. return $this->value;
  60. }
  61. }'];
  62. yield ['<?php function foo(\Bar\Baz &$qux) {};'];
  63. yield ['<?php function foo(array &$bar) {};'];
  64. yield ['<?php function foo(callable &$bar) {};'];
  65. yield ['<?php function foo(int &$bar) {};'];
  66. yield ['<?php function foo(string &$bar) {};'];
  67. yield ['<?php foreach($foos as &$foo) {}'];
  68. yield ['<?php foreach($foos as $key => &$foo) {}'];
  69. yield ['<?php function foo(?int &$bar) {};'];
  70. }
  71. /**
  72. * @dataProvider provideNonReferenceCases
  73. */
  74. public function testNonReference(string $code): void
  75. {
  76. $this->doTestCode(false, $code);
  77. }
  78. /**
  79. * @return iterable<array{string}>
  80. */
  81. public static function provideNonReferenceCases(): iterable
  82. {
  83. yield ['<?php $foo & $bar;'];
  84. yield ['<?php FOO & $bar;'];
  85. yield ['<?php Foo::BAR & $baz;'];
  86. yield ['<?php foo(1, 2) & $bar;'];
  87. yield ['<?php foo($bar & $baz);'];
  88. yield ['<?php foo($bar, $baz & $qux);'];
  89. yield ['<?php foo($bar->baz & $qux);'];
  90. yield ['<?php foo(Bar::BAZ & $qux);'];
  91. yield ['<?php foo(Bar\Baz::qux & $quux);'];
  92. yield ['<?php foo(\Bar\Baz::qux & $quux);'];
  93. yield ['<?php foo($bar["mode"] & $baz);'];
  94. yield ['<?php foo(0b11111111 & $bar);'];
  95. yield ['<?php foo(127 & $bar);'];
  96. yield ['<?php foo("bar" & $baz);'];
  97. yield ['<?php foo($bar = BAZ & $qux);'];
  98. yield ['<?php function foo($bar = BAZ & QUX) {};'];
  99. yield ['<?php function foo($bar = BAZ::QUX & QUUX) {};'];
  100. yield ['<?php function foo(array $bar = BAZ & QUX) {};'];
  101. yield ['<?php function foo(callable $bar = BAZ & QUX) {};'];
  102. yield ['<?php foreach($foos as $foo) { $foo & $bar; }'];
  103. yield ['<?php if ($foo instanceof Bar & 0b01010101) {}'];
  104. yield ['<?php function foo(?int $bar = BAZ & QUX) {};'];
  105. }
  106. /**
  107. * @dataProvider provideNonReferencePre84Cases
  108. *
  109. * @requires PHP <8.4
  110. */
  111. public function testNonReferencePre84(string $code): void
  112. {
  113. $this->doTestCode(false, $code);
  114. }
  115. /**
  116. * @return iterable<array{string}>
  117. */
  118. public static function provideNonReferencePre84Cases(): iterable
  119. {
  120. yield ['<?php foo($bar{"mode"} & $baz);'];
  121. }
  122. private function doTestCode(bool $expected, string $code): void
  123. {
  124. $analyzer = new ReferenceAnalyzer();
  125. $tokens = Tokens::fromCode($code);
  126. foreach ($tokens as $index => $token) {
  127. if ('&' === $token->getContent()) {
  128. self::assertSame($expected, $analyzer->isReference($tokens, $index));
  129. }
  130. }
  131. }
  132. }