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
46
votes
5 answers

What is the difference between StudlyCaps and camelCase?

PSR suggests, method names MUST be declared in camelCase and class names MUST be declared in StudlyCaps.
kta
  • 19,412
  • 7
  • 65
  • 47
44
votes
4 answers

Objective reasons for using spaces instead of tabs for indentation?

Are there objective reasons for using spaces instead of tabs for indenting files as per PSR-2 standard, can someone provide: facts, references, specific expertise on which PSR-2 standard is based? Authors of PSR-2 standard had in mind something…
yergo
  • 4,761
  • 2
  • 19
  • 41
35
votes
4 answers

Add an empty line at end of file according to PSR-2 on PhpStorm

I use PSR-2 for code styling my code. When I inspect a file using Codesniffer most of the times I get the following error. 332 | ERROR | [x] Expected 1 newline at end of file; 0 found It's obvious how to fix this. What I need to know is if…
gmponos
  • 2,157
  • 4
  • 22
  • 33
24
votes
1 answer

PSR-2 if-statement — what is allowed?

Can I use if-statement like: if(true) return $value; Or must use always with braces: if(true) { return $value; }
Max Lipsky
  • 1,774
  • 1
  • 18
  • 29
13
votes
2 answers

What are the main differences between the PSR-2 coding standard and the Symfony2 code standard for phpcs?

I am trying to figure out which code style to enforce with the phpcs code sniffer. Since the popularity of Symfony2, it seems to be a good practice to use its code standard. On the other hand, its code style is based upon PSR2, so this seems to be…
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
13
votes
5 answers

Are parentheses required in PSR-2 PHP ternary syntax?

Question: are parentheses required in PSR-2 PHP ternary syntax? Looking for which (if either) of the following ternary statement's syntax is compliant with PSR-2 - I also need to be pointed to documentation or some authority link: $error =…
Tyler Wall
  • 3,747
  • 7
  • 37
  • 52
12
votes
1 answer

PSR2 - multiline array indentation between key and value

What is the correct way according to PSR2 for having spaces between keys and values of multiline array. $result = [ 'key1' => 'value1', 'another_key' => 'value2', 'some_other_key' => 'value3' ]; vs $result = [ 'key1' =>…
dav
  • 8,931
  • 15
  • 76
  • 140
12
votes
1 answer

Does PSR-2 require vertical alignment?

What is allowed between these two: $value = 'value'; $user = 'John'; $timestamp = 1480927909; $day = date('Y-m-d', $timestamp); or $value = 'value'; $user = 'John'; $timestamp = 1480927909; $day = date('Y-m-d', $timestamp);
9
votes
1 answer

PHP 's' in namespace: Controller or Controllers, Model or Models?

For example Symfony uses \Controller. Yii2 uses \controllers and \models. Is there a standard about ...s|es like PSR?
Ivan
  • 625
  • 1
  • 5
  • 18
8
votes
1 answer

When running phpcs, ERROR: Referenced sniff "PHPCompatibility" does not exist is coming

I want to run phpcs tool, but this error is coming, ERROR: Referenced sniff "PHPCompatibility" does not exist I ran phpcs -i. That gave me , The installed coding standards are PEAR, PSR1, Zend, Squiz, PSR12, PSR2, MySource and PHPCompatibility. But…
Viraj Amarasinghe
  • 911
  • 10
  • 20
8
votes
3 answers

php-cs-fixer: need more information on using fix --level option

Okay, I know php-cs-fixer allows following levels of fixes for coding standards: php php-cs-fixer.phar fix /path/to/project --level=psr0 php php-cs-fixer.phar fix /path/to/project --level=psr1 php php-cs-fixer.phar fix /path/to/project…
kabirbaidhya
  • 3,264
  • 3
  • 34
  • 59
7
votes
1 answer

Should I use new self or new static?

I work on a proprietary project that uses quite a lot of factories of one form or another. Most of them don't instantiate the class by name, fortunately, but whether new self() or new static() is used to instantiate varies depending on the…
Mikkel
  • 1,192
  • 9
  • 22
5
votes
2 answers

Best way to comment on if/else statement with PHP PSR-2

In my opinion, sample #2 seems to be the more readable way of commenting. But if I apply PSR-2 to both samples, sample #1 won't change but the result of sample #2 changes as below and it's not a proper comment. What is the best way to comment in…
KEINOS
  • 1,050
  • 2
  • 13
  • 32
5
votes
1 answer

Why Laravel migrations class doesn't have a namespace?

I am using Laravel 5.1 now. The migrations class file generated by php artisan make:migration create_users_table --create=users command, would be like this:
cjli
  • 61
  • 6
5
votes
2 answers

PHP Namespace Class Naming Convention

I currently follow PSR-2 and PSR-4. I'm running into a small dilemma when trying to name a few classes. Here's an example. I have a base REST client, \Vendor\RestClient\AbstractClient. I have two implementations of this Abstract…
1
2 3 4 5