From 0b120bd04b17ff16dc9b6133a9ec7a5cad555daa Mon Sep 17 00:00:00 2001 From: vrana Date: Fri, 22 Feb 2013 21:12:29 -0800 Subject: [PATCH] Add unit tests for implicit fallthrough lint rule Summary: The perf fix actually catches some real problems. I didn't find anything in libphutil, Arcanist and Phabricator though. Also bump version. Also allow configuring the hook. Test Plan: Added a test, saw it fail with the old code. Repeat for hook. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D5097 --- src/__phutil_library_map__.php | 2 ++ src/lint/linter/ArcanistXHPASTLinter.php | 3 ++- .../ArcanistXHPASTLintTestSwitchHook.php | 16 ++++++++++++++++ .../linter/__tests__/xhpast/switches.lint-test | 9 ++++++++- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/lint/linter/__tests__/ArcanistXHPASTLintTestSwitchHook.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index bdc96826..546cdac8 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -149,6 +149,7 @@ phutil_register_library_map(array( 'ArcanistXHPASTLintNamingHook' => 'lint/linter/xhpast/ArcanistXHPASTLintNamingHook.php', 'ArcanistXHPASTLintNamingHookTestCase' => 'lint/linter/xhpast/__tests__/ArcanistXHPASTLintNamingHookTestCase.php', 'ArcanistXHPASTLintSwitchHook' => 'lint/linter/xhpast/ArcanistXHPASTLintSwitchHook.php', + 'ArcanistXHPASTLintTestSwitchHook' => 'lint/linter/__tests__/ArcanistXHPASTLintTestSwitchHook.php', 'ArcanistXHPASTLinter' => 'lint/linter/ArcanistXHPASTLinter.php', 'ArcanistXHPASTLinterTestCase' => 'lint/linter/__tests__/ArcanistXHPASTLinterTestCase.php', 'ComprehensiveLintEngine' => 'lint/engine/ComprehensiveLintEngine.php', @@ -275,6 +276,7 @@ phutil_register_library_map(array( 'ArcanistUserAbortException' => 'ArcanistUsageException', 'ArcanistWhichWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistXHPASTLintNamingHookTestCase' => 'ArcanistTestCase', + 'ArcanistXHPASTLintTestSwitchHook' => 'ArcanistXHPASTLintSwitchHook', 'ArcanistXHPASTLinter' => 'ArcanistBaseXHPASTLinter', 'ArcanistXHPASTLinterTestCase' => 'ArcanistArcanistLinterTestCase', 'ComprehensiveLintEngine' => 'ArcanistLintEngine', diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 886236be..5501cd1f 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -157,7 +157,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } public function getCacheVersion() { - $version = '3'; + $version = '4'; $path = xhpast_get_binary_path(); if (Filesystem::pathExists($path)) { $version .= '-'.md5_file($path); @@ -480,6 +480,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { $working_copy = $this->getEngine()->getWorkingCopy(); if ($working_copy) { $hook_class = $working_copy->getConfig('lint.xhpast.switchhook'); + $hook_class = $this->getConfig('switchhook', $hook_class); if ($hook_class) { $hook_obj = newv($hook_class, array()); assert_instances_of(array($hook_obj), 'ArcanistXHPASTLintSwitchHook'); diff --git a/src/lint/linter/__tests__/ArcanistXHPASTLintTestSwitchHook.php b/src/lint/linter/__tests__/ArcanistXHPASTLintTestSwitchHook.php new file mode 100644 index 00000000..35997eb0 --- /dev/null +++ b/src/lint/linter/__tests__/ArcanistXHPASTLintTestSwitchHook.php @@ -0,0 +1,16 @@ +getTypeName() == 'T_STRING') { + switch (strtolower($token->getValue())) { + case 'throw_exception': + return true; + } + } + return false; + } + +} diff --git a/src/lint/linter/__tests__/xhpast/switches.lint-test b/src/lint/linter/__tests__/xhpast/switches.lint-test index 0e4aafed..b32d55cd 100644 --- a/src/lint/linter/__tests__/xhpast/switches.lint-test +++ b/src/lint/linter/__tests__/xhpast/switches.lint-test @@ -78,6 +78,10 @@ switch ($x) { do { break 2; } while (true); + case 7: + /* fallthrough */ + case 8: + throw_exception(); } ~~~~~~~~~~ warning:41:3 @@ -87,4 +91,7 @@ warning:57:3 warning:66:3 disabled:68:7 # PHP 5.3 features warning:71:3 -warning:75:3 \ No newline at end of file +warning:75:3 +~~~~~~~~~~ +~~~~~~~~~~ +{"config":{"switchhook":"ArcanistXHPASTLintTestSwitchHook"}} -- 2.43.0