Skip to content

Annotation errors reported against wrong token/line #250

Closed
@Majkl578

Description

@Majkl578

Currently annotation errors are bound to the element they're for - either T_VARIABLE or T_FUNCTION.
The error should be reported on the line (pointer) of the annotation itself.

Example

<?php

class Foo
{
    /**
     * @var integer
     */
    public $foo;

    /**
     * Doing foo.
     * @param int $foo
     * @return int
     */
    public function foo(int $foo) : int
    {
        return 123;
    }
}

Actual output

  8 | ERROR | [x] Expected "int" but found "integer" in @var annotation. (SlevomatCodingStandard.TypeHints.LongTypeHints.UsedLongTypeHint)
 15 | ERROR | [x] Method \Foo::foo() has useless @return annotation. (SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation)
 15 | ERROR | [x] Method \Foo::foo() has useless @param annotation for parameter $foo. (SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessParameterAnnotation)

Expected output

  6 | ERROR | [x] Expected "int" but found "integer" in @var annotation. (SlevomatCodingStandard.TypeHints.LongTypeHints.UsedLongTypeHint)
 12 | ERROR | [x] Method \Foo::foo() has useless @return annotation. (SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation)
 13 | ERROR | [x] Method \Foo::foo() has useless @param annotation for parameter $foo. (SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessParameterAnnotation)

This currently makes trouble in Doctrine DBAL (doctrine/dbal#2996, doctrine/dbal#2998) where we use line-based CS validation (so new/changed code obeys CS rules, but existing is still not compliant due the amount of conflicts it would cause).

Supporting this in UselessLongTypeHint sniff should be easy, just by storing $pointer in Annotation itself, but TypeHintDeclaration sniff may not be that trivial.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions