FunctionComment broken for complex string types

Created on 22 March 2021, over 3 years ago
Updated 18 April 2023, about 1 year ago

Problem/Motivation

Psalm and phpstan support complex strings that provide some additional static analysis like "numeric-string" or "class-string" that can help find type miss-matches and logic errors.

https://phpstan.org/writing-php-code/phpdoc-types
https://psalm.dev/docs/annotating_code/type_syntax/scalar_types/

However using these breaks code's FunctionCommentSniff.

----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 244 | ERROR | [x] Expected "numericstring|null" but found
     |       |     "numeric-string|null" for parameter type
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Steps to reproduce

Here's a method that will trigger the sniff failure.

  /**
   * Apply numeric formatting rules from style guide.
   *
   * @param numeric-string|null $value
   *   Unformated raw numeric data.
   * @param string $format
   *   The expected format.
   * @param int $precision
   *   The numeric "precision" to apply to the output.
   *
   * @return string
   *   A formatted number ready for display.
   */
  private function formatData($value, string $format, $precision) {
    //
  }

Proposed resolution

At least one of the problems is that FunctionCommentSniff::suggestType treats '-' as an invalid character in types.

I poked at this briefly and it seems to be a bit more complex with fixing that just revealing some additional checks in FunctionCommentSniff that fail so probably more fixes would be required.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Fixed

Version

8.3

Component

Coder Sniffer

Created by

πŸ‡ΊπŸ‡ΈUnited States neclimdul Houston, TX

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024