Questions tagged [psr-2]

PSR-2 is a PHP Standards Recommendation containing PHP's Coding Style Guide.

PSR-2 is a PHP Standards Recommendation containing 's Coding Style Guide. It extends and expands .

67 questions
5
votes
2 answers

PSR-2 Opening braces for control structures

One thing which I am trying to understand why PSR2 has curly brackets for class and method on new line and for the rest not. I would say it's much easier to read code if curly brackets are always on a new line. class Foo extends Bar implements…
Zdenek Machek
  • 1,758
  • 1
  • 20
  • 30
5
votes
1 answer

PHP PSR-2 new lines before/ after statments

Example no 1: $something = [1,2,3,4] foreach ($something as $key => $value) { //code code code } $something2 = []; I can't find clear answer about new live before and after foreach, what PSR-2 say about that ?
Wizard
  • 10,985
  • 38
  • 91
  • 165
5
votes
1 answer

psr-2 more then one blank line

Is it a valid way(PSR-2) to write code with 2 blank lines in a row? For example: $a = 1; $b = 2; Or there always has to be only one blank line beetween $a and $b?
jftp
  • 185
  • 1
  • 8
5
votes
3 answers

Multiple "right margins" or guides in PHPStorm

PHPStorm has a vertical line on what they call the "right margin" which is set by default to 120 characters or columns... Given that PSR standards state There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines…
user1960364
  • 1,951
  • 6
  • 28
  • 47
4
votes
2 answers

PhpStorm enforcing PSR-2 on a subset of files

I want my IDE (PhpStorm) to enforce PSR-2 compatible formatting on all of own files. Occasionally however, I have to submit patches on legacy code where I only want to commit functional changes, not changes in code style. How can I enforce PSR-2…
ooxi
  • 3,159
  • 2
  • 28
  • 41
4
votes
2 answers

Clarifications on PSR-2 indenting rules

The PSR-2 Coding Style Guide chooses 4-space indent over tabs. I had always assumed that tabs vs spaces was just a question of preference and this impression of mine seems kind of supported by the fact that even PSR-2 project members survey was far…
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
4
votes
2 answers

PSR-2. When to capitalize directories?

As I understood according to PSR-2 standard we should capitalize directory names which contain classes. But how to do if the directory also contains some other files, for example styles and scripts? Say I have a plugin directory, each plugin can…
ymakux
  • 3,415
  • 1
  • 34
  • 43
4
votes
1 answer

PSR-2 compatible inline PHP tags in HTML templates

PSR-2 doesn't mention anything that would apply to the use of inline php tags in HTML templates, yet the php-cs-fixer tool seems to have a standard for it. This is my template file, before running php-cs-fixer on it:
Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
3
votes
3 answers

What will line ending cause when programming?

There is a thing make me puzzled in 2.2 of PSR-2 document. All I know is that different os use different line ending as default : windows:CRLF unix:LF mac:CR I want to know why should "All PHP files MUST use the Unix LF(linefeed) line ending"…
chenxinlong
  • 1,677
  • 2
  • 15
  • 30
3
votes
0 answers

How to check php code for psr 2 validation in Netbeans

Is there any Plugin in Netbeans for checking PSR2 Violation and fix the same. I tried adding php-cs-fixer.phar in Netbeans > Options > php > frameworks and tools > PHP CS Fixer.Then click on Fix option PHP CS Fixer.This option can be seen by right…
Shakun Chaudhary
  • 341
  • 3
  • 6
  • 15
3
votes
1 answer

PSR and constructor visibility

According to PSR-2 Visibility MUST be declared on all properties and methods but what about __construct, it's specific function and I was wondering if it should also be marked by default as public when we're using PSR?
3
votes
1 answer

Where in the PSR standards does it say to not have the variable name after the type declaration?

Can anyone advise where in the PSR standards docs it documents that there should be nothing after the variable type in class member variables? I used PHP CodeStyle Fixer by doing the following: php-cs-fixer fix MyConsoleCommand.php ...and it removed…
crmpicco
  • 16,605
  • 26
  • 134
  • 210
3
votes
2 answers

PHP: PSR-2: Why blank lines after namespace / use declarations?

So I was going over the PSR coding standards the other day (which I duly follow), and Ive kind of always understood the reasons for everything, and mostly coded that way for everything Ive worked on over the last 10+ years as very similar standards…
Todd
  • 2,824
  • 2
  • 29
  • 39
2
votes
1 answer

PSR-2 and "Line exceeds by 120 characters" for a Codeline

I'm trying to write some code to the PSR-2 PHP Standard. At validation I get a lot of errors like this: Line exceeds 120 characters; contains 122 characters I've tried a few ways to fix this problem. Here is the original line: $s = sprintf('%.2F…
Xyraphid
  • 23
  • 1
  • 5
2
votes
0 answers

How do I tell PhpStorm not to put an empty line before a class' closing brace?

When I run PHP Code Inspector set to PSR-2 over my PHP code, it always points out that "the closing brace for a class must go on the next line after the body." PhpStorm formats classes like this: class MyClass { public function myFunction1() …