phpinsights icon indicating copy to clipboard operation
phpinsights copied to clipboard

Possible false positive after updating to `2.4.0`

Open tarlepp opened this issue 2 years ago • 2 comments

Q A
Bug report? yes
Feature request? no
Library version 2.4.0

After updating to 2.4.0 I see following error:

• [Code] Useless constant type hint:
  src/Validator/Constraints/Locale.php:37: Useless @var annotation.

And the related code:

/**
 * {@inheritdoc}
 *
 * @var array<string, string> // Line 37
 */
protected const ERROR_NAMES = [
    self::INVALID_LOCALE => 'INVALID_LOCALE',
];

I really think that error is false-positive - or am I missing something here?

And sure I could "fix" this to just change that to use @psalm-var array<string, string> or @phpstan-var array<string, string> - but imho that is not the correct solution here.

tarlepp avatar Jun 06 '22 17:06 tarlepp

I completely agree, I have noticed this with const in PHP strongly typed, analysers seem to think it is incorrect. Which analyser did you use that is giving you this issue? Could you share your config?

JustSteveKing avatar Jun 13 '22 07:06 JustSteveKing

Sure, here is contents of phpinsights.php file:

<?php
declare(strict_types = 1);
/** @noinspection PhpUndefinedNamespaceInspection */
/** @noinspection PhpUndefinedClassInspection */

