CallTimePassByReferencePassTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2017 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 PhpParser\NodeTraverser;
  12. use Psy\CodeCleaner\CallTimePassByReferencePass;
  13. class CallTimePassByReferencePassTest extends CodeCleanerTestCase
  14. {
  15. public function setUp()
  16. {
  17. $this->pass = new CallTimePassByReferencePass();
  18. $this->traverser = new NodeTraverser();
  19. $this->traverser->addVisitor($this->pass);
  20. }
  21. /**
  22. * @dataProvider invalidStatements
  23. * @expectedException \Psy\Exception\FatalErrorException
  24. */
  25. public function testProcessStatementFails($code)
  26. {
  27. if (version_compare(PHP_VERSION, '5.4', '<')) {
  28. $this->markTestSkipped();
  29. }
  30. $stmts = $this->parse($code);
  31. $this->traverser->traverse($stmts);
  32. }
  33. public function invalidStatements()
  34. {
  35. return array(
  36. array('f(&$arg)'),
  37. array('$object->method($first, &$arg)'),
  38. array('$closure($first, &$arg, $last)'),
  39. array('A::b(&$arg)'),
  40. );
  41. }
  42. /**
  43. * @dataProvider validStatements
  44. */
  45. public function testProcessStatementPasses($code)
  46. {
  47. $stmts = $this->parse($code);
  48. $this->traverser->traverse($stmts);
  49. }
  50. public function validStatements()
  51. {
  52. $data = array(
  53. array('array(&$var)'),
  54. array('$a = &$b'),
  55. array('f(array(&$b))'),
  56. );
  57. if (version_compare(PHP_VERSION, '5.4', '<')) {
  58. $data = array_merge($data, $this->invalidStatements());
  59. }
  60. return $data;
  61. }
  62. }