SlideShare une entreprise Scribd logo
1  sur  25
Télécharger pour lire hors ligne
PSR-2:
Coding Style Guide
PSR-2's purpose is to have a single
style guide for PHP code that
results in uniformly formatted
shared code.
● PSR-2 is a coding style agreed upon by many
PHP framework developers
● It’s an extension of the original PSR-1,
which basically has 3 main conventions.
● The benefit of PSR-2 is not in the rules
themselves, but in the sharing of those
rules.
3 Main Conventions of PSR-1
● Classes are upper CamelCase.
● Methods are lower camelCase.
● Constants are written in caps like
VERSION_NUMBER.
Some other rules of PSR-1
● Files MUST use only <?php and <?= tags.
● Files MUST use only UTF-8 without BOM for
PHP code.
● Class names MUST be declared in StudlyCaps
(text notation in which the capitalization of letters
varies by some pattern, or arbitrarily, usually also
omitting spaces between words and often omitting some
letters).
etc...
PSR-2 expands PSR-1 by using these
standards:
● Indents are 4 spaces instead of tabs.
● All PHP files MUST end with a single blank line.
● The closing ?> tag MUST be omitted from files
containing only PHP.
● Keep line lengths at 80 characters. Soft limit of
120 characters.
● Use one blank line after a namespace declaration.
● Use a blank line after a group of declarations.
● There MUST NOT be trailing whitespace at the end of
non-blank lines.
● The `extends` and `implements` keywords MUST be
declared on the same line as the class name.
● Visibility MUST be declared on all methods.
● In the argument list, there MUST NOT be a space before
each comma, and there MUST be one space after each
comma.
● Opening braces for classes MUST go on the next line.
Closing brace must be on its own line.
● Opening braces for methods must go on the next line,
and a separate line for the closing brace.
● Closures must have a space after the function keyword.
● Never use the var keyword, this is used with
JavaScript and would cause some confusion.
● Control structures, like IF, WHICH, FOR, and FOREACH
must have one space before the condition parenthesis.
● The case keywords should be indented from the switch
keyword.
● Keywords, like true, false, and null should be in
lowercase.
Examples
that show how to
use PSR-2
CLASS
METHOD
PROPERTYWrong
Right
METHOD ARGUMENTS
CONTROL:
one space before the condition parenthesis
SWITCH
WHILE, DO WHILE
KEYWORD
PHP Mess Detector
(PHPMD)
What PHPMD does?
It takes a given PHP source code
base and look for several potential
problems within that source.
● PHPMD works through Rules – specific sets of
instructions on what to look for in source code
● There’s a wide variety of prepared rules and
users can write their own
● PHPMD can be installed globally via composer by
running
composer global require 'phpmd/phpmd=*'
Potential Problems within
the Source:
● Possible bugs
● Suboptimal code
● Overcomplicated expressions
● Unused parameters, methods, properties
etc.
Some examples of rules:
● Clean Code Rules: enforce a clean code base.
● Naming Rules: about names - too long, too short,
and so forth
● Unused Code Rules: finds unused code.
● ElseExpression: An if expression with an else
branch is never necessary. You can rewrite the
conditions in a way that the else is not necessary
and the code becomes simpler to read. To achieve
this use early return statements.
● ExcessiveMethodLength: Try to reduce the method
size by creating helper methods and removing
any copy/pasted code.
● ExcessiveClassLength: Try to break it down, and
reduce the size to something manageable.
● ExcessiveParameterList: Long parameter lists
can indicate that a new object should be
created to wrap the numerous parameters.
● ExcessivePublicCount: A large number of public
methods and attributes declared in a class can
indicate the class may need to be broken up.
● CamelCaseClassName: It is considered best practice
to use the CamelCase notation to name classes.
● CamelCasePropertyName: use the camelCase notation
to name attributes.
● CamelCaseMethodName: use the camelCase notation to
name methods.
● CamelCaseParameterName: use the camelCase notation
to name parameters.
● CamelCaseVariableName: use the camelCase notation
to name variables.
● NumberOfChildren: A class with an excessive number of
children is an indicator for an unbalanced class
hierarchy. You should consider to refactor this class
hierarchy.
● DepthOfInheritance: A class with many parents is an
indicator for an unbalanced and wrong class hierarchy.
● ShortVariable: Detects when a field, local, or
parameter has a very short name.
● LongVariable: Detects when a field, formal or local
variable is declared with a long name.
● ShortMethodName: Detects when very short method names
are used.
● ConstantNamingConventions: Class/Interface constant
names should always be defined in uppercase.

