Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leading backslash in global namespace #7686

Closed
norberttech opened this issue Jan 6, 2024 · 3 comments · Fixed by #7679
Closed

Leading backslash in global namespace #7686

norberttech opened this issue Jan 6, 2024 · 3 comments · Fixed by #7679
Labels
kind/bug topic/fqcn Fully Qualified Class Name usage and conversions

Comments

@norberttech
Copy link

norberttech commented Jan 6, 2024

Bug report

Description

After v3.46.0 CS fixer started removing backslashes from global namespaces,

-\Exception
+Exception

Which I guess was correct behavior since it wasn't specified, but then after adding:

        'fully_qualified_strict_types' => [
            'leading_backslash_in_global_namespace' => true,
        ]

it worked as expected except few things like for example:

use Nyholm\Psr7\Factory\Psr17Factory;
-use Psr\Http\Message;

$factory = new Psr17Factory();
$client = new Client($factory, $factory);

$extractor = new PsrHttpClientDynamicExtractor($client, new class implements NextRequestFactory {
-    public function create(?Message\ResponseInterface $previousResponse = null) : ?Message\RequestInterface
+   public function create(?\Message\ResponseInterface $previousResponse = null) : ?\Message\RequestInterface

or

-    't_shirt_color' => self::$colors[\array_rand(self::$colors)],
-    'country_code' => self::$countries[\array_rand(self::$countries)],
+    't_shirt_color' => \self::$colors[\array_rand(\self::$colors)],
+    'country_code' => \self::$countries[\array_rand(\self::$countries)],

which looks like a bug to me.

Runtime version

PHP CS Fixer 3.46.0 Three Keys by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.1.27

Used command

Configuration file

<?php 

declare(strict_types=1);

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$finder = Finder::create()
    ->files()
    ->in([...]);

return (new Config())
    ->setRiskyAllowed(true)
    ->setCacheFile(__DIR__ . '/var/cs-fixer/php_cs.cache')
    ->setRules([
        '@Symfony' => true,
        '@Symfony:risky' => true,
        'blank_line_after_opening_tag' => false,
        'blank_line_before_statement' => [
            'statements' => [
                'break',
                'continue',
                'declare',
                'default',
                'do',
                'exit',
                'for',
                'foreach',
                'goto',
                'if',
                'include',
                'include_once',
                'require',
                'require_once',
                'return',
                'switch',
                'throw',
                'try',
                'while',
            ],
        ],
        'blank_line_between_import_groups' => false,
        'blank_lines_before_namespace' => false,
        'class_attributes_separation' => ['elements' => ['const' => 'one', 'method' => 'one', 'property' => 'one']],
        'combine_consecutive_issets' => true,
        'combine_consecutive_unsets' => true,
        'concat_space' => ['spacing' => 'one'],
        'declare_strict_types' => true,
        'explicit_indirect_variable' => true,
        'explicit_string_variable' => true,
        'fopen_flags' => true,
        'heredoc_to_nowdoc' => true,
        'increment_style' => ['style' => 'post'],
        'linebreak_after_opening_tag' => false,
        'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
        'modernize_types_casting' => false,
        'multiline_comment_opening_closing' => true,
        'multiline_whitespace_before_semicolons' => true,
        'native_constant_invocation' => false,
        'native_function_invocation' => ['include' => ['@all']], // todo
        'new_with_parentheses' => false,
        'nullable_type_declaration_for_default_null_value' => true,
        'no_extra_blank_lines' => true, // todo?
        'no_mixed_echo_print' => ['use' => 'print'],
        'no_superfluous_elseif' => true,
        'no_superfluous_phpdoc_tags' => false,
        'no_unneeded_control_parentheses' => true, // todo?
        'no_unreachable_default_argument_value' => true,
        'no_unset_on_property' => true,
        'no_useless_else' => true,
        'no_useless_return' => true,
        'ordered_class_elements' => [
            'order' => [
                'use_trait',
                'constant_public',
                'constant_protected',
                'constant_private',
                'case',
                'property_public_static',
                'property_protected_static',
                'property_private_static',
                'property_public',
                'property_protected',
                'property_private',
                'construct',
                'method_public_static',
                'destruct',
                'magic',
                'phpunit',
                'method_public',
                'method_protected',
                'method_private',
                'method_protected_static',
                'method_private_static',
            ],
            'sort_algorithm' => 'alpha'
        ],
        'ordered_imports' => [
            'imports_order' => [
                'const',
                'function',
                'class',
            ],
            'sort_algorithm' => 'alpha',
        ],
        'ordered_interfaces' => [
            'direction' => 'ascend',
            'order' => 'alpha',
        ],
        'phpdoc_align' => ['align' => 'left'],
        'phpdoc_no_empty_return' => true,
        'phpdoc_order' => true,
        'phpdoc_to_comment' => false,
        'phpdoc_types_order' => true,
        'php_unit_method_casing' => ['case' => 'snake_case'],
        'protected_to_private' => true,
        'return_assignment' => false,
        'return_type_declaration' => ['space_before' => 'one'],
        'self_static_accessor' => true,
        'single_line_throw' => false,
        'strict_param' => true,
        'ternary_to_null_coalescing' => true,
        'yoda_style' => false,
        'void_return' => true,
        'fully_qualified_strict_types' => [
            'leading_backslash_in_global_namespace' => true,
        ]
    ])
    ->setFinder($finder);

Code snippet that reproduces the problem

It can be reproduced on this repo: https://github.com/flow-php/flow

@Wirone
Copy link
Member

Wirone commented Jan 6, 2024

Hi @norberttech 🙂. It most probably will be fixed with #7679, @mvorisek can you confirm?

@mvorisek
Copy link
Contributor

mvorisek commented Jan 6, 2024

Which I guess was correct behavior since it wasn't specified, but then after adding:

You are correct. #7679 will fix it, hopefully it will be merged soon :)

PS: in general, leading backslash in global namespace is not needed, leading_backslash_in_global_namespace is however very helpful when migrating code from non-namespaced to namespaced.

@norberttech
Copy link
Author

Thank you both for your quick response!

PS: in general, leading backslash in global namespace is not needed, leading_backslash_in_global_namespace is however very helpful when migrating code from non-namespaced to namespace.

I just pretty much want all functions/classes from the global namespace to have the leading backslash without a use statement. I also want to enforce it on a project level so when any contributor does something like final class CustomException extends Exception it would get corrected into final class CustomException extends \Exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug topic/fqcn Fully Qualified Class Name usage and conversions
Projects
None yet
3 participants