Php-cs-fixer: Import classes instead using FQCN with php_unit_no_expectation_annotation fixer

Created on 25 Oct 2018  Â·  4Comments  Â·  Source: FriendsOfPHP/PHP-CS-Fixer

The PHP version you are using ($ php -v):

PHP 7.2.11-2+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 15 2018 11:40:35) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.11-2+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

PHP CS Fixer version you are using ($ php-cs-fixer -V):

PHP CS Fixer 2.13.0 Yogi's BBQ by Fabien Potencier and Dariusz Ruminski (7136aa4)

The command you use to run PHP CS Fixer:

php-cs-fixer fix

The configuration file you are using, if any:

<?php

return PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        'php_unit_no_expectation_annotation' => true,
    ])
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->in(__DIR__)
    )
;

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
<?php

declare(strict_types=1);

namespace MyApp\Tests;

use PHPUnit\Framework\TestCase;
use MyApp\MyException;

final class MyTest extends TestCase
{
    /**
     * @expectedException \MyApp\MyException
     * @expectedExceptionMessage Test message.
     */
    public function testThrowsException(): void
    {
        throw new MyException;
    }
}
  • with unexpected changes applied when running PHP CS Fixer:
<?php

declare(strict_types=1);

namespace MyApp\Tests;

use PHPUnit\Framework\TestCase;
use MyApp\MyException;

final class MyTest extends TestCase
{
    /**
     */
    public function testThrowsException(): void
    {
        $this->setExpectedException(\MyApp\MyException::class, 'Test message.');

        throw new MyException;
    }
}
  • with the changes you expected instead:
<?php

declare(strict_types=1);

namespace MyApp\Tests;

use PHPUnit\Framework\TestCase;
use MyApp\MyException;

final class MyTest extends TestCase
{
    public function testThrowsException(): void
    {
        $this->setExpectedException(MyException::class, 'Test message.');

        throw new MyException;
    }
}
kinfeature request

Most helpful comment

IMO, this could be a new fixer, as the behaviour is useful outside of the php_unit_no_expectation_annotation fixer. Your thoughts?

All 4 comments

Hi @emodric ,

Thanks for the report! Your request seems like a good improvement to the project so i like to keep it open :)

We're kind of busy with PR's and issues at the moment so it might take some time before someone picks this up, do you plan to give it a shot with a PR to bring this improvement in?

Hi @SpacePossum

I can try to pick it up if I will have some free time, sure! :+1:

IMO, this could be a new fixer, as the behaviour is useful outside of the php_unit_no_expectation_annotation fixer. Your thoughts?

It would be incredibly helpful to have a general fixer to automatically import namespaces. I stupidly thought GlobalNamespaceImportFixer was supposed to do that before I realized my obvious mistake, but it seems like much of the work is already done there, it just needs to be extended to all namespaces. This would also duplicate the FullyQualifiedStrictTypesFixer. I created a simple testcase based on the GlobalNamespaceImportFixerTest:

        return [
            'with same namespace' => [
                <<<'EXPECTED'
<?php
namespace Test;
use foo;

new Foo();
Bar::baz();

/** @return Baz<string, foo> */
function x() {}
EXPECTED
                ,
                <<<'INPUT'
<?php
namespace Test;

new \Foo();
\Test\Bar::baz();

/** @return \Test\Baz<string, \foo> */
function x() {}
INPUT
            ],
            'with child namespace' => [
                <<<'EXPECTED'
<?php
namespace Test;
use Test\Child\Bar;
use Test\Child\Baz;
use foo;

new Foo();
Bar::baz();

/** @return Baz<string, foo> */
function x() {}
EXPECTED
                ,
                <<<'INPUT'
<?php
namespace Test;

new \Foo();
\Test\Child\Bar::baz();

/** @return \Test\Child\Baz<string, \foo> */
function x() {}
INPUT
            ],
        ];
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kcloze picture kcloze  Â·  3Comments

BackEndTea picture BackEndTea  Â·  3Comments

fisharebest picture fisharebest  Â·  3Comments

aidantwoods picture aidantwoods  Â·  3Comments

vitek-rostislav picture vitek-rostislav  Â·  3Comments