Contenu connexe

Tendances

BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)
BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)
BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)
Michael Smith
 

Tendances (12)

Subnetting
SubnettingSubnetting
Subnetting
 
Your First Step To The Software Industry
Your First Step To The Software IndustryYour First Step To The Software Industry
Your First Step To The Software Industry
 
Blockchain - HyperLedger Fabric
Blockchain - HyperLedger FabricBlockchain - HyperLedger Fabric
Blockchain - HyperLedger Fabric
 
Proprofs
ProprofsProprofs
Proprofs
 
BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)
BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)
BlackHat 2011 - Exploiting Siemens Simatic S7 PLCs (white paper)
 
Block chain
Block chainBlock chain
Block chain
 
Cryptocurrency
CryptocurrencyCryptocurrency
Cryptocurrency
 
Blockchain Supply Chain : Supply Chain Blockchain Use Cases
Blockchain Supply Chain : Supply Chain Blockchain Use CasesBlockchain Supply Chain : Supply Chain Blockchain Use Cases
Blockchain Supply Chain : Supply Chain Blockchain Use Cases
 
Design Pattern (Strategy & Template)
Design Pattern (Strategy & Template)Design Pattern (Strategy & Template)
Design Pattern (Strategy & Template)
 
Hyperledger Fabric Technical Deep Dive 20190618
Hyperledger Fabric Technical Deep Dive 20190618Hyperledger Fabric Technical Deep Dive 20190618
Hyperledger Fabric Technical Deep Dive 20190618
 
1. ibm blockchain explained
1. ibm blockchain explained1. ibm blockchain explained
1. ibm blockchain explained
 
Blockchain for Marketing & Insights
Blockchain for Marketing & InsightsBlockchain for Marketing & Insights
Blockchain for Marketing & Insights
 

Similaire à Psr 2 coding style guide - Tidepool Labs

Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
Rakesh Madugula
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
VinayakHospet1
 
Presentation
PresentationPresentation
Presentation
bugway
 

Similaire à Psr 2 coding style guide - Tidepool Labs (20)

Coding conventions
Coding conventionsCoding conventions
Coding conventions
 
Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.Let's PHP in a better way! - Coding Recommendations.
Let's PHP in a better way! - Coding Recommendations.
 
Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
 
PHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIGPHP Standards Recommendations - PHP-FIG
PHP Standards Recommendations - PHP-FIG
 
PPT 19.pptx
PPT 19.pptxPPT 19.pptx
PPT 19.pptx
 
introduction to server-side scripting
introduction to server-side scriptingintroduction to server-side scripting
introduction to server-side scripting
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Md06 advance class features
Md06 advance class featuresMd06 advance class features
Md06 advance class features
 
c-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.pptc-coding-standards-and-best-programming-practices.ppt
c-coding-standards-and-best-programming-practices.ppt
 
Presentation
PresentationPresentation
Presentation
 
Hsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdfHsc IT 5. Server-Side Scripting (PHP).pdf
Hsc IT 5. Server-Side Scripting (PHP).pdf
 
Lambdas in Java 8
Lambdas in Java 8Lambdas in Java 8
Lambdas in Java 8
 
An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)An intro to php standards recommendation (psr)
An intro to php standards recommendation (psr)
 
Server Scripting Language -PHP
Server Scripting Language -PHPServer Scripting Language -PHP
Server Scripting Language -PHP
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
In-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTMLIn-Depth Guide On WordPress Coding Standards For PHP & HTML
In-Depth Guide On WordPress Coding Standards For PHP & HTML
 
Java
JavaJava
Java
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
overview of c#
overview of c#overview of c#
overview of c#
 
Annotations
AnnotationsAnnotations
Annotations
 

Dernier

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 

Dernier (20)

Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 

