Skip to content

PSR2.ControlStructures.SwitchDeclaration.TerminatingComment does not handle try/finally blocks #3297

@umherirrender

Description

@umherirrender

Describe the bug
Using the sniff PSR2.ControlStructures.SwitchDeclaration brings in an edge case for the error code TerminatingComment when having try/finally in the switch case body

Code sample

<?php

$statusValue = mt_rand();
$resource = SomeClass::getWorker();

switch ( $statusValue ) {
	case 0:
		// fall-through
	case 2:
		try {
			return $resource->doWork();
		} finally {
			$resource->release();
		}
	case 3:
		$other = $code;
		break;
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
  <rule ref="PSR2.ControlStructures.SwitchDeclaration" />
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
 9 | ERROR | There must be a comment when fall-through is intentional
   |       | in a non-empty case body
   |       | (PSR2.ControlStructures.SwitchDeclaration.TerminatingComment)

Expected behavior
The try/finally always returns, that means there is no fall-through which does not need a comment.
That also the same when using finally + catch or catch alone

Versions (please complete the following information):

  • OS: Windows 10
  • PHP: 8.0
  • PHPCS: 3.6.0
  • Standard: -

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions