Php_codesniffer: Generic.Whitespace.LanguageConstructSpacing should not be checking namespace operators

Created on 19 Oct 2018  路  5Comments  路  Source: squizlabs/PHP_CodeSniffer

The Generic.Whitespace.LanguageConstructSpacing sniff demands one space after PHP language construct keywords.

The namespace keyword however, is both a construct as well as an operator, depending on the context.

<?php
// Here it is a language construct:
namespace MyProject;

// Here it is an operator or as the documentation states:
// "It is the namespace equivalent of the self operator for classes. "
namespace\func(); // calls function MyProject\func()
$a = new namespace\sub\cname(); // instantiates object of class MyProject\sub\cname

See: http://php.net/manual/en/language.namespaces.nsconstants.php#example-256

Currently, the sniff will trigger errors for the use of the keyword as an operator and demand one space after the keyword.

This, to me, is unexpected.

Is this intended behaviour or a bug ? /cc @gmponos

Bug

All 5 comments

It's a bug

Ok, will fix it.

@gsherwood Just checking - should just the bug be fixed, i.e. no error should be thrown, or should the sniff be enhanced to demand no space between the namespace keyword as an operator and the \ in that case ?

I think it should just ignore the namespace keyword when used like that. Rules for how namespaces are defined (spaces or not, newlines or not, etc) should probably be left to other sniffs.

Okidoki. Though IIRC there is no sniff which currently covers the namespace keyword as an operator. I think I'll address this when I finally get round to fixing #2150.

Was this page helpful?
0 / 5 - 0 ratings