Psr 2 coding style guide - Tidepool Labs

  • 2. PSR-2's purpose is to have a single style guide for PHP code that results in uniformly formatted shared code.
  • 3. ● PSR-2 is a coding style agreed upon by many PHP framework developers ● It’s an extension of the original PSR-1, which basically has 3 main conventions. ● The benefit of PSR-2 is not in the rules themselves, but in the sharing of those rules.
  • 4. 3 Main Conventions of PSR-1 ● Classes are upper CamelCase. ● Methods are lower camelCase. ● Constants are written in caps like VERSION_NUMBER.
  • 5. Some other rules of PSR-1 ● Files MUST use only <?php and <?= tags. ● Files MUST use only UTF-8 without BOM for PHP code. ● Class names MUST be declared in StudlyCaps (text notation in which the capitalization of letters varies by some pattern, or arbitrarily, usually also omitting spaces between words and often omitting some letters). etc...
  • 6. PSR-2 expands PSR-1 by using these standards: ● Indents are 4 spaces instead of tabs. ● All PHP files MUST end with a single blank line. ● The closing ?> tag MUST be omitted from files containing only PHP. ● Keep line lengths at 80 characters. Soft limit of 120 characters. ● Use one blank line after a namespace declaration. ● Use a blank line after a group of declarations.
  • 7. ● There MUST NOT be trailing whitespace at the end of non-blank lines. ● The `extends` and `implements` keywords MUST be declared on the same line as the class name. ● Visibility MUST be declared on all methods. ● In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
  • 8. ● Opening braces for classes MUST go on the next line. Closing brace must be on its own line. ● Opening braces for methods must go on the next line, and a separate line for the closing brace. ● Closures must have a space after the function keyword. ● Never use the var keyword, this is used with JavaScript and would cause some confusion. ● Control structures, like IF, WHICH, FOR, and FOREACH must have one space before the condition parenthesis. ● The case keywords should be indented from the switch keyword. ● Keywords, like true, false, and null should be in lowercase.
  • 9. Examples that show how to use PSR-2
  • 10. CLASS
  • 14. CONTROL: one space before the condition parenthesis
  • 19. What PHPMD does? It takes a given PHP source code base and look for several potential problems within that source.
  • 20. ● PHPMD works through Rules – specific sets of instructions on what to look for in source code ● There’s a wide variety of prepared rules and users can write their own ● PHPMD can be installed globally via composer by running composer global require 'phpmd/phpmd=*'
  • 21. Potential Problems within the Source: ● Possible bugs ● Suboptimal code ● Overcomplicated expressions ● Unused parameters, methods, properties etc.
  • 22. Some examples of rules: ● Clean Code Rules: enforce a clean code base. ● Naming Rules: about names - too long, too short, and so forth ● Unused Code Rules: finds unused code. ● ElseExpression: An if expression with an else branch is never necessary. You can rewrite the conditions in a way that the else is not necessary and the code becomes simpler to read. To achieve this use early return statements.
  • 23. ● ExcessiveMethodLength: Try to reduce the method size by creating helper methods and removing any copy/pasted code. ● ExcessiveClassLength: Try to break it down, and reduce the size to something manageable. ● ExcessiveParameterList: Long parameter lists can indicate that a new object should be created to wrap the numerous parameters. ● ExcessivePublicCount: A large number of public methods and attributes declared in a class can indicate the class may need to be broken up.
  • 24. ● CamelCaseClassName: It is considered best practice to use the CamelCase notation to name classes. ● CamelCasePropertyName: use the camelCase notation to name attributes. ● CamelCaseMethodName: use the camelCase notation to name methods. ● CamelCaseParameterName: use the camelCase notation to name parameters. ● CamelCaseVariableName: use the camelCase notation to name variables.
  • 25. ● NumberOfChildren: A class with an excessive number of children is an indicator for an unbalanced class hierarchy. You should consider to refactor this class hierarchy. ● DepthOfInheritance: A class with many parents is an indicator for an unbalanced and wrong class hierarchy. ● ShortVariable: Detects when a field, local, or parameter has a very short name. ● LongVariable: Detects when a field, formal or local variable is declared with a long name. ● ShortMethodName: Detects when very short method names are used. ● ConstantNamingConventions: Class/Interface constant names should always be defined in uppercase.