Questions tagged [psr-2]

0

votes
0

answer
3

Views

How can I modify PSR2 to check that all opening braces are on the new line everywhere

I've spent a lot of time to figure out how to require the { always on the new line (including inside functions). We have several projects with altered coding standards and I would like to write phpcs-ruleset.xml, so phpcs could check this standard. I know how to write phpcs-ruleset.xml based on PSR2...
Howard
1

votes
0

answer
113

Views

String Concatenation Convention

What is the best practice in concatenating multiple strings? What I did is every concatenation will be on the next line? I'm following the psr-2 80 line limit and the 120 line soft limit. e.g. return ( Config::get('url')['base_folder'] . Config::get('url')['user_image_folder'] . '/{$image_name}.jpg'...
Mr. Programmer
1

votes
0

answer
79

Views

Resolve a PHPCS error in a Laravel migration file

In all my laravel migrations I want to use PSR2, but I getting a warning. use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreatePlayersTable extends Migration { // migration } The error phpcs warning is Each class must be in a namespace of at least one...
LeBlaireau
1

votes
0

answer
63

Views

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() { // function body goes here } public function myFunction...
1

votes
1

answer
34

Views

Uppercase Booleans in Netbeans 8.2

When I using netbeans 8.2 for PHP code, it always give NULL FALSE TRUE in uppercase. But PSR-2 tells us The PHP constants true, false, and null MUST be in lower case. How I can change it, and netbeabs give it in lowercase
Los Vitaly
1

votes
1

answer
435

Views

Issue while validating with PSR-2 on Prestashop Validator

While validating my new module on Prestashop Validator, I am getting the following error in my main controller file (php file). End of line character is invalid; expected '\n' but found '\r\n' I am getting the error on the first line that only has the starting
Wolfack
1

votes
1

answer
473

Views

PSR-2 - Should the array opening bracket be on its own line

If we are conforming to PSR-2 standards, going off their description for multi-line arguments: Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per l...
Mikey
1

votes
2

answer
2k

Views

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 FooInterface { public function sampleFunction($a, $b = n...
Zdenek Machek
3

votes
2

answer
1.6k

Views

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 contain templates and other stuff: plugins PluginName...
ymakux
18

votes
2

answer
11.1k

Views

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
5

votes
1

answer
1.3k

Views

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
3

votes
1

answer
567

Views

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
5

votes
3

answer
1.8k

Views

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 SHOULD be 80 characters or less., it'd be nice if I cou...
user1960364
2

votes
3

answer
173

Views

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' . Or in other word, what will line ending cause ?
chenxinlong
3

votes
1

answer
2.4k

Views

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: Link Link Link And after running php-cs-fixer fix views/ui/nav.php --level=psr2: Li...
Josh Harrison
3

votes
2

answer
1.3k

Views

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 Client: \Vendor\GoogleClient\GoogleClient \Vendor\GithubClient\GithubCl...
3

votes
2

answer
629

Views

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 from being unanimous. However, the indenting sectio...
Álvaro González
3

votes
0

answer
832

Views

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' => 'value1', 'another_key' => 'value2', 'some_other_key' => 'value3' ]...
dav
2

votes
1

answer
179

Views

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 the variable from the docblock. I have been doing...
crmpicco
7

votes
4

answer
4.1k

Views

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 = ($error_status) ? 'Error' : 'No Error'; OR $error = $error_...
Wallter
5

votes
1

answer
617

Views

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 developer. I'm aware of the difference, but I'm curious if t...
Mikkel
2

votes
1

answer
85

Views

phpcs Is there a rule to disallow empty lines?

Is there a rule that I can use with phpcs that limits the number of consecutive empty lines to e.g. 3? I currently use PSR2, which does not take this into consideration at all.
Chris
1

votes
2

answer
543

Views

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 these case? sample #1 /* Read cached data */ if ($use...
KEINOS
13

votes
4

answer
7.4k

Views

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 PhpStorm has a way to add the 1 newline at end of file I a...
gmponos
3

votes
2

answer
1.1k

Views

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 were heavily forced upon me in my university days -...
Todd
2

votes
0

answer
729

Views

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 clicking on filename.php in netbeans. This fix only...
Shakun Chaudhary
2

votes
1

answer
24

Views

SonarQube: Message displayed in PHP code

I have this statement: return ++$maxContratNum; SonarQube display this message: Extract this increment or decrement operator into a dedicated statement what's meaning this message ?? Thank you
fatma_wings
8

votes
3

answer
4.5k

Views

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 --level=psr2 php php-cs-fixer.phar fix /path/to/project --level...
kabirbaidhya
4

votes
1

answer
421

Views

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
8

votes
1

answer
994

Views

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);
Marko Milivojevic
1

votes
3

answer
391

Views

How to implement a copy constructor in PHP?

I have a class Account which has a default constructor function: class Account { AccountType $type; AccountLabel[] $labels; AccountAttribute[] $attributes; // Initializes a new account and assigns labels to the new account. public function __construct( AccountType $type, AccountLabel[] $labels, Acco...
Believe2014
2

votes
1

answer
197

Views

PSR Standard One statement per line

Is this 2 statements or 1? if ($a == 'A' && $b == 'B') { // do something } How do I use two statements if I want to follow PSR standards? Should I use multiple if's? Like if inside an if?
Loveb Razil
2

votes
1

answer
241

Views

Verify if my yii 1.1.x project is PSR-2

If yii 1.1.x support PSR-2 ? I mean original framework? If there is some tool to verify if my yii 1.1.x project(or just php project) corresponds to PSR-2 ?I am tring to write in PSR-2, but old habits are difficult to overcome...
mstdmstd
2

votes
1

answer
389

Views

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?
szeksu
2

votes
2

answer
1.6k

Views

PHP-CS-FIXER is ignoring my config

I have created a file called .gm_cs in the root of my project and php-cs-fixer is installed globally. In the config file is the following
jsnfwlr
3

votes
1

answer
70

Views

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
2

votes
1

answer
118

Views

PSR for using description of class

I can't find anything saying in PSR about where should placed comment about class and namespace. Is it should be first description of class or namespace?
Timur