0

I found myself looking on the internet for someone who has already answered it, but I can't find any.

I was cleaning up some code with a strict PSR12 standard, and I found this issue with namespace getting longer than the configured 80 characters (which is our soft limit, which created a warning).

So far I have found a proper way of writing readable code while staying well within the 80 lines, no matter how long the names were. But for the namespace I haven't. Many of our namespace declarations look like this:

namespace CompanyName\ProjectName\Domain\SubClasification\Clasification\ModuleName;

They can get quite long, especially since many on my colleagues like very long class names, so I would like to know if there are ways to fit the namespace declaration on multiple lines?

  • Any line length fits as *There MUST NOT be a hard limit on line length* – Nigel Ren Aug 31 '20 at 17:48
  • Does this answer your question? [What is a line length "soft limit" and how do I interpret this in the PSR-2 convention?](https://stackoverflow.com/questions/14962667/what-is-a-line-length-soft-limit-and-how-do-i-interpret-this-in-the-psr-2-conv) – chiliNUT Aug 31 '20 at 17:54
  • Yes, fully aware of how the soft and hard limit are meant to do, but I was getting some warnings for something where I couldn't find an obvious solution to follow the guidelines provided in PSR12 while still creating clean and readable code. – Danny Peeters Sep 01 '20 at 08:51

1 Answers1

1

You can split the namespace on the separator character, so this is valid syntax:

namespace CompanyName
    \ProjectName
    \Domain
    \SubClasification
    \Clasification
    \ModuleName;

Alternatively, you can tell PHPCS to ignore the next line:

// phpcs:ignore
namespace CompanyName\ProjectName\Domain\SubClasification\Clasification\ModuleName;

You could also perhaps create a custom PHPCS sniff that extends the existing line length sniff but excludes namespace lines.

[EDIT] Note, it looks like the ability to multi-line the namespace may be going away in PHP 8: https://3v4l.org/IWbe1

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98