/simplesamlphp/vendor/squizlabs/php_codesniffer/tests/Core/Tokenizer/DoubleArrowTest.php

https://github.com/UMMS-Biocore/dolphinnext · PHP · 237 lines · 128 code · 47 blank · 62 comment · 0 complexity · 1edb0f99dd69b9ac5c24ec59e5a4ffe6 MD5 · raw file

  1. <?php
  2. /**
  3. * Tests the retokenization of the double arrow to T_MATCH_ARROW for PHP 8.0 match structures
  4. * and makes sure that the tokenization of other double arrows (array, arrow function, yield)
  5. * is not aversely affected.
  6. *
  7. * @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
  8. * @copyright 2020-2021 Squiz Pty Ltd (ABN 77 084 670 600)
  9. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  10. */
  11. namespace PHP_CodeSniffer\Tests\Core\Tokenizer;
  12. use PHP_CodeSniffer\Tests\Core\AbstractMethodUnitTest;
  13. class DoubleArrowTest extends AbstractMethodUnitTest
  14. {
  15. /**
  16. * Test that "normal" double arrows are correctly tokenized as `T_DOUBLE_ARROW`.
  17. *
  18. * @param string $testMarker The comment prefacing the target token.
  19. *
  20. * @dataProvider dataDoubleArrow
  21. * @coversNothing
  22. *
  23. * @return void
  24. */
  25. public function testDoubleArrow($testMarker)
  26. {
  27. $tokens = self::$phpcsFile->getTokens();
  28. $token = $this->getTargetToken($testMarker, [T_DOUBLE_ARROW, T_MATCH_ARROW, T_FN_ARROW]);
  29. $tokenArray = $tokens[$token];
  30. $this->assertSame(T_DOUBLE_ARROW, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_DOUBLE_ARROW (code)');
  31. $this->assertSame('T_DOUBLE_ARROW', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_DOUBLE_ARROW (type)');
  32. }//end testDoubleArrow()
  33. /**
  34. * Data provider.
  35. *
  36. * @see testDoubleArrow()
  37. *
  38. * @return array
  39. */
  40. public function dataDoubleArrow()
  41. {
  42. return [
  43. 'simple_long_array' => ['/* testLongArrayArrowSimple */'],
  44. 'simple_short_array' => ['/* testShortArrayArrowSimple */'],
  45. 'simple_long_list' => ['/* testLongListArrowSimple */'],
  46. 'simple_short_list' => ['/* testShortListArrowSimple */'],
  47. 'simple_yield' => ['/* testYieldArrowSimple */'],
  48. 'simple_foreach' => ['/* testForeachArrowSimple */'],
  49. 'long_array_with_match_value_1' => ['/* testLongArrayArrowWithNestedMatchValue1 */'],
  50. 'long_array_with_match_value_2' => ['/* testLongArrayArrowWithNestedMatchValue2 */'],
  51. 'short_array_with_match_value_1' => ['/* testShortArrayArrowWithNestedMatchValue1 */'],
  52. 'short_array_with_match_value_2' => ['/* testShortArrayArrowWithNestedMatchValue2 */'],
  53. 'long_array_with_match_key' => ['/* testLongArrayArrowWithMatchKey */'],
  54. 'short_array_with_match_key' => ['/* testShortArrayArrowWithMatchKey */'],
  55. 'long_array_in_match_body_1' => ['/* testLongArrayArrowInMatchBody1 */'],
  56. 'long_array_in_match_body_2' => ['/* testLongArrayArrowInMatchBody2 */'],
  57. 'long_array_in_match_body_2' => ['/* testLongArrayArrowInMatchBody3 */'],
  58. 'short_array_in_match_body_1' => ['/* testShortArrayArrowInMatchBody1 */'],
  59. 'short_array_in_match_body_2' => ['/* testShortArrayArrowInMatchBody2 */'],
  60. 'short_array_in_match_body_2' => ['/* testShortArrayArrowInMatchBody3 */'],
  61. 'short_array_in_match_case_1' => ['/* testShortArrayArrowinMatchCase1 */'],
  62. 'short_array_in_match_case_2' => ['/* testShortArrayArrowinMatchCase2 */'],
  63. 'short_array_in_match_case_3' => ['/* testShortArrayArrowinMatchCase3 */'],
  64. 'long_array_in_match_case_4' => ['/* testLongArrayArrowinMatchCase4 */'],
  65. 'in_complex_short_array_key_match_value' => ['/* testShortArrayArrowInComplexMatchValueinShortArrayKey */'],
  66. 'in_complex_short_array_toplevel' => ['/* testShortArrayArrowInComplexMatchArrayMismash */'],
  67. 'in_complex_short_array_value_match_value' => ['/* testShortArrayArrowInComplexMatchValueinShortArrayValue */'],
  68. 'long_list_in_match_body' => ['/* testLongListArrowInMatchBody */'],
  69. 'long_list_in_match_case' => ['/* testLongListArrowInMatchCase */'],
  70. 'short_list_in_match_body' => ['/* testShortListArrowInMatchBody */'],
  71. 'short_list_in_match_case' => ['/* testShortListArrowInMatchCase */'],
  72. 'long_list_with_match_in_key' => ['/* testLongListArrowWithMatchInKey */'],
  73. 'short_list_with_match_in_key' => ['/* testShortListArrowWithMatchInKey */'],
  74. 'long_array_with_constant_default_in_key' => ['/* testLongArrayArrowWithClassConstantKey */'],
  75. 'short_array_with_constant_default_in_key' => ['/* testShortArrayArrowWithClassConstantKey */'],
  76. 'yield_with_constant_default_in_key' => ['/* testYieldArrowWithClassConstantKey */'],
  77. 'long_array_with_default_in_key_in_match' => ['/* testLongArrayArrowWithClassConstantKeyNestedInMatch */'],
  78. 'short_array_with_default_in_key_in_match' => ['/* testShortArrayArrowWithClassConstantKeyNestedInMatch */'],
  79. 'long_array_with_default_in_key_with_match' => ['/* testLongArrayArrowWithClassConstantKeyWithNestedMatch */'],
  80. 'long_array_with_default_in_key_with_match' => ['/* testShortArrayArrowWithClassConstantKeyWithNestedMatch */'],
  81. ];
  82. }//end dataDoubleArrow()
  83. /**
  84. * Test that double arrows in match expressions which are the demarkation between a case and the return value
  85. * are correctly tokenized as `T_MATCH_ARROW`.
  86. *
  87. * @param string $testMarker The comment prefacing the target token.
  88. *
  89. * @dataProvider dataMatchArrow
  90. * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
  91. *
  92. * @return void
  93. */
  94. public function testMatchArrow($testMarker)
  95. {
  96. $tokens = self::$phpcsFile->getTokens();
  97. $token = $this->getTargetToken($testMarker, [T_DOUBLE_ARROW, T_MATCH_ARROW, T_FN_ARROW]);
  98. $tokenArray = $tokens[$token];
  99. $this->assertSame(T_MATCH_ARROW, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_MATCH_ARROW (code)');
  100. $this->assertSame('T_MATCH_ARROW', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_MATCH_ARROW (type)');
  101. }//end testMatchArrow()
  102. /**
  103. * Data provider.
  104. *
  105. * @see testMatchArrow()
  106. *
  107. * @return array
  108. */
  109. public function dataMatchArrow()
  110. {
  111. return [
  112. 'single_case' => ['/* testMatchArrowSimpleSingleCase */'],
  113. 'multi_case' => ['/* testMatchArrowSimpleMultiCase */'],
  114. 'single_case_with_trailing_comma' => ['/* testMatchArrowSimpleSingleCaseWithTrailingComma */'],
  115. 'multi_case_with_trailing_comma' => ['/* testMatchArrowSimpleMultiCaseWithTrailingComma */'],
  116. 'match_nested_outer' => ['/* testMatchArrowNestedMatchOuter */'],
  117. 'match_nested_inner' => ['/* testMatchArrowNestedMatchInner */'],
  118. 'in_long_array_value_1' => ['/* testMatchArrowInLongArrayValue1 */'],
  119. 'in_long_array_value_2' => ['/* testMatchArrowInLongArrayValue2 */'],
  120. 'in_long_array_value_3' => ['/* testMatchArrowInLongArrayValue3 */'],
  121. 'in_short_array_value_1' => ['/* testMatchArrowInShortArrayValue1 */'],
  122. 'in_short_array_value_2' => ['/* testMatchArrowInShortArrayValue2 */'],
  123. 'in_short_array_value_3' => ['/* testMatchArrowInShortArrayValue3 */'],
  124. 'in_long_array_key_1' => ['/* testMatchArrowInLongArrayKey1 */'],
  125. 'in_long_array_key_2' => ['/* testMatchArrowInLongArrayKey2 */'],
  126. 'in_short_array_key_1' => ['/* testMatchArrowInShortArrayKey1 */'],
  127. 'in_short_array_key_2' => ['/* testMatchArrowInShortArrayKey2 */'],
  128. 'with_long_array_value_with_keys' => ['/* testMatchArrowWithLongArrayBodyWithKeys */'],
  129. 'with_short_array_value_without_keys' => ['/* testMatchArrowWithShortArrayBodyWithoutKeys */'],
  130. 'with_long_array_value_without_keys' => ['/* testMatchArrowWithLongArrayBodyWithoutKeys */'],
  131. 'with_short_array_value_with_keys' => ['/* testMatchArrowWithShortArrayBodyWithKeys */'],
  132. 'with_short_array_with_keys_as_case' => ['/* testMatchArrowWithShortArrayWithKeysAsCase */'],
  133. 'with_multiple_arrays_with_keys_as_case' => ['/* testMatchArrowWithMultipleArraysWithKeysAsCase */'],
  134. 'in_fn_body_case' => ['/* testMatchArrowInFnBody1 */'],
  135. 'in_fn_body_default' => ['/* testMatchArrowInFnBody2 */'],
  136. 'with_fn_body_case' => ['/* testMatchArrowWithFnBody1 */'],
  137. 'with_fn_body_default' => ['/* testMatchArrowWithFnBody2 */'],
  138. 'in_complex_short_array_key_1' => ['/* testMatchArrowInComplexShortArrayKey1 */'],
  139. 'in_complex_short_array_key_2' => ['/* testMatchArrowInComplexShortArrayKey2 */'],
  140. 'in_complex_short_array_value_1' => ['/* testMatchArrowInComplexShortArrayValue1 */'],
  141. 'in_complex_short_array_key_2' => ['/* testMatchArrowInComplexShortArrayValue1 */'],
  142. 'with_long_list_in_body' => ['/* testMatchArrowWithLongListBody */'],
  143. 'with_long_list_in_case' => ['/* testMatchArrowWithLongListInCase */'],
  144. 'with_short_list_in_body' => ['/* testMatchArrowWithShortListBody */'],
  145. 'with_short_list_in_case' => ['/* testMatchArrowWithShortListInCase */'],
  146. 'in_long_list_key' => ['/* testMatchArrowInLongListKey */'],
  147. 'in_short_list_key' => ['/* testMatchArrowInShortListKey */'],
  148. 'with_long_array_value_with_default_key' => ['/* testMatchArrowWithNestedLongArrayWithClassConstantKey */'],
  149. 'with_short_array_value_with_default_key' => ['/* testMatchArrowWithNestedShortArrayWithClassConstantKey */'],
  150. 'in_long_array_value_with_default_key' => ['/* testMatchArrowNestedInLongArrayWithClassConstantKey */'],
  151. 'in_short_array_value_with_default_key' => ['/* testMatchArrowNestedInShortArrayWithClassConstantKey */'],
  152. ];
  153. }//end dataMatchArrow()
  154. /**
  155. * Test that double arrows used as the scope opener for an arrow function
  156. * are correctly tokenized as `T_FN_ARROW`.
  157. *
  158. * @param string $testMarker The comment prefacing the target token.
  159. *
  160. * @dataProvider dataFnArrow
  161. * @covers PHP_CodeSniffer\Tokenizers\PHP::processAdditional
  162. *
  163. * @return void
  164. */
  165. public function testFnArrow($testMarker)
  166. {
  167. $tokens = self::$phpcsFile->getTokens();
  168. $token = $this->getTargetToken($testMarker, [T_DOUBLE_ARROW, T_MATCH_ARROW, T_FN_ARROW]);
  169. $tokenArray = $tokens[$token];
  170. $this->assertSame(T_FN_ARROW, $tokenArray['code'], 'Token tokenized as '.$tokenArray['type'].', not T_FN_ARROW (code)');
  171. $this->assertSame('T_FN_ARROW', $tokenArray['type'], 'Token tokenized as '.$tokenArray['type'].', not T_FN_ARROW (type)');
  172. }//end testFnArrow()
  173. /**
  174. * Data provider.
  175. *
  176. * @see testFnArrow()
  177. *
  178. * @return array
  179. */
  180. public function dataFnArrow()
  181. {
  182. return [
  183. 'simple_fn' => ['/* testFnArrowSimple */'],
  184. 'with_match_as_value' => ['/* testFnArrowWithMatchInValue */'],
  185. 'in_match_value_case' => ['/* testFnArrowInMatchBody1 */'],
  186. 'in_match_value_default' => ['/* testFnArrowInMatchBody2 */'],
  187. 'in_complex_match_value_in_short_array' => ['/* testFnArrowInComplexMatchValueInShortArrayValue */'],
  188. ];
  189. }//end dataFnArrow()
  190. }//end class