LabelContextPassTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Test\CodeCleaner;
  11. use Psy\CodeCleaner\LabelContextPass;
  12. /**
  13. * @group isolation-fail
  14. */
  15. class LabelContextPassTest extends CodeCleanerTestCase
  16. {
  17. /**
  18. * @before
  19. */
  20. public function getReady()
  21. {
  22. $this->setPass(new LabelContextPass());
  23. }
  24. /**
  25. * @dataProvider validStatements
  26. */
  27. public function testProcessStatementPasses($code)
  28. {
  29. $this->parseAndTraverse($code);
  30. $this->assertTrue(true);
  31. }
  32. public function validStatements()
  33. {
  34. return [
  35. ['function foo() { foo: "echo"; goto foo; }'],
  36. ['function foo() { "echo"; goto foo; }'],
  37. ['begin: foreach (range(1, 5) as $i) { goto end; } end: goto begin;'],
  38. ['bar: if (true) goto bar;'],
  39. // False negative
  40. // PHP Fatal error: 'goto' into loop or switch statement is disallowed
  41. 'false negative1' => ['while (true) { label: "error"; } goto label;'],
  42. // PHP Fatal error: 'goto' to undefined label 'none'
  43. 'false negative2' => ['$f = function () { goto none; };'],
  44. ];
  45. }
  46. /**
  47. * @dataProvider invalidStatements
  48. */
  49. public function testInvalid($code)
  50. {
  51. $this->expectException(\Psy\Exception\FatalErrorException::class);
  52. $this->parseAndTraverse($code);
  53. $this->fail();
  54. }
  55. public function invalidStatements()
  56. {
  57. return [
  58. ['goto bar;'],
  59. ['if (true) goto bar;'],
  60. ['buz: if (true) goto bar;'],
  61. ];
  62. }
  63. /**
  64. * @dataProvider unreachableLabelStatements
  65. */
  66. public function testUnreachedLabel($code)
  67. {
  68. $this->parseAndTraverse($code);
  69. $this->assertTrue(true);
  70. }
  71. public function unreachableLabelStatements()
  72. {
  73. return [
  74. ['buz:'],
  75. ['foo: buz: goto foo;'],
  76. ['foo: buz: goto buz;'],
  77. ];
  78. }
  79. }