return [
    /*
    |--------------------------------------------------------------------------
    | Default Preset
    |--------------------------------------------------------------------------
    |
    | This option controls the default preset that will be used by PHP Insights
    | to make your code reliable, simple, and clean. However, you can always
    | adjust the `Metrics` and `Insights` below in this configuration file.
    |
    | Supported: "default", "laravel", "symfony"
    |
    */
    'preset' => 'symfony',
    /*
    |--------------------------------------------------------------------------
    | Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may adjust all the various `Insights` that will be used by PHP
    | Insights. You can either add, remove or configure `Insights`. Keep in
    | mind, that all added `Insights` must belong to a specific `Metric`.
    |
    */
    'exclude' => [
        'bin',
        'build',
        'config',
        'doc',
        'docker',
        'public',
        'secrets',
        'migrations',
        'templates',
        'tests',
        'translations',
        'var',
        'vendor',
        'tools',
    ],
    'add' => [
        //  ExampleMetric::class => [
        //      ExampleInsight::class,
        //  ]
    ],
    'remove' => [
        //  ExampleInsight::class,
        NunoMaduro\PhpInsights\Domain\Insights\Composer\ComposerMustBeValid::class,
        NunoMaduro\PhpInsights\Domain\Insights\ForbiddenNormalClasses::class,
        NunoMaduro\PhpInsights\Domain\Insights\ForbiddenTraits::class,
        NunoMaduro\PhpInsights\Domain\Sniffs\ForbiddenSetterSniff::class,
        ObjectCalisthenics\Sniffs\Classes\ForbiddenPublicPropertySniff::class,
        ObjectCalisthenics\Sniffs\NamingConventions\NoSetterSniff::class,
        PhpCsFixer\Fixer\ArrayNotation\NoMultilineWhitespaceAroundDoubleArrowFixer::class,
        SlevomatCodingStandard\Sniffs\Classes\SuperfluousExceptionNamingSniff::class,
        SlevomatCodingStandard\Sniffs\Classes\SuperfluousInterfaceNamingSniff::class,
        SlevomatCodingStandard\Sniffs\Classes\SuperfluousTraitNamingSniff::class,
        SlevomatCodingStandard\Sniffs\Classes\ForbiddenPublicPropertySniff::class,
        SlevomatCodingStandard\Sniffs\Commenting\DocCommentSpacingSniff::class,
        SlevomatCodingStandard\Sniffs\Commenting\InlineDocCommentDeclarationSniff::class,
        SlevomatCodingStandard\Sniffs\Commenting\UselessInheritDocCommentSniff ::class,
        SlevomatCodingStandard\Sniffs\Commenting\UselessFunctionDocCommentSniff::class,
        SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff::class,
        SlevomatCodingStandard\Sniffs\TypeHints\DisallowMixedTypeHintSniff::class,
        SlevomatCodingStandard\Sniffs\TypeHints\DisallowArrayTypeHintSyntaxSniff::class,
        SlevomatCodingStandard\Sniffs\TypeHints\ParameterTypeHintSniff::class,
        SlevomatCodingStandard\Sniffs\TypeHints\PropertyTypeHintSniff::class,
        SlevomatCodingStandard\Sniffs\TypeHints\ReturnTypeHintSniff::class,
        Symplify\CodingStandard\Fixer\Strict\BlankLineAfterStrictTypesFixer::class,
    ],
    'config' => [
        //  ExampleInsight::class => [
        //      'key' => 'value',
        //  ],
        SlevomatCodingStandard\Sniffs\Functions\FunctionLengthSniff::class => [
            'maxLinesLength' => 50,
        ],
        PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff::class => [
            'lineLimit' => 120,
            'absoluteLineLimit' => 140,
            'ignoreComments' => true,
        ],
        PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff::class => [
            'spacing' => 0,
        ],
        PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff::class => [
            'spacing' => 0,
        ],
        PhpCsFixer\Fixer\CastNotation\CastSpacesFixer::class => [
            'space' => 'none', // possible values ['single', 'none'],
        ],
        PhpCsFixer\Fixer\Import\OrderedImportsFixer::class => [
            'imports_order' => ['class', 'function', 'const'],
            'sort_algorithm' => 'alpha', // possible values ['alpha', 'length', 'none']
        ],
        PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer::class => [
            'space' => 'single', // possible values ['none', 'single']
        ],
        PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer::class => [
            'operators' => [
                '&' => 'align',
            ],
        ],
        SlevomatCodingStandard\Sniffs\Functions\UnusedParameterSniff::class => [
            'exclude' => [
                'src/ArgumentResolver/LoggedInUserValueResolver.php',
                'src/ArgumentResolver/RestDtoValueResolver.php',
                'src/AutoMapper/RestRequestMapper.php',
                'src/Decorator/StopwatchDecorator.php',
                'src/Doctrine/DBAL/Types/EnumType.php',
                'src/Rest/Traits/Methods/RestMethodProcessCriteria.php',
                'src/Rest/Traits/RestResourceCount.php',
                'src/Rest/Traits/RestResourceCreate.php',
                'src/Rest/Traits/RestResourceDelete.php',
                'src/Rest/Traits/RestResourceFind.php',
                'src/Rest/Traits/RestResourceFindOne.php',
                'src/Rest/Traits/RestResourceFindOneBy.php',
                'src/Rest/Traits/RestResourceIds.php',
                'src/Rest/Traits/RestResourcePatch.php',
                'src/Rest/Traits/RestResourceSave.php',
                'src/Rest/Traits/RestResourceUpdate.php',
                'src/Security/Authenticator/ApiKeyAuthenticator.php',
                'src/Security/Handler/TranslatedAuthenticationFailureHandler.php',
                'src/Security/Provider/ApiKeyUserProvider.php',
                'src/Security/Voter/IsUserHimselfVoter.php',
                'src/Serializer/Normalizer/CollectionNormalizer.php',
                'src/Validator/Constraints/LanguageValidator.php',
                'src/Validator/Constraints/LocaleValidator.php',
                'src/Validator/Constraints/TimezoneValidator.php',
                'src/Validator/Constraints/UniqueEmailValidator.php',
                'src/Validator/Constraints/UniqueUsernameValidator.php',
            ],
        ],
        SlevomatCodingStandard\Sniffs\Namespaces\UnusedUsesSniff::class => [
            'searchAnnotations' => true,
        ],
        SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff::class => [
            'linesCountBeforeDeclare' => 0,
            'linesCountAfterDeclare' => 0,
            'spacesCountAroundEqualsSign' => 1,
        ],
    ],
];

tarlepp avatar Jun 13 '22 07:06 tarlepp