Questions tagged [psr-12]

This tag must only be used for questions that specifically relate to the PSR-12 coding standard for PHP. Do not tag general PHP questions with this tag nor use it for other coding standards such as PSR-1 and PSR-2.

Questions tagged with psr-12 refer to the PSR-12: Extended Coding Style which is defined here.

The PSR-12 coding style is intended to extend and replace PSR-2 and arheres to the requirements outlined in PSR-1.

PSR-12 is intended to reduce the difficulties associated with reviewing code written by different authors by declaring specific rules around the formatting of PHP code.

20 questions
765
votes
9 answers

PHP | define() vs. const

In PHP, you can declare constants in two ways: With define keyword define('FOO', 1); Using const keyword const FOO = 1; What are the main differences between those two? When and why should you use one and when use the other?
danjarvis
  • 10,040
  • 3
  • 22
  • 21
4
votes
0 answers

PSR-12 and method chaining

Does PSR-12 establish any kind of restriction with method chaining? Are both options below equally valid according to the standard? Option 1: $object->methodA()->methodB()->methodC()->methodD(); Option 2: $object->methodA() ->methodB() …
Ernesto Allely
  • 851
  • 10
  • 16
3
votes
0 answers

PSR12 Allow newline after closing parenthesis

How can I allow the writing of the bellow code? which rules should I add to my custom ruleset xml? foreach ($mIn as $key => $value) { $mOut -> $key = $mIn -> $key; } currently, PHPCS is issuing a warning stating that a space is expected…
exigez
  • 41
  • 4
3
votes
1 answer

How can I Partially disable PHPCS' PSR-12 declare(strict_types=1) checks?

I currently run php vendor/bin/phpcs --standard=PSR12 src in several of my projects' CI. They have been failing for 6+ months, because my code is organized like:
Theodore R. Smith
  • 21,848
  • 12
  • 65
  • 91
2
votes
0 answers

PhpStorm code style and PHP_CodeSniffer conflict: Multi-line function call not indented correctly; expected 12 spaces but found 16

I am using PHP_CodeSniffer like this to scan my code: vendor/bin/phpcs --standard=PSR12 src I also configured PhpStorm to use PSR-12 at Preferences -> Editor -> Code Style -> Set From -> PSR12 When I run PHP_CodeSniffer there are no error at first,…
Gregory Boutte
  • 584
  • 6
  • 26
2
votes
1 answer

Is using alternative PHP syntax for control structures PSR compliant? (when mixed with HTML)

I have looked through the PHP-Fig website for any PSR related to alternative PHP syntax for control structures but failed to find anything about it. The alternative syntax is this for example:

block…

Theo
  • 2,262
  • 3
  • 23
  • 49
2
votes
1 answer

Is self a keyword in PHP and therefore should it be in lower case according to PSR-12?

The PHP manual does not include self in its list of keywords. A comment on the manual page suggests that self should be included, but it has been significantly downvoted. Does that mean that self is not a keyword? The reason this matters is that the…
Tim Rogers
  • 426
  • 5
  • 14
1
vote
1 answer

Not operator or Strict comparison on false?

What's the best practice for writing if-statement on boolean value? function foo(boolean $bar) { if ($bar === false) // ... // or if (!$bar) // ... } With a Not operator it's more compact, but you may not notice this operator while…
NameX
  • 49
  • 4
1
vote
2 answers

Is it necessary to put space after $this keyword in OOP PHP according to PSRs?

I studied PSR-2 and PSR-12 documentations completely but didn't find any explanation regarding whether to put any space after $this keyword. My exact question is which one of following styles is more correct according to PSRs: return…
IT_man2018
  • 53
  • 7
1
vote
0 answers

Are comparison format and and or satisfy the PSR12 standard?

Considering this fragment of code if ($x > 0 and $x !== 1) { return; } Is the comparison operator expressed as and and or rather than && and || considered acceptable according to the PSR-12 standard?
Antonino Bonumore
  • 787
  • 1
  • 13
  • 27
1
vote
0 answers

Trailing Whitespace and PSR-12 Formatting in Mixed PHP/HTML Files

I'm trying to be more strict about my adherence to PSR-12 and I'm not sure how to handle a specific situation involving whitespace. In the past, I have often used trailing whitespace after a PHP tag to ensure that there is space in the resulting…
Libertie
  • 141
  • 1
  • 10
1
vote
1 answer

Does PSR-12 say anything about the way how namespace imports and aliasing should be grouped and sorted?

I'm working on a code quality automation with phpmd, phpcs and phpcpd tools. The code sniffer has been set to --standard=PSR12. I was unable to find any details or recommendations in terms of the way how namespace imports and aliases should be…
Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92
1
vote
1 answer

PSR-12 and assigning same value to multiple variables

Is it allowed to assign multiple variables on the same line in PSR-12? For example: $count = $score = 0;
Ernesto Allely
  • 851
  • 10
  • 16
1
vote
1 answer

How many new lines before namespace declaration in PSR?

PSR has a fairly clear stance regarding new lines after namespace declaration: When present, there MUST be one blank line after the namespace declaration. But what about blank lines before namespaces? Are there any rules or limitations about it? I…
Gino Pane
  • 4,740
  • 5
  • 31
  • 46
0
votes
0 answers

Is the following code PSR-1 and PSR-12 compliant?

For a project assigned to me, I have to stick to following the PSR coding standard for PHP. Now, I am having a hard time understanding one thing. Is the following PSR-1 and PSR-12 compliant?
coderboy
  • 1,710
  • 1
  • 6
  • 16
1
2