squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Squiz.Commenting.FunctionComment.InvalidNoReturn with never return type #3798

Closed SharkMachine closed 5 months ago

SharkMachine commented 1 year ago

Describe the bug

Squiz.Commenting.FunctionComment doesn't seem to support PHP 8.1 never return type.

Code sample

<?php

/**
 * @return never
 */
function redirect(): never
{
    header('Location: /');
    exit();
}

Custom ruleset

<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <description>Coding standard.</description>
    <arg name="tab-width" value="4"/>

    <rule ref="PSR12" />

    <!-- PHPDoc and type hinting sniffs -->
    <rule ref="Squiz.Commenting.FunctionComment" />
    <exclude-pattern>vendor/*</exclude-pattern>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above.
  2. Create a file called phpcs.xml with the custom ruleset above.
  3. Run phpcs test.php
  4. See error message displayed
    
    FILE: test.php
    ------------------------------------------------------------------------------------
    FOUND 1 ERROR AFFECTING 1 LINE
    ------------------------------------------------------------------------------------
    4 | ERROR | Function return type is not void, but function has no return statement
    ------------------------------------------------------------------------------------

Time: 34ms; Memory: 6MB



**Expected behavior**

`never` return type is detected and no error is shown.

**Versions (please complete the following information):**

 - OS: Ubuntu 22.04 LTS
 - PHP: 8.1
 - PHPCS: 3.7.2
 - Standard: Squiz
fredden commented 1 year ago

https://github.com/squizlabs/PHP_CodeSniffer/pull/3717 looks like it will fix this.

jrfnl commented 8 months ago

@SharkMachine Could you please verify ? The fix from #3717 was included in the 3.8.0 release from a few weeks ago.

SharkMachine commented 5 months ago

The fix works