Skip to content

PSR12.Functions.NullableTypeDeclaration false positive when using new static() #2641

Closed
@gsherwood

Description

@gsherwood

I have ran into this bug while switching to PSR12 (running 3.5.0).
It seems to be related to the use of new static(...)

Console output:

PS G:\phpcs-bug> phpcs --version
PHP_CodeSniffer version 3.5.0 (stable) by Squiz (http://www.squiz.net)

PS G:\phpcs-bug> phpcs -s --standard=psr12 --report=code --report-width=120 .\Attachment.php -v
Registering sniffs in the PSR12 standard... DONE (59 sniffs registered)
Creating file list... DONE (1 files in queue)
Changing into directory G:\phpcs-bug
Processing Attachment.php [PHP => 282 tokens in 42 lines]... DONE in 10ms (2 errors, 0 warnings)

FILE: G:\phpcs-bug\Attachment.php
------------------------------------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------
LINE 34: ERROR [x] There must not be a space between the question mark and the type in nullable type declarations
                   (PSR12.Functions.NullableTypeDeclaration.WhitespaceFound)
------------------------------------------------------------------------------------------------------------------------
   32:          $finfo = new \finfo(FILEINFO_MIME_TYPE);
   33:          return new static(
>> 34:              (is_null($name) ? basename($file) : $name),
   35:              is_null($type) ? $finfo->file($file) : $type,
   36:              null,
------------------------------------------------------------------------------------------------------------------------
LINE 35: ERROR [ ] There must not be a space between the question mark and the type in nullable type declarations
                   (PSR12.Functions.NullableTypeDeclaration.UnexpectedCharactersFound)
------------------------------------------------------------------------------------------------------------------------
   33:          return new static(
   34:              (is_null($name) ? basename($file) : $name),
>> 35:              is_null($type) ? $finfo->file($file) : $type,
   36:              null,
   37:              $file
------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------
Time: 278ms; Memory: 4MB

PHP file:

<?php

namespace rebar\mail\letter;

class Attachment
{
    private $name;
    private $type;
    private $contents;
    private $file;

    /**
     * Attachment constructor.
     *
     * @param $name
     * @param $type
     * @param $contents
     * @param $file
     */
    private function __construct($name, $type, $contents, $file)
    {
        $this->name = $name;
        $this->type = $type;
        $this->contents = $contents;
        $this->file = $file;
    }

    public static function fromFile($file, $name = null, $type = null)
    {
        $test = Something::one(static::CONSTANT) ?: ''; //Added to test if regression of previous test case

        $finfo = new \finfo(FILEINFO_MIME_TYPE);
        return new static(
            (is_null($name) ? basename($file) : $name),
            is_null($type) ? $finfo->file($file) : $type,
            null,
            $file
        );
    }

    public static function fromFileUsingSelf($file, $name = null, $type = null)
    {
        //Using self instead of static
        $test = Something::one(static::CONSTANT) ?: ''; //Added to test if regression of previous test case

        $finfo = new \finfo(FILEINFO_MIME_TYPE);
        return new self(
            (is_null($name) ? basename($file) : $name),
            is_null($type) ? $finfo->file($file) : $type,
            null,
            $file
        );
    }

    // rest of methods removed for test
}

Originally posted by @tehbeard in #2552 (comment)

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